X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstrings%2Fglob.cpp;fp=source%2Fstrings%2Fglob.cpp;h=0af6b9bbbbd2b81d3421878201ae7c8fcfbe23db;hp=0000000000000000000000000000000000000000;hb=b42ed73a1b241c0e93ee03c43c4584b41c549bac;hpb=5b1368cb791cab043f0435628cacbaff36e39b7b diff --git a/source/strings/glob.cpp b/source/strings/glob.cpp new file mode 100644 index 0000000..0af6b9b --- /dev/null +++ b/source/strings/glob.cpp @@ -0,0 +1,68 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2007 Mikko Rasa +Distributed under the LGPL +*/ + +#include "glob.h" + +using namespace std; + +namespace { + +template +bool cmp(char c, char h); + +template<> +bool cmp(char c, char h) +{ return tolower(c)==tolower(h); } + +template<> +bool cmp(char c, char h) +{ return c==h; } + +template +bool globmatch(string::const_iterator pat_i, const string::const_iterator &pat_e, string::const_iterator str_i, const string::const_iterator &str_e) +{ + while(pat_i!=pat_e && str_i!=str_e) + { + if(*pat_i=='?' || cmp(*str_i, *pat_i)) + { + ++pat_i; + ++str_i; + } + else if(*pat_i=='*') + { + ++pat_i; + if(pat_i==pat_e) + return true; + + for(; str_i!=str_e; ++str_i) + if(cmp(*str_i, *pat_i) && globmatch(pat_i, pat_e, str_i, str_e)) + return true; + + return false; + } + else + return false; + } + + return pat_i==pat_e && str_i==str_e; +} + +} + +namespace Msp { + +bool globmatch(const string &pat, const string &str) +{ + return ::globmatch(pat.begin(), pat.end(), str.begin(), str.end()); +} + +bool globcasematch(const string &pat, const string &str) +{ + return ::globmatch(pat.begin(), pat.end(), str.begin(), str.end()); +} + +} // namespace Msp