Changeset 1052
- Timestamp:
- Apr 18, 2010, 4:39:57 AM (11 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Configuration.cc
r1038 r1052 83 83 Configuration::codon(std::string file_name) const 84 84 { 85 std::cout << "file_name: " << file_name << std::endl; 85 86 if (const std::pair<std::string,std::string>* dict=dictionary(file_name)) 86 87 file_name = translate(file_name, *dict); 87 88 for (String2Codons::const_iterator i(string2codons_.begin()); 88 89 i!=string2codons_.end(); ++i) { 89 if (svndigest::equal(file_name.begin(), file_name.end(), 90 i->first.begin(), i->first.end()) ) { 90 if (fnmatch(i->first.c_str(), file_name.c_str())) 91 91 return &i->second; 92 }93 92 } 94 93 return NULL; … … 106 105 { 107 106 for (size_t i=0; i<dictionary_.size(); ++i) 108 if (svndigest::equal(lhs.begin(), lhs.end(), 109 dictionary_[i].first.begin(), 110 dictionary_[i].first.end())) 107 if (fnmatch(lhs.c_str(), dictionary_[i].first.c_str())) 111 108 return &dictionary_[i]; 112 109 return NULL; … … 230 227 for (String2Codons::const_iterator i(string2codons_.begin()); 231 228 i!=string2codons_.end(); ++i) { 232 if (svndigest::equal(lhs.begin(), lhs.end(), 233 i->first.begin(), i->first.end()) ) { 229 if (fnmatch(lhs.c_str(), i->first.c_str())) { 234 230 mess << "`" << i->first << "'"; 235 231 break; … … 370 366 const std::pair<std::string, std::string>& dic) const 371 367 { 372 assert(svndigest::equal(str.begin(), str.end(),373 dic.first.begin(), dic.first.end()));374 368 std::string res; 375 369 std::vector<std::string> vec; 376 regexp(str.begin(), str.end(), dic.first.begin(), dic.first.end(), vec); 370 if (!regexp(str.begin(), str.end(), dic.first.begin(),dic.first.end(),vec)){ 371 std::stringstream mess; 372 mess << "svndigest: invalid config file: " 373 << "expression " << dic.first << " is invalid"; 374 throw std::runtime_error(mess.str()); 375 } 377 376 for (std::string::const_iterator i(dic.second.begin()); 378 377 i!=dic.second.end(); ++i) { … … 387 386 if (n) 388 387 mess << "because " << n << " is a too large."; 389 throw std::runtime_error( "");388 throw std::runtime_error(mess.str()); 390 389 } 391 390 res += vec[n-1]; -
trunk/lib/Configuration.h
r1023 r1052 141 141 Translate string \a str using dictionary \a dict 142 142 143 \note \a str must be equal to d.first (see f unction equal),143 \note \a str must be equal to d.first (see fnmatch), 144 144 or behavior is unspecified. 145 145 -
trunk/lib/utility.cc
r978 r1052 30 30 #include <cstdlib> 31 31 #include <cstring> 32 #include <fnmatch.h> 32 33 #include <fstream> 33 34 #include <sstream> … … 106 107 107 108 108 bool equal(std::string::const_iterator first1,109 std::string::const_iterator end1,110 std::string::const_iterator first2,111 std::string::const_iterator end2)112 {113 if (first1==end1 && first2==end2)114 return true;115 if (first1!=end1 && first2!=end2 &&116 (*first1==*first2 || *first1=='*' || *first2=='*' ||117 *first1=='?' || *first2=='?') &&118 equal(first1+1, end1, first2+1, end2) )119 return true;120 if ( (first1!=end1 && first2!=end2 && (*first1=='*'|| *first2=='*') ) &&121 (equal(first1+1, end1, first2, end2) ||122 equal(first1, end1, first2+1, end2))123 )124 return true;125 return false;126 }127 128 129 109 std::string file_name(const std::string& full_path) 130 110 { … … 163 143 --length; 164 144 return str.substr(0,length); 145 } 146 147 148 bool fnmatch(const std::string& pattern, const std::string& str) 149 { 150 int res = ::fnmatch(pattern.c_str(), str.c_str(), FNM_PERIOD); 151 if (res==0) 152 return true; 153 if (res!=FNM_NOMATCH) { 154 std::stringstream ss; 155 ss << "svndigest: fnmatch with args: " << pattern << ", " << str; 156 throw std::runtime_error(ss.str()); 157 } 158 return false; 165 159 } 166 160 -
trunk/lib/utility.h
r978 r1052 78 78 std::string directory_name(std::string path); 79 79 80 /**81 Function to compare strings. Strings may contain '*'.82 83 \return true if \a a and \a b are identical.84 */85 bool equal(std::string::const_iterator first1,86 std::string::const_iterator end1,87 std::string::const_iterator first2,88 std::string::const_iterator end2);89 90 80 /// 91 81 /// @return everything after last '/' 92 82 /// 93 83 std::string file_name(const std::string&); 84 85 /** 86 \return true if \a str matches \a pattern 87 88 \see fnmatch(3) 89 */ 90 bool fnmatch(const std::string& pattern, const std::string& str); 94 91 95 92 /// -
trunk/test/config_test.cc
r996 r1052 59 59 suite.add(false); 60 60 } 61 if (!c.codon(" ../dir/test.cc")){61 if (!c.codon("/dir/test.cc")){ 62 62 suite.out() << "No codon for test.cc\n"; 63 63 suite.add(false); -
trunk/test/utility_test.cc
r978 r1052 29 29 30 30 bool test_hex(int, unsigned int, std::string); 31 bool test_ equal(bool, std::string, std::string);31 bool test_fnmatch(bool, std::string, std::string); 32 32 bool test_regexp(bool, std::string, std::string, 33 33 const std::vector<std::string>&); … … 41 41 ok &= test_hex(16,2, "10"); 42 42 43 ok &= test_equal(true,"peter", "peter"); 44 ok &= test_equal(false,"peter", "peterj"); 45 ok &= test_equal(true,"p*", "peterj"); 46 ok &= test_equal(true,"peter", "p*"); 47 ok &= test_equal(false,"peter", "p*j"); 48 ok &= test_equal(true,"peter", "*peter"); 43 ok &= test_fnmatch(true,"peter", "peter"); 44 ok &= test_fnmatch(false,"peter", "peterj"); 45 ok &= test_fnmatch(true,"p*", "peterj"); 46 ok &= test_fnmatch(true, "p*", "peter"); 47 ok &= test_fnmatch(false, "p*j", "peter"); 48 ok &= test_fnmatch(true, "*peter", "peter"); 49 50 ok &= test_fnmatch(true, "filename", "filename"); 51 ok &= test_fnmatch(true, "*name", "filename"); 52 ok &= test_fnmatch(true, "[fa]il?name", "filename"); 53 ok &= test_fnmatch(true, "*name", "/path/to/filename"); 54 ok &= test_fnmatch(true, "*name", "file.name"); 55 // posix dictates that leading persion can not be matched by wildcard 56 ok &= test_fnmatch(false, "*.txt", ".file.txt"); 49 57 50 58 std::vector<std::string> vec; … … 69 77 } 70 78 71 bool test_ equal(bool answ, std::string a, std::string b)79 bool test_fnmatch(bool answ, std::string a, std::string b) 72 80 { 73 if (theplu::svndigest::equal(a.begin(), a.end(), b.begin(), b.end())==answ) 81 using namespace theplu::svndigest; 82 bool res = fnmatch(a.c_str(), b.c_str()); 83 if (res == answ) 74 84 return true; 75 std::cerr << " equal(" << a << ", " << b << ") results "76 << theplu::svndigest::equal(a.begin(), a.end(),b.begin(), b.end())85 std::cerr << "fnmatch(" << a << ", " << b << ") results " 86 << res 77 87 << ". Expects " << answ << std::endl; 78 88 return false; … … 107 117 } 108 118 return true; 109 110 119 }
Note: See TracChangeset
for help on using the changeset viewer.