Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Feb 17th, 2013  |  syntax: None  |  size: 3.06 KB  |  hits: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.   bool evaluate(const string &query) const {
  2.     if(query.empty()) return true;
  3.     lstring rules = string{query}.replace(" ", "").split(",");
  4.  
  5.     for(auto &rule : rules) {
  6.       enum class Comparator : unsigned { ID, EQ, NE, LT, LE, GT, GE };
  7.       auto comparator = Comparator::ID;
  8.            if(rule.wildcard("*!=*")) comparator = Comparator::NE;
  9.       else if(rule.wildcard("*<=*")) comparator = Comparator::LE;
  10.       else if(rule.wildcard("*>=*")) comparator = Comparator::GE;
  11.       else if(rule.wildcard ("*=*")) comparator = Comparator::EQ;
  12.       else if(rule.wildcard ("*<*")) comparator = Comparator::LT;
  13.       else if(rule.wildcard ("*>*")) comparator = Comparator::GT;
  14.  
  15.       if(comparator == Comparator::ID) {
  16.         if(find(rule).size()) continue;
  17.         return false;
  18.       }
  19.  
  20.       lstring side;
  21.       switch(comparator) {
  22.       case Comparator::EQ: side = rule.split<1> ("="); break;
  23.       case Comparator::NE: side = rule.split<1>("!="); break;
  24.       case Comparator::LT: side = rule.split<1> ("<"); break;
  25.       case Comparator::LE: side = rule.split<1>("<="); break;
  26.       case Comparator::GT: side = rule.split<1> (">"); break;
  27.       case Comparator::GE: side = rule.split<1>(">="); break;
  28.       }
  29.  
  30.       string data = text();
  31.       if(side(0).empty() == false) {
  32.         auto result = find(side(0));
  33.         if(result.size() == 0) return false;
  34.         data = result(0).data;
  35.       }
  36.  
  37.       switch(comparator) {
  38.       case Comparator::EQ: if(data.wildcard(side(1)) ==  true)   continue; break;
  39.       case Comparator::NE: if(data.wildcard(side(1)) == false)   continue; break;
  40.       case Comparator::LT: if(numeral(data)  < numeral(side(1))) continue; break;
  41.       case Comparator::LE: if(numeral(data) <= numeral(side(1))) continue; break;
  42.       case Comparator::GT: if(numeral(data)  > numeral(side(1))) continue; break;
  43.       case Comparator::GE: if(numeral(data) >= numeral(side(1))) continue; break;
  44.       }
  45.  
  46.       return false;
  47.     }
  48.  
  49.     return true;
  50.   }
  51.  
  52.   vector<Node> find(const string &query) const {
  53.     vector<Node> result;
  54.  
  55.     lstring path = query.split("/");
  56.     string name = path.take(0), rule;
  57.     unsigned lo = 0u, hi = ~0u;
  58.  
  59.     if(name.wildcard("*[*]")) {
  60.       lstring side = name.split<1>("[");
  61.       name = side(0);
  62.       side = side(1).rtrim<1>("]").split<1>("-");
  63.       lo = side(0).empty() ?  0u : numeral(side(0));
  64.       hi = side(1).empty() ? ~0u : numeral(side(1));
  65.     }
  66.  
  67.     if(name.wildcard("*(*)")) {
  68.       lstring side = name.split<1>("(");
  69.       name = side(0);
  70.       rule = side(1).rtrim<1>(")");
  71.     }
  72.  
  73.     unsigned position = 0;
  74.     for(auto &node : children) {
  75.       if(node.name.wildcard(name) == false) continue;
  76.       if(node.evaluate(rule) == false) continue;
  77.  
  78.       bool inrange = position >= lo && position <= hi;
  79.       position++;
  80.       if(inrange == false) continue;
  81.  
  82.       if(path.size() == 0) result.append(node);
  83.       else {
  84.         auto list = node.find(path.concatenate("/"));
  85.         for(auto &item : list) result.append(item);
  86.       }
  87.     }
  88.  
  89.     return result;
  90.   }