primitive_configuration.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright, 2000-2002 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #include "nel/ligo/primitive_configuration.h"
00025 #include "nel/ligo/ligo_config.h"
00026 #include "nel/ligo/primitive.h"
00027 #include "nel/misc/i_xml.h"
00028 
00029 using namespace std;
00030 using namespace NLMISC;
00031 using namespace NLLIGO;
00032 
00033 extern bool ReadColor (CRGBA &color, xmlNodePtr node);
00034 
00035 // ***************************************************************************
00036 
00037 bool CPrimitiveConfigurations::read (xmlNodePtr configurationNode, const char *filename, const char *name, NLLIGO::CLigoConfig &config)
00038 {
00039     // The name
00040     Name = name;
00041 
00042     // Read the color
00043     ReadColor (Color, configurationNode);
00044 
00045     // Get the first matching pair
00046     MatchPairs.reserve (CIXml::countChildren (configurationNode, "MATCH_GROUP"));
00047     xmlNodePtr matchGroups = CIXml::getFirstChildNode (configurationNode, "MATCH_GROUP");
00048     if (matchGroups)
00049     {
00050         do
00051         {
00052             // Add a pair
00053             MatchPairs.push_back(CMatchGroup());
00054             CMatchGroup &matchGroup = MatchPairs.back();
00055 
00056             // Get the first matching pair
00057             matchGroup.Pairs.reserve (CIXml::countChildren (matchGroups, "MATCH"));
00058             xmlNodePtr match = CIXml::getFirstChildNode (matchGroups, "MATCH");
00059             if (match)
00060             {
00061                 do
00062                 {
00063                     // Add the match
00064                     matchGroup.Pairs.resize (matchGroup.Pairs.size()+1);
00065                     std::pair<std::string, std::string> &pair = matchGroup.Pairs.back();
00066 
00067                     // Get the match name
00068                     std::string name;
00069                     if (config.getPropertyString (name, filename, match, "NAME"))
00070                     {
00071                         pair.first = name;
00072                     }
00073                     else
00074                     {
00075                         config.syntaxError (filename, match, "Missing match name in configuration (%s)", name.c_str());
00076                         return false;
00077                     }
00078 
00079                     // Get the match value
00080                     if (config.getPropertyString (name, filename, match, "VALUE"))
00081                     {
00082                         pair.second = name;
00083                     }
00084 
00085                     match = CIXml::getNextChildNode (match, "MATCH");
00086                 }
00087                 while (match);
00088             }
00089 
00090             matchGroups = CIXml::getNextChildNode (matchGroups, "MATCH_GROUP");
00091         }
00092         while (matchGroups);
00093     }
00094     return true;
00095 }
00096 
00097 // ***************************************************************************
00098 
00099 bool    CPrimitiveConfigurations::belong (const IPrimitive &primitive) const
00100 {
00101     // For each match group
00102     uint group;
00103     const uint numGroup = MatchPairs.size();
00104     for (group=0; group<numGroup; group++)
00105     {
00106         const CMatchGroup &matchGroup = MatchPairs[group];
00107 
00108         // For each rules
00109         uint rules;
00110         const uint numRules = matchGroup.Pairs.size();
00111         for (rules=0; rules<numRules; rules++)
00112         {
00113             const std::pair<std::string, std::string> &pairs = matchGroup.Pairs[rules];
00114             string key = toLower(pairs.second);
00115 
00116             // Get the property
00117             string value;
00118             if (primitive.getPropertyByName (pairs.first.c_str(), value))
00119             {
00120                 if (toLower(value) == key)
00121                     continue;
00122             }
00123 
00124             // Get the property
00125             const std::vector<string> *array = NULL;
00126             if (primitive.getPropertyByName (pairs.first.c_str(), array) && array)
00127             {
00128                 uint i;
00129                 for (i=0; i<array->size(); i++)
00130                 {
00131                     if (toLower((*array)[i]) == key)
00132                         break;
00133                 }
00134                 if (i!=array->size())
00135                     continue;
00136             }
00137 
00138             // Don't match
00139             break;
00140         }
00141 
00142         // Match ?
00143         if (rules == numRules)
00144             return true;
00145     }
00146     return false;
00147 }
00148 
00149 // ***************************************************************************
00150 

Generated on Thu Jan 7 08:26:33 2010 for NeL by  doxygen 1.6.1