Changeset 1052


Ignore:
Timestamp:
Apr 18, 2010, 4:39:57 AM (11 years ago)
Author:
Peter Johansson
Message:

refs #450. using fnmatch in codon fil name pattern match.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Configuration.cc

    r1038 r1052  
    8383  Configuration::codon(std::string file_name) const
    8484  {
     85    std::cout << "file_name: " << file_name << std::endl;
    8586    if (const std::pair<std::string,std::string>* dict=dictionary(file_name))
    8687      file_name = translate(file_name, *dict);
    8788    for (String2Codons::const_iterator i(string2codons_.begin());
    8889         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()))
    9191        return &i->second;
    92       }
    9392    }
    9493    return NULL;
     
    106105  {
    107106    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()))
    111108        return &dictionary_[i];
    112109    return NULL;
     
    230227          for (String2Codons::const_iterator i(string2codons_.begin());
    231228               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())) {
    234230              mess << "`" << i->first << "'";
    235231              break;
     
    370366                           const std::pair<std::string, std::string>& dic) const
    371367  {
    372     assert(svndigest::equal(str.begin(), str.end(),
    373                             dic.first.begin(), dic.first.end()));
    374368    std::string res;
    375369    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    }
    377376    for (std::string::const_iterator i(dic.second.begin());
    378377         i!=dic.second.end(); ++i) {
     
    387386          if (n)
    388387            mess << "because " << n << " is a too large.";
    389           throw std::runtime_error("");       
     388          throw std::runtime_error(mess.str());       
    390389        }
    391390        res += vec[n-1];
  • trunk/lib/Configuration.h

    r1023 r1052  
    141141       Translate string \a str using dictionary \a dict
    142142
    143        \note \a str must be equal to d.first (see function equal),
     143       \note \a str must be equal to d.first (see fnmatch),
    144144       or behavior is unspecified.
    145145
  • trunk/lib/utility.cc

    r978 r1052  
    3030#include <cstdlib>
    3131#include <cstring>
     32#include <fnmatch.h>
    3233#include <fstream>
    3334#include <sstream>
     
    106107
    107108
    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 
    129109  std::string file_name(const std::string& full_path)
    130110  {
     
    163143      --length;
    164144    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;
    165159  }
    166160
  • trunk/lib/utility.h

    r978 r1052  
    7878  std::string directory_name(std::string path);
    7979
    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 
    9080  ///
    9181  /// @return everything after last '/'
    9282  ///
    9383  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);
    9491
    9592  ///
  • trunk/test/config_test.cc

    r996 r1052  
    5959      suite.add(false);
    6060    }
    61     if (!c.codon("../dir/test.cc")){
     61    if (!c.codon("/dir/test.cc")){
    6262      suite.out() << "No codon for test.cc\n";
    6363      suite.add(false);
  • trunk/test/utility_test.cc

    r978 r1052  
    2929
    3030bool test_hex(int, unsigned int, std::string);
    31 bool test_equal(bool, std::string, std::string);
     31bool test_fnmatch(bool, std::string, std::string);
    3232bool test_regexp(bool, std::string, std::string,
    3333                 const std::vector<std::string>&);
     
    4141  ok &= test_hex(16,2, "10");
    4242
    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");
    4957
    5058  std::vector<std::string> vec;
     
    6977}
    7078
    71 bool test_equal(bool answ, std::string a, std::string b)
     79bool test_fnmatch(bool answ, std::string a, std::string b)
    7280{
    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)
    7484    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
    7787            << ". Expects " << answ << std::endl;
    7888  return false;
     
    107117  }
    108118  return true;
    109 
    110119}
Note: See TracChangeset for help on using the changeset viewer.