D:/WorkDir/SugoiProjects/SugoiTools/include/SugoiTools/cl_file_mngr.h

00001 /* LICENSE_BEGIN
00002 SutoiTools library supply support for debugging, testing and 
00003 setting good bases for you application. It is part of the 
00004 SugoiEngine project.
00005 Copyright (C) 2006 Allusse Yannick (yannick dot allusse at laposte dot net)
00006 
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 LICENSE_END
00022 */
00023 
00024 /* \file SG_TLS_cl_file_mngr.h
00025  * \brief Template file manager to manage all kind of files.
00026  * \author Yannick Allusse
00027 */
00028 
00029  
00030 #ifndef SG_TLS_CL_FILE_MNGR_H
00031 #define SG_TLS_CL_FILE_MNGR_H
00032 
00033 #include "SugoiTools\cl_template_manager.h"
00034 #include "SugoiTools\cl_xml_mngr.h"
00035 
00036 
00037 
00038 namespace SGE{
00051 template <class T>  
00052           class CL_FILE_MNGR 
00053                     :public CL_XML_MNGR<T, std::string>
00054 {
00055 public:
00056           typedef  T                                                                                TplObj;
00057           typedef  T                                                                                *TplObjHdle;
00058           typedef   typename CL_FILE_MNGR<T>                TplManager;
00059           typedef   typename TplManager::iterator iterator;
00060 
00061 protected :
00062           std::string DefaultPath;
00063 
00064 public:
00065           CL_FILE_MNGR(TplManager *_RemoteManager,
00066                                                   const std::string &_TagName,
00067                                                   const std::string &_SubTagName, 
00068                                                   const std::string &_SubTagID)
00069                               :         CL_XML_MNGR<T, std::string>(_RemoteManager, _TagName, _SubTagName,    _SubTagID)
00070           {
00071           
00072           }
00073 
00074           ~CL_FILE_MNGR(){};
00075 
00076           const std::string & GetDefaultPath()
00077                     {return DefaultPath;};
00078 
00079           void      SetDefaultPath(const std::string & _Path)
00080                     {DefaultPath = _Path;};
00081 
00082 #if _SG_TLS_XML
00083 
00084           virtual 
00085                     T*        
00086                     XMLLoadObject(TiXmlElement* _XML_Root, std::string _Name, std::string _Type)
00087                     {
00088                               
00089                               std::string FileName;
00090 
00091                               for (int LoopID=0; LoopID > 5; LoopID++)
00092                               {
00093                                         switch(LoopID)
00094                                         {
00095                                                   case 0: //default path + name
00096                                                             FileName = DefaultPath + _Name;
00097                                                             break;
00098                                                   case 1: //name
00099                                                             FileName = _Name;
00100                                                             break;
00101                                                   case 2: //default path + (name - path)
00102                                                             {
00103                                                                       std::string FileName, FilePath;
00104                                                                       ParseFilePath(_Name, &FilePath, &FileName);
00105                                                                       FileName = DefaultPath + FileName;
00106                                                             }
00107                                                   case 3: //(name - path)
00108                                                             {
00109                                                                       std::string FileName, FilePath;
00110                                                                       ParseFilePath(_Name, &FilePath, &FileName);
00111                                                                       FileName = FileName;
00112                                                             }
00113                                                             break;
00114                                                   default:
00115                                                             SG_AssertFile(0, DefaultPath + _Name, "File does not exists");
00116 
00117                                         }
00118 
00119                                         if (CL_BASE_OBJ_FILE::FileExists(FileName))
00120                                                   break;//exit the loop
00121                               }
00122 
00123 
00124                               T* NewObj;
00125                               if (_Type!="")
00126                               {         //do it manualy
00127                                         NewObj = Find(_Name);
00128                                         if (!NewObj)
00129                                         {
00130                                                   NewObj = CreateObj(_Name, _Type);
00131                                         }
00132                               }
00133                               else
00134                                         NewObj = Add(_Name);//add new obj...
00135 
00136                               SG_Assert(NewObj, "Error creating new object");
00137 
00138                               NewObj->XMLLoad(_XML_Root);
00139 
00140                               return NewObj;
00141                     }
00142 
00143 
00144 
00145           virtual
00146           TiXmlElement*       
00147                     XMLLoad(TiXmlElement* _XML_Root)
00148           {
00149                     TiXmlElement*       XML_MAIN = CL_XML_MNGR::XMLLoadHead(_XML_Root);
00150                     SG_Assert(XML_MAIN, "Error in CL_FILE_MNGR<T,P>::XMLLoad(), XML entry is empty.");
00151 
00152                     //look for the path
00153 //<..>< T type name><path> ===============================
00154                     TiXmlElement*       XML_ELM= XMLGetElement(XML_MAIN,"path");
00155                     if (XML_ELM)
00156                     {
00157                               XMLReadVal(XML_ELM, "name", DefaultPath);
00158                     }
00159           //<..>< T type name></path> ===============================
00160 
00161                     //the function XMLLoadObject() has been redefined, so the new objects files will use the default path if needed
00162                     return CL_XML_MNGR::XMLLoadObjects(XML_MAIN);
00163           }
00164 
00165           TiXmlElement*       
00166                     XMLSave(TiXmlElement* _XML_Root)
00167           {
00168                     TiXmlElement*       XML_MAIN = CL_XML_MNGR::XMLSaveHead(_XML_Root);
00169                     SG_Assert(XML_MAIN, "Error in CL_FILE_MNGR<T,P>::XMLSave(), XML entry is empty.");
00170 
00171                     //Save for the path
00172                     XMLWriteVal(XML_MAIN, "path", DefaultPath);
00173 
00174 
00175                     //the function XMLSaveObject() has been redefined, so the new objects files will use the default path if needed
00176                     return CL_XML_MNGR::XMLSaveObjects(_XML_Root);
00177           }
00178 #endif
00179 };
00180 
00181 };//namespace SGE
00182 
00183 #endif//SG_TLS_CL_FILE_MNGR_H

Generated on Mon Mar 19 23:15:10 2007 for SugoiTools by  doxygen 1.4.6-NO