00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
00067
00068
00069 CType *CFormLoader::loadType (const char *filename)
00070 {
00071
00072 string lowerStr = strlwr ((string)filename);
00073 lowerStr = CFile::getFilename (lowerStr);
00074
00075
00076 TTypeMap::iterator ite = _MapType.find (lowerStr);
00077 if (ite != _MapType.end() && (ite->second != NULL) )
00078 {
00079
00080 return ite->second;
00081 }
00082 else
00083 {
00084
00085 CType *type = new CType;
00086
00087
00088 try
00089 {
00090
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
00098 CIXml read;
00099 read.init (file);
00100
00101
00102 type->read (read.getRootNode ());
00103 }
00104 else
00105 {
00106
00107 warning (false, "loadType", "Can't open the form file (%s).", filename);
00108
00109
00110 delete type;
00111 type = NULL;
00112 }
00113 }
00114 catch (Exception &e)
00115 {
00116
00117 warning (false, "loadType", "Error while loading the form (%s): %s", filename, e.what());
00118
00119
00120 delete type;
00121 type = NULL;
00122 }
00123
00124
00125 if (type)
00126 {
00127
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
00142 string lowerStr = strlwr ((string)filename);
00143 lowerStr = CFile::getFilename (lowerStr);
00144
00145
00146 TFormDfnMap::iterator ite = _MapFormDfn.find (lowerStr);
00147 if (ite != _MapFormDfn.end() && ite->second)
00148 {
00149
00150 return ite->second;
00151 }
00152 else
00153 {
00154
00155 CFormDfn *formDfn = new CFormDfn;
00156
00157
00158 _MapFormDfn[lowerStr] = formDfn;
00159
00160
00161 try
00162 {
00163
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
00171 CIXml read;
00172 read.init (file);
00173
00174
00175 formDfn->read (read.getRootNode (), *this, forceLoad, filename);
00176 }
00177 else
00178 {
00179
00180 warning (false, "loadFormDfn", "Can't open the form file (%s).", filename);
00181
00182
00183 delete formDfn;
00184 formDfn = NULL;
00185 _MapFormDfn.erase (lowerStr);
00186 }
00187 }
00188 catch (Exception &e)
00189 {
00190
00191 warning (false, "loadFormDfn", "Error while loading the form (%s): %s", filename, e.what());
00192
00193
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
00208 string lowerStr = strlwr ((string)filename);
00209 lowerStr = CFile::getFilename (lowerStr);
00210
00211
00212 TFormMap::iterator ite = _MapForm.find (lowerStr);
00213 if (ite != _MapForm.end() && ite->second)
00214 {
00215
00216 return (CForm*)ite->second;
00217 }
00218 else
00219 {
00220
00221 CForm *form = new CForm;
00222
00223
00224 _MapForm[lowerStr] = form;
00225
00226
00227 try
00228 {
00229
00230 string name = CFile::getFilename (filename);
00231 uint index = name.rfind ('.');
00232 if (index == string::npos)
00233 {
00234
00235 warning (false, "loadForm", "Form name is invalid (%s). It should have the extension of its DFN type.", name.c_str ());
00236
00237
00238 delete form;
00239 form = NULL;
00240 _MapForm.erase (lowerStr);
00241 }
00242 name = name.substr (index+1);
00243 name += ".dfn";
00244
00245
00246 CFormDfn *dfn = loadFormDfn (name.c_str (), false);
00247 if (dfn)
00248 {
00249
00250 name = CPath::lookup (filename, false, false);
00251 if (name.empty())
00252 name = filename;
00253 CIFile file;
00254 if (file.open (name))
00255 {
00256
00257 CIXml read;
00258 read.init (file);
00259
00260
00261 form->read (read.getRootNode (), *this, dfn, filename);
00262 }
00263 else
00264 {
00265
00266 warning (false, "loadForm", "Can't open the form file (%s).", filename);
00267
00268
00269 delete form;
00270 form = NULL;
00271 _MapForm.erase (lowerStr);
00272 }
00273 }
00274 else
00275 {
00276
00277 warning (false, "loadForm", "Can't open the dfn file (%s).", name.c_str ());
00278
00279
00280 delete form;
00281 form = NULL;
00282 _MapForm.erase (lowerStr);
00283 }
00284 }
00285 catch (Exception &e)
00286 {
00287
00288 warning (false, "loadForm", "Error while loading the form (%s): %s", filename, e.what());
00289
00290
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
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
00326 NLGEORGES::warning (exception, "(CFormLoader::%s) : %s", function, buffer);
00327 }
00328
00329
00330
00331 }