# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

string_id_array.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_STRING_ID_ARRAY_H
00027 #define NL_STRING_ID_ARRAY_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 
00031 #include <math.h>
00032 #include <string>
00033 #include <vector>
00034 #include <set>
00035 #include <algorithm>
00036 
00037 #include "nel/misc/debug.h"
00038 
00039 
00040 namespace NLMISC {
00041 
00042 
00050 class CStringIdArray
00051 {
00052 public:
00053 
00054         typedef sint16 TStringId;
00055 
00056         CStringIdArray () : _IgnoreAllUnknownId(false) { }
00057 
00059         void addString(const std::string &str, TStringId id)
00060         {
00061                 nlassert (id >= 0 && id < pow(2, sizeof (TStringId)*8));
00062                 nlassert (!str.empty());
00063 
00064                 if (id >= (sint32) _StringArray.size())
00065                         _StringArray.resize(id+1);
00066 
00067                 _StringArray[id] = str;
00068 
00069                 if (!_IgnoreAllUnknownId)
00070                 {
00071                         _AskedStringArray.erase (str);
00072                         _NeedToAskStringArray.erase (str);
00073                 }
00074         }
00075 
00077         void addString(const std::string &str)
00078         {
00079                 nlassert (_StringArray.size () < pow(2, sizeof (TStringId)*8));
00080                 nlassert (!str.empty());
00081 
00082                 // add at the end
00083                 addString (str, _StringArray.size ());
00084         }
00085 
00089         TStringId getId (const std::string &str, bool IgnoreIfUnknown = false)
00090         {
00091                 nlassert (!str.empty());
00092 
00093                 // sorry for this bullshit but it's the simplest way ;)
00094                 if (this == NULL) return -1;
00095 
00096                 for (TStringId i = 0; i < (TStringId) _StringArray.size(); i++)
00097                 {
00098                         if (_StringArray[i] == str)
00099                                 return i;
00100                 }
00101 
00102                 if (!_IgnoreAllUnknownId && !IgnoreIfUnknown)
00103                 {
00104                         // the string is not found, add it to the _AskedStringArray if necessary
00105                         if (_NeedToAskStringArray.find (str) == _NeedToAskStringArray.end ())
00106                         {
00107                                 if (_AskedStringArray.find (str) == _AskedStringArray.end ())
00108                                 {
00109                                         //nldebug ("String '%s' not found, add it to _NeedToAskStringArray", str.c_str ());     
00110                                         _NeedToAskStringArray.insert (str);
00111                                 }
00112                                 else
00113                                 {
00114                                         //nldebug ("Found '%s' in the _AskedStringArray", str.c_str ());        
00115                                 }
00116                         }
00117                         else
00118                         {
00119                                 //nldebug ("Found '%s' in the _NeedToAskStringArray", str.c_str ());    
00120                         }
00121                 }
00122                 else
00123                 {
00124                         //nldebug ("Ignoring unknown association ('%s')", str.c_str ());        
00125                 }
00126 
00127                 return -1;
00128         }
00129 
00131         std::string getString (TStringId id) const
00132         {
00133                 // sorry for this bullshit but it's the simplest way ;)
00134                 if (this == NULL) return "<NoSIDA>";
00135 
00136                 nlassert (id >= 0 && id < (TStringId)_StringArray.size());
00137 
00138                 return _StringArray[id];
00139         }
00140 
00142         void resize (TStringId size)
00143         {
00144                 _StringArray.resize (size);
00145         }
00146 
00148         TStringId size () const
00149         {
00150                 return _StringArray.size ();
00151         }
00152 
00154         const std::set<std::string> &getNeedToAskedStringArray () const
00155         {
00156                 return _NeedToAskStringArray;
00157         }
00158 
00160         const std::set<std::string> &getAskedStringArray () const
00161         {
00162                 return _AskedStringArray;
00163         }
00164 
00166         void moveNeedToAskToAskedStringArray ()
00167         {
00168                 if (!_IgnoreAllUnknownId)
00169                 {
00170                         _AskedStringArray.insert (_NeedToAskStringArray.begin(), _NeedToAskStringArray.end());
00171                         _NeedToAskStringArray.clear ();
00172                 }
00173         }
00174 
00176         void ignoreAllUnknownId (bool b) { _IgnoreAllUnknownId = b; }
00177 
00179         void clear ()
00180         {
00181                 _StringArray.clear ();
00182                 _NeedToAskStringArray.clear ();
00183                 _AskedStringArray.clear ();
00184         }
00185 
00187         void display ()
00188         {
00189                 nlinfo ("static const char *OtherSideAssociations[] = {");
00190                 for (uint i = 0; i < _StringArray.size(); i++)
00191                 {
00192                         nlinfo(" \"%s\",", _StringArray[i].c_str());
00193                 }
00194                 nlinfo ("};");
00195         }
00196 
00197 private:
00198 
00199         bool _IgnoreAllUnknownId;
00200 
00201         std::vector<std::string>        _StringArray;
00202 
00203         std::set<std::string>   _NeedToAskStringArray;
00204 
00205         std::set<std::string>   _AskedStringArray;
00206 };
00207 
00208 
00209 } // NLMISC
00210 
00211 
00212 #endif // NL_STRING_ID_ARRAY_H
00213 
00214 /* End of string_id_array.h */