]> git.tdb.fi Git - libs/core.git/blob - source/glob.cpp
Add Id tags and copyright notices to a few files that were missing them
[libs/core.git] / source / glob.cpp
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2007 Mikko Rasa
5 Distributed under the LGPL
6 */
7 #include "glob.h"
8
9 using namespace std;
10
11 namespace {
12
13 template<bool icase>
14 bool cmp(char c, char h);
15
16 template<>
17 bool cmp<true>(char c, char h)
18 { return tolower(c)==tolower(h); }
19
20 template<>
21 bool cmp<false>(char c, char h)
22 { return c==h; }
23
24 template<bool icase>
25 bool globmatch(string::const_iterator pat_i, const string::const_iterator &pat_e, string::const_iterator str_i, const string::const_iterator &str_e)
26 {
27         while(pat_i!=pat_e && str_i!=str_e)
28         {
29                 if(*pat_i=='?' || cmp<icase>(*str_i, *pat_i))
30                 {
31                         ++pat_i;
32                         ++str_i;
33                 }
34                 else if(*pat_i=='*')
35                 {
36                         ++pat_i;
37                         if(pat_i==pat_e)
38                                 return true;
39                         
40                         for(; str_i!=str_e; ++str_i)
41                                 if(cmp<icase>(*str_i, *pat_i) && globmatch<icase>(pat_i, pat_e, str_i, str_e))
42                                         return true;
43                         
44                         return false;
45                 }
46                 else
47                         return false;
48         }
49
50         return pat_i==pat_e && str_i==str_e;
51 }
52
53 }
54
55 namespace Msp {
56
57 bool globmatch(const string &pat, const string &str)
58 {
59         return ::globmatch<false>(pat.begin(), pat.end(), str.begin(), str.end());
60 }
61
62 bool globcasematch(const string &pat, const string &str)
63 {
64         return ::globmatch<true>(pat.begin(), pat.end(), str.begin(), str.end());
65 }
66
67 } // namespace Msp