00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 #include "bbtkConfigurationFile.h"
00037 #include "bbtkMessageManager.h"
00038 #include "bbtkXML.h"
00039 #include "bbtkUtilities.h"
00040
00041 #if defined(WIN32)
00042 #include <direct.h>
00043 #endif
00044
00045
00046
00047 namespace bbtk
00048 {
00049
00050
00052 ConfigurationFile::ConfigurationFile()
00053 {
00054
00055 mFile_separator = VALID_FILE_SEPARATOR;
00056
00057
00058 mBin_path = GetExecutablePath();
00059
00060 mInstall_path = mBin_path + "/..";
00061
00062 mDoc_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH);
00063
00064
00065 mDoc_path = mInstall_path + "/" + mDoc_rel_path;
00066
00067 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
00068
00069
00070 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
00071
00072
00073
00074
00075
00076
00077
00078
00079 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
00080
00081 Utilities::replace( mBin_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00082 Utilities::replace( mInstall_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00083 Utilities::replace( mDoc_rel_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00084 Utilities::replace( mDoc_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00085 Utilities::replace( mBbs_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00086 Utilities::replace( mData_path , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00087
00088 bbtkMessage("Config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
00089 bbtkMessage("Config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
00090 bbtkMessage("Config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
00091 bbtkMessage("Config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
00092 bbtkMessage("Config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
00093
00094
00095
00096
00097
00098
00099 mBbs_paths.push_back( "." );
00100
00101 mBbs_paths.push_back(mBbs_path);
00102
00103
00104 std::string toolsappli_rel_path("/");
00105
00106 toolsappli_rel_path += "toolsbbtk/appli";
00107
00108
00109
00110
00111
00112
00113 int iStrVec,sizeStrVec;
00114
00115 sizeStrVec = mBbs_paths.size();
00116 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
00117 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00118 }
00119
00120
00121
00122 mPackage_paths.push_back(".");
00123
00124 mPackage_paths.push_back(mBin_path);
00125
00126
00127 mPackage_paths.push_back(mInstall_path + "/lib");
00128 #ifdef WIN32
00129
00130
00131 mPackage_paths.push_back(mBin_path + "/Debug");
00132
00133 mPackage_paths.push_back(mBin_path + "/Release");
00134 #endif
00135
00136 sizeStrVec = mPackage_paths.size();
00137 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
00138 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00139 }
00140
00141
00142
00143 GetHelp(2);
00144
00145
00146 char buf[2048];
00147 const char *currentDir = getcwd(buf, 2048);
00148
00149 if( !currentDir )
00150 {
00151 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
00152
00153
00154
00155 }
00156
00157
00158 std::string configXmlFullPathName = currentDir ;
00159 configXmlFullPathName += "/bbtk_config.xml";
00160 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00161
00162 if ( Utilities::FileExists( configXmlFullPathName ))
00163 {
00164 bbtkMessage("Config",1, "ConfigurationFile : [" << configXmlFullPathName <<
00165 "] found in current directory" << std::endl);
00166
00167
00168 }
00169
00170
00171 else
00172 {
00173 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
00174 if (!Utilities::FileExists( configXmlFullPathName ))
00175 {
00176
00177 InstallPath ();
00178 }
00179 }
00180
00181
00182 Read(configXmlFullPathName.c_str());
00183 }
00184
00185
00186
00188 ConfigurationFile::~ConfigurationFile()
00189 {
00190 }
00191
00192
00193
00194
00195
00196
00197 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
00198 {
00199 FILE *fp;
00200 char configXml[250];
00201 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
00202 bbtkDebugMessage("Config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
00203 fp = fopen (configXml, "w");
00204 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
00205 fprintf(fp, "<config>\n");
00206 fprintf(fp, " <bbs_path> </bbs_path>\n");
00207 fprintf(fp, " <package_path> </package_path>\n");
00208 fprintf(fp, "</config>\n");
00209 fclose(fp);
00210 }
00211
00212
00213
00214
00215 std::string ConfigurationFile::GetExecutablePath()
00216 {
00218 char *buf = (char *)malloc(512);
00219 char *slash;
00220
00221 #if defined(WIN32)
00222 GetModuleFileName(NULL, buf, 511);
00223 slash = strrchr(buf, '\\');
00224 if (slash)
00225 {
00226 *slash = 0;
00227 }
00228 #elif defined(__GNUC__)
00229 int res;
00230 res = readlink("/proc/self/exe", buf, 512);
00231 if (res == -1)
00232 return "";
00233 buf[res] = 0;
00234 slash = strrchr(buf, '/');
00235 if (slash)
00236 {
00237 *slash = 0;
00238 }
00239 #else
00240 return "";
00241 #endif
00242 std::string ret(buf);
00243 free(buf);
00244 return ret;
00245 }
00246
00247
00248
00249
00250 void ConfigurationFile::InstallPath ()
00251 {
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 #if defined(__GNUC__)
00272
00273
00274
00275 char bbtk_path[100];
00276 strcpy(bbtk_path, "/usr/local/bin");
00277
00278
00279 char rootDirectory[200];
00280 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
00281
00282
00283 char configPath[200];
00284 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
00285
00286
00287 char makeDir[250];
00288 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
00289
00290
00291 char configXmlTmp[250];
00292 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
00293
00294
00295 char copyFile[250];
00296
00297 if (!Utilities::FileExists(configXmlTmp))
00298 {
00299 if (!Utilities::FileExists(rootDirectory))
00300 {
00301 system(makeDir);
00302 }
00303
00304
00305 CreateConfigXML( rootDirectory );
00306 }
00307 else
00308 {
00309 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
00310 if (!Utilities::FileExists(rootDirectory))
00311 {
00312
00313 system(makeDir);
00314 }
00315
00316 if (!Utilities::FileExists(configPath))
00317 {
00318 system(copyFile);
00319 }
00320 }
00321 return;
00322
00323
00324 #elif defined(WIN32)
00325
00326
00327
00328 char bbtk_path[100];
00329 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
00330 char bbtk_path2[100];
00331 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
00332
00333
00334 char rootDirectory[200];
00335 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
00336
00337
00338
00339 char configPath[200];
00340 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
00341
00342
00343 char makeDir[250];
00344 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
00345
00346
00347 char configXmlTmp[250];
00348 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
00349
00350
00351 char copyFile[250];
00352
00353 if (!Utilities::FileExists(configXmlTmp))
00354 {
00355 if (!Utilities::FileExists(rootDirectory))
00356 {
00357 system(makeDir);
00358 }
00359
00360
00361 CreateConfigXML( rootDirectory );
00362 return;
00363 }
00364
00365 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
00366
00367 int attribs = GetFileAttributes (rootDirectory);
00368 bbtkMessage("Config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
00369 if ( attribs != 0xFFFFFFFF)
00370 {
00371 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY )
00372 {
00373 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
00374 {
00375 system(copyFile);
00376 }
00377 }
00378 }
00379 else
00380 {
00381 system(makeDir);
00382 system(copyFile);
00383 }
00384 return;
00385
00386 #else
00388
00389 return;
00390 #endif
00391
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401 void ConfigurationFile::Read(const std::string& filename)
00402 {
00403
00404 bbtkDebugMessage("Config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
00405
00406 mConfig_xml_full_path = filename;
00407 XMLResults* res = new XMLResults;
00408 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
00409
00410 if ( res->error != eXMLErrorNone )
00411 {
00412 std::string mess = GetErrorMessage(res,filename);
00413 delete res;
00414 bbtkDebugMessage("Config",1,mess<< std::endl);
00415 bbtkError(mess);
00416 }
00417 delete res;
00418
00419 bbtkDebugMessage("Config",1,"OK" << std::endl);
00420
00421 int i,j;
00422
00423
00424 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
00425 {
00426 std::string val;
00427 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
00428 mDescription += val;
00429 }
00430
00431
00432 if( BB.nChildNode((XMLCSTR)"url") )
00433 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
00434
00435
00436 if( BB.nChildNode((XMLCSTR)"data_path") )
00437 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
00438
00439
00440
00441
00442
00443
00444 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
00445 {
00446 std::string val;
00447 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
00448 mBbs_paths.push_back(val);
00449 }
00450
00451
00452
00453
00454 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
00455 {
00456 std::string val;
00457 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
00458 mPackage_paths.push_back(val);
00459 }
00460
00461
00462 if( BB.nChildNode((XMLCSTR)"default_temp_dir") )
00463 GetTextOrClear(BB.getChildNode((XMLCSTR)"default_temp_dir"),mDefault_temp_dir);
00464
00465 if ( mDefault_temp_dir == "$")
00466 {
00467 size_t pos = mConfig_xml_full_path.find("bbtk_config.xml");
00468 mDefault_temp_dir = mConfig_xml_full_path.substr (0,pos);
00469 }
00470
00471 GetHelp(2);
00472 }
00473
00474
00475
00476 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
00477 {
00478 bbtkDebugMessageInc("Config",9,
00479 "ConfigurationFile::AddPackagePathsAndWrite("
00480 <<path<<")"<<std::endl);
00481
00482 XMLResults* res = new XMLResults;
00483 XMLNode BB =
00484 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
00485 (XMLCSTR)"config",res);
00486
00487 if ( res->error != eXMLErrorNone )
00488 {
00489 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
00490 delete res;
00491 bbtkDebugMessage("Config",1,mess<< std::endl);
00492 bbtkError(mess);
00493 }
00494 delete res;
00495
00496 #ifdef _WIN32
00497 std::string bbs_path = path + "/bbs";
00498 #else
00499 std::string bbs_path = path + "/share/bbtk/bbs" ;
00500 #endif
00501 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
00502 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
00503 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
00504 mBbs_paths.push_back(bbs_path);
00505
00506 #ifdef _WIN32
00507 std::string pack_path = path + "/bin";
00508 #else
00509 std::string pack_path = path ;
00510 #endif
00511 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
00512 PACKPATH.addText((XMLCSTR)pack_path.c_str());
00513 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
00514 mPackage_paths.push_back(pack_path);
00515
00516 #ifdef _WIN32
00517 pack_path = path + "/Debug";
00518 PACKPATH = BB.addChild((XMLCSTR)"package_path");
00519 PACKPATH.addText((XMLCSTR)pack_path.c_str());
00520 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
00521 mPackage_paths.push_back(pack_path);
00522 pack_path = path + "/Release";
00523 PACKPATH = BB.addChild((XMLCSTR)"package_path");
00524 PACKPATH.addText((XMLCSTR)pack_path.c_str());
00525 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
00526 mPackage_paths.push_back(pack_path);
00527 #endif
00528
00529
00530 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
00531 if ( err != eXMLErrorNone )
00532 {
00533 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
00534 bbtkDebugMessage("Config",1,mess<< std::endl);
00535 bbtkError(mess);
00536 }
00537
00538 return true;
00539 }
00540
00541
00542
00543 void ConfigurationFile::GetHelp(int level) const
00544 {
00545 bbtkDebugMessageInc("Config",9,"ConfigurationFile::GetHelp("<<level
00546 <<")"<<std::endl);
00547
00548 const std::string config_xml_full_path = Get_config_xml_full_path();
00549 const std::string description = Get_description();
00550 const std::string url = Get_doc_path();
00551 const std::string data_path = Get_data_path();
00552 const std::string default_temp_dir = Get_default_temp_dir();
00553 const std::string file_separator = Get_file_separator();
00554 const std::vector<std::string>bbs_paths = Get_bbs_paths();
00555 const std::vector<std::string>package_paths = Get_package_paths();
00556
00557 bbtkMessage("Help",level, "=============" << std::endl);
00558 bbtkMessage("Help",level, "Configuration" << std::endl);
00559 bbtkMessage("Help",level, "=============" << std::endl);
00560 bbtkMessage("Help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
00561 bbtkMessage("Help",level, "Documentation Path : [" << url << "]" << std::endl);
00562 bbtkMessage("Help",level, "Data Path : [" << data_path << "]" << std::endl);
00563 bbtkMessage("Help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
00564 bbtkMessage("Help",level, "File Separator : [" << file_separator << "]" << std::endl);
00565
00566 std::vector<std::string>::const_iterator i;
00567
00568 bbtkMessage("Help",level, "BBS Paths " << std::endl);
00569 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
00570 {
00571 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
00572 }
00573
00574 bbtkMessage("Help",level, "PACKAGE Paths : " << std::endl);
00575 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
00576 {
00577 bbtkMessage("Help",level,"--- ["<<*i<<"]"<<std::endl);
00578 }
00579
00580 bbtkDebugDecTab("Config",9);
00581 }
00582
00583
00584
00585 }