# 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  

form_loader.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000-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 #include "stdgeorges.h"
00027 
00028 #include "nel/misc/file.h"
00029 #include "nel/misc/path.h"
00030 #include "nel/misc/i_xml.h"
00031 
00032 #include "nel/georges/u_form.h"
00033 
00034 #include "form_loader.h"
00035 #include "type.h"
00036 #include "form.h"
00037 #include "form_dfn.h"
00038 
00039 using namespace NLMISC;
00040 using namespace std;
00041 
00042 namespace NLGEORGES 
00043 {
00044 
00045 // ***************************************************************************
00046 
00047 void warning (bool exception, const char *format, ... );
00048 
00049 // ***************************************************************************
00050 // UFormLoader
00051 // ***************************************************************************
00052 
00053 UFormLoader *UFormLoader::createLoader ()
00054 {
00055         return new CFormLoader;
00056 }
00057 
00058 // ***************************************************************************
00059 
00060 void UFormLoader::releaseLoader (UFormLoader *loader)
00061 {
00062         delete loader;
00063 }
00064 
00065 // ***************************************************************************
00066 // CFormLoader
00067 // ***************************************************************************
00068 
00069 CType *CFormLoader::loadType (const char *filename)
00070 {
00071         // Lower string filename
00072         string lowerStr = strlwr ((string)filename);
00073         lowerStr = CFile::getFilename (lowerStr);
00074 
00075         // Already in the map ?
00076         TTypeMap::iterator ite = _MapType.find (lowerStr);
00077         if (ite != _MapType.end() && (ite->second != NULL) )
00078         {
00079                 // Return the pointer
00080                 return ite->second;
00081         }
00082         else
00083         {
00084                 // Create the type
00085                 CType *type = new CType;
00086 
00087                 // Load the type
00088                 try
00089                 {
00090                         // Open the file
00091                         string name = CPath::lookup (filename, false, false);
00092                         if (name.empty())
00093                                 name = filename;
00094                         CIFile file;
00095                         if (file.open (name))
00096                         {
00097                                 // Init an xml stream
00098                                 CIXml read;
00099                                 read.init (file);
00100 
00101                                 // Read the type
00102                                 type->read (read.getRootNode ());
00103                         }
00104                         else
00105                         {
00106                                 // Output error
00107                                 warning (false, "loadType", "Can't open the form file (%s).", filename);
00108 
00109                                 // Delete the type
00110                                 delete type;
00111                                 type = NULL;
00112                         }
00113                 }
00114                 catch (Exception &e)
00115                 {
00116                         // Output error
00117                         warning (false, "loadType", "Error while loading the form (%s): %s", filename, e.what());
00118 
00119                         // Delete the type
00120                         delete type;
00121                         type = NULL;
00122                 }
00123 
00124                 // Loaded ?
00125                 if (type)
00126                 {
00127                         // Insert a new entry
00128                         _MapType[lowerStr]= type;
00129                         ite = _MapType.find (lowerStr);
00130                         CType *typeType = ite->second;
00131                         int toto = 0;
00132                 }
00133                 return type;
00134         }
00135 }
00136 
00137 // ***************************************************************************
00138 
00139 CFormDfn *CFormLoader::loadFormDfn (const char *filename, bool forceLoad)
00140 {
00141         // Lower string filename
00142         string lowerStr = strlwr ((string)filename);
00143         lowerStr = CFile::getFilename (lowerStr);
00144 
00145         // Already in the map ?
00146         TFormDfnMap::iterator ite = _MapFormDfn.find (lowerStr);
00147         if (ite != _MapFormDfn.end() && ite->second)
00148         {
00149                 // Return the pointer
00150                 return ite->second;
00151         }
00152         else
00153         {
00154                 // Create the formDfn
00155                 CFormDfn *formDfn = new CFormDfn;
00156 
00157                 // Insert the form first
00158                 _MapFormDfn[lowerStr] = formDfn;
00159 
00160                 // Load the type
00161                 try
00162                 {
00163                         // Open the file
00164                         string name = CPath::lookup (filename, false, false);
00165                         if (name.empty())
00166                                 name = filename;
00167                         CIFile file;
00168                         if (file.open (name))
00169                         {
00170                                 // Init an xml stream
00171                                 CIXml read;
00172                                 read.init (file);
00173 
00174                                 // Read the type
00175                                 formDfn->read (read.getRootNode (), *this, forceLoad, filename);
00176                         }
00177                         else
00178                         {
00179                                 // Output error
00180                                 warning (false, "loadFormDfn", "Can't open the form file (%s).", filename);
00181 
00182                                 // Delete the formDfn
00183                                 delete formDfn;
00184                                 formDfn = NULL;
00185                                 _MapFormDfn.erase (lowerStr);
00186                         }
00187                 }
00188                 catch (Exception &e)
00189                 {
00190                         // Output error
00191                         warning (false, "loadFormDfn", "Error while loading the form (%s): %s", filename, e.what());
00192 
00193                         // Delete the formDfn
00194                         delete formDfn;
00195                         formDfn = NULL;
00196                         _MapFormDfn.erase (lowerStr);
00197                 }
00198 
00199                 return formDfn;
00200         }
00201 }
00202 
00203 // ***************************************************************************
00204 
00205 UForm *CFormLoader::loadForm (const char *filename)
00206 {
00207         // Lower string filename
00208         string lowerStr = strlwr ((string)filename);
00209         lowerStr = CFile::getFilename (lowerStr);
00210 
00211         // Already in the map ?
00212         TFormMap::iterator ite = _MapForm.find (lowerStr);
00213         if (ite != _MapForm.end() && ite->second)
00214         {
00215                 // Return the pointer
00216                 return (CForm*)ite->second;
00217         }
00218         else
00219         {
00220                 // Create the form
00221                 CForm *form = new CForm;
00222 
00223                 // Insert the form first
00224                 _MapForm[lowerStr] = form;
00225 
00226                 // Load the type
00227                 try
00228                 {
00229                         // Get the form DFN filename
00230                         string name = CFile::getFilename (filename);
00231                         uint index = name.rfind ('.');
00232                         if (index == string::npos)
00233                         {
00234                                 // Output error
00235                                 warning (false, "loadForm", "Form name is invalid (%s). It should have the extension of its DFN type.", name.c_str ());
00236 
00237                                 // Delete the form
00238                                 delete form;
00239                                 form = NULL;
00240                                 _MapForm.erase (lowerStr);
00241                         }
00242                         name = name.substr (index+1);
00243                         name += ".dfn";
00244 
00245                         // Load the dfn
00246                         CFormDfn        *dfn = loadFormDfn (name.c_str (), false);
00247                         if (dfn)
00248                         {
00249                                 // Open the file
00250                                 name = CPath::lookup (filename, false, false);
00251                                 if (name.empty())
00252                                         name = filename;
00253                                 CIFile file;
00254                                 if (file.open (name))
00255                                 {
00256                                         // Init an xml stream
00257                                         CIXml read;
00258                                         read.init (file);
00259 
00260                                         // Read the form
00261                                         form->read (read.getRootNode (), *this, dfn, filename);
00262                                 }
00263                                 else
00264                                 {
00265                                         // Output error
00266                                         warning (false, "loadForm", "Can't open the form file (%s).", filename);
00267 
00268                                         // Delete the form
00269                                         delete form;
00270                                         form = NULL;
00271                                         _MapForm.erase (lowerStr);
00272                                 }
00273                         }
00274                         else
00275                         {
00276                                 // Output error
00277                                 warning (false, "loadForm", "Can't open the dfn file (%s).", name.c_str ());
00278 
00279                                 // Delete the form
00280                                 delete form;
00281                                 form = NULL;
00282                                 _MapForm.erase (lowerStr);
00283                         }
00284                 }
00285                 catch (Exception &e)
00286                 {
00287                         // Output error
00288                         warning (false, "loadForm", "Error while loading the form (%s): %s", filename, e.what());
00289 
00290                         // Delete the form
00291                         delete form;
00292                         form = NULL;
00293                         _MapForm.erase (lowerStr);
00294                 }
00295 
00296                 return form;
00297         }
00298 }
00299 
00300 // ***************************************************************************
00301 
00302 UFormDfn *CFormLoader::loadFormDfn (const char *filename)
00303 {
00304         return loadFormDfn (filename, false);
00305 }
00306 
00307 // ***************************************************************************
00308 
00309 UType *CFormLoader::loadFormType (const char *filename)
00310 {
00311         return loadType (filename);
00312 }
00313 
00314 // ***************************************************************************
00315 
00316 void CFormLoader::warning (bool exception, const char *function, const char *format, ... ) const
00317 {
00318         // Make a buffer string
00319         va_list args;
00320         va_start( args, format );
00321         char buffer[1024];
00322         sint ret = vsnprintf( buffer, 1024, format, args );
00323         va_end( args );
00324 
00325         // Set the warning
00326         NLGEORGES::warning (exception, "(CFormLoader::%s) : %s", function, buffer);
00327 }
00328 
00329 // ***************************************************************************
00330 
00331 } // NLGEORGES