Changeset 222

Show
Ignore:
Timestamp:
02/21/07 22:31:44 (1 year ago)
Author:
ug
Message:

Make SGF parser handle long property names.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 06/libkombilo/sgfparser.cpp

    r210 r222  
    144144 
    145145    while (i < lSGFstring && s[i] != '[' && IDindex < 30) { 
    146       if (65 <= s[i] && s[i] <= 90) 
     146      if (65 <= s[i] && s[i] <= 90) { 
    147147        ID[IDindex++] = s[i]; 
    148       else if (!(97 <= s[i] && s[i] <= 122) && !(s[i]==' ' || s[i]=='\n' || s[i]=='\r' || s[i]=='\t')) { 
     148      } else if (!(97 <= s[i] && s[i] <= 122) && (!(s[i]==' ' || s[i]=='\n' || s[i]=='\r' || s[i]=='\t'))) { 
    149149        throw SGFError(); 
    150150      } 
     
    214214} 
    215215 
     216PropValue::PropValue(std::string IDC, std::vector<std::string>* PV) { 
     217  IDcomplete = IDC; 
     218  pv = PV; 
     219} 
     220 
     221PropValue::~PropValue() { 
     222  if (pv) delete pv; 
     223} 
     224 
     225 
    216226Node::Node(Node* prev, char* SGFst, int lvl=0) throw(SGFError) { 
    217227  next = NULL; 
     
    234244} 
    235245 
     246string remove_lowercase(string s) throw(SGFError) { 
     247  char ID[s.size()+1]; 
     248  int IDindex = 0; 
     249  for(unsigned int i=0; i<s.size(); i++) { 
     250    if (65 <= s[i] && s[i] <= 90) ID[IDindex++] = s[i]; 
     251    else if (!(97 <= s[i] && s[i] <= 122)) { 
     252      throw SGFError(); 
     253    } 
     254  } 
     255  ID[IDindex] = 0; 
     256  return string(ID); 
     257} 
     258 
    236259vector<string> Node::gpv(const string& prop) { 
    237260  vector<string>* result = get_property_value(prop); 
     
    244267    parseNode(); 
    245268  } 
    246   map<string, vector<string>* >::iterator result = data.find(prop); 
     269  map<string, PropValue >::iterator result = data.find(remove_lowercase(prop)); 
    247270  if (result == data.end()) return 0; 
    248   else return result->second
     271  else return result->second.pv
    249272} 
    250273 
     
    291314      char ID[30]; 
    292315      int IDindex = 0; 
    293  
    294       while (i < lSGFstring && s[i] != '[' && IDindex < 30) { 
    295         if (65 <= s[i] && s[i] <= 90) 
     316      char IDcomplete[100]; // store long property name here 
     317      int IDcompleteIndex = 0; 
     318 
     319      while (i < lSGFstring && s[i] != '[' && IDindex < 30 && IDcompleteIndex < 100) { 
     320        if (65 <= s[i] && s[i] <= 90) { 
    296321          ID[IDindex++] = s[i]; 
    297         else if (!(97 <= s[i] && s[i] <= 122) && !(s[i]==' ' || s[i]=='\n' || s[i]=='\r' || s[i]=='\t')) { 
     322          IDcomplete[IDcompleteIndex++] = s[i]; 
     323        } else if (97 <= s[i] && s[i] <= 122) { 
     324          IDcomplete[IDcompleteIndex++] = s[i]; 
     325        } else if (!(s[i]==' ' || s[i]=='\n' || s[i]=='\r' || s[i]=='\t')) { 
    298326          throw SGFError(); 
    299327        } 
     
    302330      i++; 
    303331 
    304       if (i >= lSGFstring || IDindex >= 30 || !IDindex) throw SGFError(); 
     332      if (i >= lSGFstring || IDindex >= 30 || !IDindex || IDcompleteIndex >= 100) { 
     333        throw SGFError(); 
     334      } 
    305335      ID[IDindex] = 0; 
     336      IDcomplete[IDcompleteIndex] = 0; 
    306337      vector<string>* propValueList = new vector<string>; 
    307338 
     
    352383        else i++; 
    353384      } 
    354       data.insert(make_pair(string(ID), propValueList)); 
     385      data.insert(make_pair(string(ID), PropValue(string(IDcomplete), propValueList))); 
    355386    } 
    356387    parsed = 1; 
     
    358389}     
    359390 
    360 void Node::set_property_value(string& ID, vector<string>* propValue) throw(SGFError) { 
    361   map<string, vector<string>* >::iterator it = data.find(ID); 
    362   if (it == data.end()) data.insert(make_pair(ID, propValue)); 
    363   else it->second->insert(it->second->end(), propValue->begin(), propValue->end()); 
     391void Node::set_property_value(string& IDcomplete, vector<string>* propValue) throw(SGFError) { 
     392  string ID = remove_lowercase(IDcomplete); 
     393  map<string, PropValue >::iterator it = data.find(ID); 
     394  if (it == data.end()) data.insert(make_pair(ID, PropValue(IDcomplete, propValue))); 
     395  else it->second.pv->insert(it->second.pv->end(), propValue->begin(), propValue->end()); 
    364396  SGFstring = nodeToString(data); 
    365397  parsed = 1; 
     
    877909// } 
    878910  
    879 string nodeToString(map<string, vector<string>* >& data) throw(SGFError) { 
     911string nodeToString(map<string, PropValue >& data) throw(SGFError) { 
    880912  string result = ";"; 
    881913  int l = 0; 
    882   for(map<string, vector<string>* >::iterator kv = data.begin(); kv != data.end(); kv++) { 
    883     if (!kv->second || !kv->second->size()) continue; 
    884  
    885     result += kv->first
    886     for(vector<string>::iterator it = kv->second->begin(); it != kv->second->end(); it++) { 
     914  for(map<string, PropValue >::iterator kv = data.begin(); kv != data.end(); kv++) { 
     915    if (!kv->second.pv || !kv->second.pv->size()) continue; 
     916 
     917    result += kv->second.IDcomplete
     918    for(vector<string>::iterator it = kv->second.pv->begin(); it != kv->second.pv->end(); it++) { 
    887919      char* t = SGFescape(it->c_str()); 
    888920      result += "["; 
  • 06/libkombilo/sgfparser.h

    r195 r222  
    5959char* SGFescape(const char* s); 
    6060 
     61class PropValue { 
     62  public: 
     63    PropValue(std::string IDC, std::vector<std::string>* PV); 
     64    ~PropValue(); 
     65    std::string IDcomplete; 
     66    std::vector<std::string>* pv; 
     67}; 
     68 
    6169class Node { 
    6270  public: 
     
    7179    std::vector<std::string> gpv(const std::string& prop); 
    7280    std::vector<std::string>* get_property_value(const std::string& prop); 
    73     void set_property_value(std::string& ID, std::vector<std::string>* propValue) throw(SGFError); 
     81    void set_property_value(std::string& IDcomplete, std::vector<std::string>* propValue) throw(SGFError); 
    7482 
    7583    int posyD; // used when displaying SGF structure graphically as a tree 
     
    8189    static int sloppy; 
    8290  private: 
    83     std::map<std::string, std::vector<std::string>* > data; // use get_property_value to access this 
     91    std::map<std::string, PropValue> data; // use get_property_value to access this 
    8492}; 
    8593 
     
    120128}; 
    121129 
    122 std::string nodeToString(std::map<std::string, std::vector<std::string>* >& data) throw(SGFError); 
     130std::string nodeToString(std::map<std::string, PropValue >& data) throw(SGFError); 
    123131// char* rootNodeToString(PyObject* data); 
    124132