# 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  

sheet_id.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2002 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 /* This class is case unsensitive. It means that you can call build() and
00027  * buildIdVector() with string with anycase, it'll work.
00028  */
00029 
00030 #include "stdmisc.h"
00031 
00032 #include "nel/misc/file.h"
00033 #include "nel/misc/path.h"
00034 
00035 #include "nel/misc/sheet_id.h"
00036 #include "nel/misc/common.h"
00037 
00038 using namespace std;
00039 
00040 namespace NLMISC {
00041 
00042 map<uint32,std::string> CSheetId::_SheetIdToName;
00043 map<std::string,uint32> CSheetId::_SheetNameToId;
00044 vector<std::string> CSheetId::_FileExtensions;
00045 bool CSheetId::_Initialised=false;
00046 bool CSheetId::_RemoveUnknownSheet=true;
00047 
00048 const CSheetId CSheetId::Unknown(0);
00049 
00050 void CSheetId::cbFileChange (const std::string &filename)
00051 {
00052         nlinfo ("%s changed, reload it", filename.c_str());
00053 
00054         loadSheetId();
00055 }
00056 
00057 //-----------------------------------------------
00058 //      CSheetId
00059 //
00060 //-----------------------------------------------
00061 CSheetId::CSheetId( const string& sheetName )
00062 {
00063         if (!build(sheetName))
00064         {
00065                 nlwarning("<CSheetId::CSheetId> The sheet %s is not in sheet_id.bin, setting it to Unknown",sheetName.c_str());
00066                 *this = Unknown;
00067         }
00068 
00069 } // CSheetId //
00070 
00071 
00072 //-----------------------------------------------
00073 //      Build
00074 //
00075 //-----------------------------------------------
00076 bool CSheetId::build(const std::string& sheetName)
00077 {
00078         nlassert(_Initialised);
00079 
00080         map<string,uint32>::const_iterator itId = _SheetNameToId.find( strlwr(sheetName) );
00081         if( itId != _SheetNameToId.end() )
00082         {
00083                 _Id.Id = (*itId).second;
00084                 return true;
00085         }
00086         else
00087         {
00088                 return false;           
00089         }
00090 }
00091 
00092 void CSheetId::loadSheetId ()
00093 {
00094         // Open the sheet id to sheet file name association
00095         CIFile file;
00096         if(file.open(CPath::lookup("sheet_id.bin", false, false)))
00097         {
00098                 // clear entries
00099                 _FileExtensions.clear ();
00100                 _SheetIdToName.clear ();
00101                 _SheetNameToId.clear ();
00102 
00103                 // reserve space for the vector of file extensions
00104                 _FileExtensions.resize(256);
00105 
00106                 // Get the map.
00107                 file.serialCont(_SheetIdToName);
00108 
00109                 // Close the file.
00110                 file.close();
00111 
00112                 if (_RemoveUnknownSheet)
00113                 {
00114                         uint32 removednbfiles = 0;
00115                         uint32 nbfiles = _SheetIdToName.size();
00116 
00117                         // now we remove all files that not available
00118                         map<uint32,string>::iterator itStr2;
00119                         for( itStr2 = _SheetIdToName.begin(); itStr2 != _SheetIdToName.end(); )
00120                         {
00121                                 if (CPath::exists ((*itStr2).second))
00122                                 {
00123                                         ++itStr2;
00124                                 }
00125                                 else
00126                                 {
00127                                         map<uint32,string>::iterator olditStr = itStr2;
00128                                         //nldebug ("Removing file '%s' from CSheetId because the file not exists", (*olditStr).second.c_str ());
00129                                         itStr2++;
00130                                         _SheetIdToName.erase (olditStr);
00131                                         removednbfiles++;
00132                                 }
00133                         }
00134 
00135                         nlinfo ("Removed %d files on %d from CSheetId because these files doesn't exists", removednbfiles, nbfiles);
00136                 }
00137 
00138                 // build the invert map & file extension vector
00139                 map<uint32,string>::iterator itStr;
00140                 for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00141                 {
00142                         // add entry to the inverse map
00143                         _SheetNameToId.insert( make_pair(strlwr((*itStr).second),(*itStr).first) );
00144 
00145                         // work out the type value for this entry in the map
00146                         TSheetId sheetId;
00147                         sheetId.Id=(*itStr).first;
00148                         uint8 type=     sheetId.IdInfos.Type;
00149  
00150                         // check whether we need to add an entry to the file extensions vector
00151                         if (_FileExtensions[type].empty())
00152                         {
00153                                 // find the file extension part of the given file name
00154                                 _FileExtensions[type]=strlwr(CFile::getExtension((*itStr).second));
00155                         }
00156                 }
00157         }
00158         else
00159         {
00160                 nlerror("<CSheetId::init> Can't open the file sheet_id.bin");
00161         }
00162 }
00163 
00164 //-----------------------------------------------
00165 //      init
00166 //
00167 //-----------------------------------------------
00168 void CSheetId::init(bool removeUnknownSheet)
00169 {
00170         // allow multiple calls to init in case libraries depending on sheetid call this init from their own
00171         if (_Initialised)
00172                 return;
00173 
00174         CFile::addFileChangeCallback ("sheet_id.bin", cbFileChange);
00175 
00176         _RemoveUnknownSheet = removeUnknownSheet;
00177 
00178         loadSheetId ();
00179 
00180         _Initialised=true;
00181 } // init //
00182 
00183 
00184 
00185 
00186 //-----------------------------------------------
00187 //      operator=
00188 //
00189 //-----------------------------------------------
00190 CSheetId& CSheetId::operator=( const CSheetId& sheetId )
00191 {
00192         nlassert(_Initialised);
00193 
00194         if(this == &sheetId)
00195         {
00196                 return *this;
00197         }
00198 
00199         _Id.Id = sheetId.asInt();
00200 
00201     return *this;
00202 
00203 
00204 } // operator= //
00205 
00206 
00207 //-----------------------------------------------
00208 //      operator=
00209 //
00210 //-----------------------------------------------
00211 CSheetId& CSheetId::operator=( const string& sheetName )
00212 {
00213         nlassert(_Initialised);
00214 
00215         map<string,uint32>::const_iterator itId = _SheetNameToId.find( strlwr(sheetName) );
00216         if( itId != _SheetNameToId.end() )
00217         {
00218                 _Id.Id = (*itId).second;
00219         }
00220         else
00221         {
00222                 nlwarning("<CSheetId::operator=> The sheet %s is not in sheet_id.bin, setting it to Unknown",sheetName.c_str());
00223                 *this = Unknown;
00224         }
00225         
00226         return *this;
00227 
00228 } // operator= //
00229 
00230 
00231 //-----------------------------------------------
00232 //      operator=
00233 //
00234 //-----------------------------------------------
00235 CSheetId& CSheetId::operator=( uint32 sheetRef )
00236 {
00237         nlassert(_Initialised);
00238 
00239         _Id.Id = sheetRef;
00240         
00241         return *this;
00242 
00243 } // operator= //
00244 
00245 
00246 
00247 //-----------------------------------------------
00248 //      operator<
00249 //
00250 //-----------------------------------------------
00251 bool CSheetId::operator < (const CSheetId& sheetRef ) const
00252 {
00253         nlassert(_Initialised);
00254 
00255         if (_Id.Id < sheetRef.asInt())
00256         {
00257                 return true;
00258         }
00259                         
00260         return false;
00261 
00262 } // operator< //
00263 
00264 
00265 
00266 //-----------------------------------------------
00267 //      toString
00268 //
00269 //-----------------------------------------------
00270 string CSheetId::toString() const
00271 {
00272         nlassert(_Initialised);
00273 
00274         map<uint32,string>::const_iterator itStr = _SheetIdToName.find( _Id.Id );
00275         if( itStr != _SheetIdToName.end() )
00276         {
00277                 return (*itStr).second;
00278         }
00279         else
00280         {
00281                 // This nlwarning is commented out because the loggers are mutexed, therefore
00282                 // you couldn't use toString() within a nlwarning().
00283                 //nlwarning("<CSheetId::toString> The sheet %08x is not in sheet_id.bin",_Id.Id);
00284                 return NLMISC::toString( "<Sheet %d not found in sheet_id.bin>", _Id.Id );
00285         }
00286 
00287 } // toString //
00288 
00289 
00290 
00291 //-----------------------------------------------
00292 //      display
00293 //
00294 //-----------------------------------------------
00295 void CSheetId::display()
00296 {
00297         nlassert(_Initialised);
00298 
00299         map<uint32,string>::const_iterator itStr;
00300         for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00301         {
00302                 //nlinfo("%d %s",(*itStr).first,(*itStr).second.c_str());
00303                 nlinfo("(%08x %d) %s",(*itStr).first,(*itStr).first,(*itStr).second.c_str());
00304         }
00305 
00306 } // display //
00307 
00308 
00309 
00310 //-----------------------------------------------
00311 //      display
00312 //
00313 //-----------------------------------------------
00314 void CSheetId::display(uint8 type)
00315 {
00316         nlassert(_Initialised);
00317 
00318         map<uint32,string>::const_iterator itStr;
00319         for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00320         {
00321                 // work out the type value for this entry in the map
00322                 TSheetId sheetId;
00323                 sheetId.Id=(*itStr).first;
00324  
00325                 // decide whether or not to dsiplay the entry
00326                 if (type==sheetId.IdInfos.Type)
00327                 {
00328                         //nlinfo("%d %s",(*itStr).first,(*itStr).second.c_str());
00329                         nlinfo("(%08x %d) %s",(*itStr).first,(*itStr).first,(*itStr).second.c_str());
00330                 }
00331         }
00332 
00333 } // display //
00334 
00335 
00336 
00337 //-----------------------------------------------
00338 //      buildIdVector
00339 //
00340 //-----------------------------------------------
00341 void CSheetId::buildIdVector(std::vector <CSheetId> &result)
00342 {
00343         nlassert(_Initialised);
00344 
00345         map<uint32,string>::const_iterator itStr;
00346         for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00347         {
00348                 result.push_back( (CSheetId)(*itStr).first );
00349         }
00350 
00351 } // buildIdVector //
00352 
00353 
00354 //-----------------------------------------------
00355 //      buildIdVector
00356 //
00357 //-----------------------------------------------
00358 void CSheetId::buildIdVector(std::vector <CSheetId> &result,uint8 type)
00359 {
00360         nlassert(_Initialised);
00361 
00362         map<uint32,string>::const_iterator itStr;
00363         for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00364         {
00365                 // work out the type value for this entry in the map
00366                 TSheetId sheetId;
00367                 sheetId.Id=(*itStr).first;
00368  
00369                 // decide whether or not to use the entry
00370                 if (type==sheetId.IdInfos.Type)
00371                 {
00372                         result.push_back( (CSheetId)sheetId.Id );
00373                 }
00374         }
00375 
00376 } // buildIdVector //
00377 
00378 //-----------------------------------------------
00379 //      buildIdVector
00380 //
00381 //-----------------------------------------------
00382 void CSheetId::buildIdVector(std::vector <CSheetId> &result, std::vector <std::string> &resultFilenames,uint8 type)
00383 {
00384         nlassert(_Initialised);
00385 
00386         map<uint32,string>::const_iterator itStr;
00387         for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00388         {
00389                 // work out the type value for this entry in the map
00390                 TSheetId sheetId;
00391                 sheetId.Id=(*itStr).first;
00392  
00393                 // decide whether or not to use the entry
00394                 if (type==sheetId.IdInfos.Type)
00395                 {
00396                         result.push_back( (CSheetId)sheetId.Id );
00397                         resultFilenames.push_back( (*itStr).second );
00398                 }
00399         }
00400 
00401 } // buildIdVector //
00402 
00403 //-----------------------------------------------
00404 //      buildIdVector
00405 //
00406 //-----------------------------------------------
00407 void CSheetId::buildIdVector(std::vector <CSheetId> &result,const std::string &fileExtension)
00408 {
00409         uint32 type=typeFromFileExtension(fileExtension);
00410         if (type!=(uint32)~0)
00411                 buildIdVector(result,(uint8)type);
00412 
00413 } // buildIdVector //
00414 
00415 //-----------------------------------------------
00416 //      buildIdVector
00417 //
00418 //-----------------------------------------------
00419 void CSheetId::buildIdVector(std::vector <CSheetId> &result, std::vector <std::string> &resultFilenames,const std::string &fileExtension)
00420 {
00421         uint32 type=typeFromFileExtension(fileExtension);
00422         if (type!=(uint32)~0)
00423                 buildIdVector(result,resultFilenames, (uint8)type);
00424 
00425 } // buildIdVector //
00426 
00427 
00428 //-----------------------------------------------
00429 //      typeFromFileExtension
00430 //
00431 //-----------------------------------------------
00432 uint32 CSheetId::typeFromFileExtension(const std::string &fileExtension)
00433 {
00434         nlassert(_Initialised);
00435 
00436         unsigned i;
00437         for (i=0;i<_FileExtensions.size();i++)
00438                 if (strlwr(fileExtension)==_FileExtensions[i])
00439                         return i;
00440 
00441         return ~0;
00442 } // typeFromFileExtension //
00443 
00444 
00445 //-----------------------------------------------
00446 //      fileExtensionFromType
00447 //
00448 //-----------------------------------------------
00449 const std::string &CSheetId::fileExtensionFromType(uint8 type)
00450 {
00451         nlassert(_Initialised);
00452         nlassert(type<256);
00453 
00454         return _FileExtensions[type];
00455 
00456 } // fileExtensionFromType //
00457 
00458 } // NLMISC