Changeset 516 for trunk/lib/utility.cc


Ignore:
Timestamp:
Dec 9, 2007, 8:58:54 AM (14 years ago)
Author:
Peter Johansson
Message:

adding filename translations in Configuration class. Before checking what type parsing to use, filename is translated according to rules set in config file. Default rule (as before) is to remove trailing .in from filenames. To solve the parsing I added a simple regexp function - see utility.h

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/utility.cc

    r514 r516  
    2424#include "utility.h"
    2525
     26#include <cassert>
    2627#include <cerrno>
    2728#include <cstdlib>
     
    208209
    209210
     211  bool regexp(std::string::const_iterator first1,
     212              std::string::const_iterator last1,
     213              std::string::const_iterator first2,
     214              std::string::const_iterator last2,
     215              std::vector<std::string>& vec)
     216  {
     217    if (vec.empty())
     218      vec.push_back("");
     219
     220    // first two cases when ranges are empty
     221    if (first1==last1 && first2==last2){
     222      vec.pop_back();
     223      return true;
     224    }
     225    if (first1==last1 || first2==last2)
     226      return false;
     227
     228    // then we take care of the real stuff
     229    if (*first2 == '*'){
     230      // trying '*' to consume another character
     231      vec.back().append(1, *first1);
     232      if (regexp(first1+1, last1, first2, last2, vec) )
     233        return true;
     234      assert(vec.back().size());
     235      vec.back().resize(vec.back().size()-1);
     236
     237      // stepping away from the '*'
     238      vec.push_back("");
     239      if (regexp(first1, last1, first2+1, last2, vec))
     240        return true;
     241      vec.pop_back();
     242      return false;
     243    }
     244    else if (*first2 == '?'){
     245      // eating a character
     246      vec.back() = std::string(first1, first1+1);
     247      vec.push_back("");
     248      if (regexp(first1+1, last1, first2+1, last2, vec) )
     249        return true;
     250      vec.pop_back();
     251      // ? interpreted as zero characters
     252      vec.back() = "";
     253      vec.push_back("");
     254      if (regexp(first1, last1, first2+1, last2, vec) )
     255        return true;
     256      vec.pop_back();
     257      return false;
     258    }
     259    if (*first1 != *first2)
     260      return false;
     261    return regexp(++first1, last1, ++first2, last2, vec);
     262  }
     263
     264
    210265  void replace(std::string& str, std::string old_str, std::string new_str)
    211266  {
Note: See TracChangeset for help on using the changeset viewer.