00001 /* 00002 * Copyright (C) Sergey P. Derevyago, 2003-2004. 00003 * 00004 * Permission to copy, use, modify, sell and distribute this software is granted 00005 * provided this copyright notice appears in all copies. 00006 * This software is provided "as is" without express or implied warranty, and 00007 * with no claim as to its suitability for any purpose. 00008 * 00009 */ 00010 00015 #include "namematcher.hpp" 00016 #include <ctype.h> 00017 00018 using namespace std; 00019 00020 namespace { 00021 00026 class NameMatcherImpl : public NameMatcher { 00028 string suff; 00030 int send; 00031 00038 virtual bool match(const std::string& fileName); 00039 00040 public: 00044 NameMatcherImpl(const std::string& mask); 00045 }; 00046 00047 NameMatcherImpl::NameMatcherImpl(const std::string& mask) 00048 { 00049 int beg= (mask[0]=='*') ? 1 : 0; 00050 suff.reserve(mask.size()-beg); 00051 00052 for (int i=beg; i<mask.size(); i++) suff.push_back(tolower(mask[i])); 00053 send=suff.size()-1; 00054 } 00055 00056 bool NameMatcherImpl::match(const std::string& fileName) 00057 { 00058 if (fileName.size()<suff.size()) return 0; 00059 00060 for (int fend=fileName.size()-1, i=0; i<suff.size(); i++) 00061 if (tolower(fileName[fend-i])!=suff[send-i]) return 0; 00062 00063 return 1; 00064 } 00065 00066 } 00067 00068 sh_ptr<NameMatcher> Factory::newNameMatcher(const std::string& mask) 00069 { 00070 try { return sh_ptr<NameMatcher>(new NameMatcherImpl(mask)); } 00071 catch (...) { 00072 throw newCException(_FLINE_, "Can't create NameMatcherImpl object", 00073 toCException(_FLINE_)); 00074 } 00075 }