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 #include "creaSystem.h"
00042
00043 #if defined(WIN32)
00044 # include <direct.h>
00045 # include <windows.h>
00046 #endif
00047
00048
00049 namespace bbtk
00050 {
00051
00053 ConfigurationFile::ConfigurationFile()
00054 {
00055 mFile_separator = VALID_FILE_SEPARATOR;
00056
00057
00058 mBin_path = Utilities::GetExecutablePath();
00059
00060
00061
00062
00063
00064
00065
00066
00067 #ifdef MACOSX
00068 std::string macPath("Contents/MacOS");
00069 int sbp = mBin_path.length();
00070 int smp = macPath.length();
00071 if (mBin_path.compare( sbp-smp, smp, macPath )==0 )
00072 {
00073 mBin_path = mBin_path + "/../../..";
00074 }
00075 #endif
00076
00078 mInstall_path = mBin_path + "/..";
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 InitializeDotBbtkStructure();
00090
00091
00092 mBbs_rel_path = BBTK_STRINGIFY_SYMBOL(BBTK_BBS_REL_PATH);
00093
00094 mBbs_path = mInstall_path + "/" + mBbs_rel_path;
00095
00096 mData_path = mInstall_path + "/" + BBTK_STRINGIFY_SYMBOL(BBTK_DATA_REL_PATH);
00097
00098 Utilities::replace( mBin_path,
00099 INVALID_FILE_SEPARATOR,
00100 VALID_FILE_SEPARATOR);
00101 Utilities::replace( mInstall_path,
00102 INVALID_FILE_SEPARATOR,
00103 VALID_FILE_SEPARATOR);
00104 Utilities::replace( mBbs_path,
00105 INVALID_FILE_SEPARATOR,
00106 VALID_FILE_SEPARATOR);
00107 Utilities::replace( mData_path,
00108 INVALID_FILE_SEPARATOR,
00109 VALID_FILE_SEPARATOR);
00110
00111 bbtkMessage("config",1," ==> bin : '"<<mBin_path<<"'"<<std::endl);
00112 bbtkMessage("config",1," ==> prefix : '"<<mInstall_path<<"'"<<std::endl);
00113 bbtkMessage("config",1," ==> doc : '"<<mDoc_path<<"'"<<std::endl);
00114 bbtkMessage("config",1," ==> bbs : '"<<mBbs_path<<"'"<<std::endl);
00115 bbtkMessage("config",1," ==> data : '"<<mData_path<<"'"<<std::endl);
00116
00117
00118
00119 mBbs_paths.push_back( "." );
00120
00121 mBbs_paths.push_back(mBbs_path);
00122
00123
00124 std::string toolsappli_rel_path("/");
00125
00126 toolsappli_rel_path += "toolsbbtk/appli";
00127
00128
00129
00130
00131
00132
00133 int iStrVec,sizeStrVec;
00134
00135 sizeStrVec = mBbs_paths.size();
00136 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
00137 Utilities::replace( mBbs_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00138 }
00139
00140
00141
00142 mPackage_paths.push_back(".");
00143
00144 mPackage_paths.push_back(mBin_path);
00145
00146
00147
00148
00149
00150
00152
00153 mPackage_paths.push_back(mInstall_path + "/lib/creatools");
00154 mPackage_paths.push_back(mInstall_path + "/lib64/creatools");
00155 #ifdef WIN32
00156
00157
00158 mPackage_paths.push_back(mBin_path + "/Debug");
00159
00160 mPackage_paths.push_back(mBin_path + "/Release");
00161 #endif
00162
00163 sizeStrVec = mPackage_paths.size();
00164 for (iStrVec=0;iStrVec<sizeStrVec;iStrVec++){
00165 Utilities::replace( mPackage_paths[iStrVec] , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00166 }
00167
00168 GetHelp(2);
00169
00170
00171 char buf[2048];
00172 const char *currentDir = getcwd(buf, 2048);
00173
00174 if( !currentDir )
00175 {
00176 std::cerr << "Path was too long to fit on 2048 bytes ?!?" << std::endl;
00177
00178
00179
00180 }
00181
00182
00183 std::string configXmlFullPathName = currentDir ;
00184 configXmlFullPathName += "/bbtk_config.xml";
00185 Utilities::replace( configXmlFullPathName , INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
00186
00187 if ( Utilities::FileExists( configXmlFullPathName ))
00188 {
00189 bbtkMessage("config",1, "ConfigurationFile : [" << configXmlFullPathName <<
00190 "] found in current directory" << std::endl);
00191
00192
00193 }
00194
00195
00196 else
00197 {
00198 configXmlFullPathName = Utilities::MakeUserSettingsFullFileName("bbtk_config.xml");
00199 if (!Utilities::FileExists( configXmlFullPathName ))
00200 {
00201
00202 InstallPath ();
00203 }
00204 }
00205
00206
00207 Read(configXmlFullPathName.c_str());
00208 }
00209
00210
00211
00213 ConfigurationFile::~ConfigurationFile()
00214 {
00215 }
00216
00217
00218
00219 void ConfigurationFile::InitializeDotBbtkStructure()
00220 {
00221 mDot_bbtk_path = Utilities::GetUserSettingsDir();
00222 mDot_bbtk_is_new = false;
00223 if (!Utilities::FileExists(mDot_bbtk_path)) mDot_bbtk_is_new = true;
00224 Utilities::CreateDirectoryIfNeeded(mDot_bbtk_path);
00225
00226 mDoc_path = Utilities::MakeUserSettingsFullFileName("doc");
00227 Utilities::CreateDirectoryIfNeeded(mDoc_path);
00228
00229 std::string bbdoc_path =
00230 Utilities::MakeUserSettingsFullFileName("doc/bbdoc");
00231 Utilities::CreateDirectoryIfNeeded(bbdoc_path);
00232
00233
00234 std::string filename = Utilities::MakeUserSettingsFullFileName("doc/help_contents.html");
00235 if (!Utilities::FileExists(filename))
00236 {
00237 bbtkDebugMessage("config",1,
00238 "* Creating [" << filename << "]" << std::endl);
00239
00240 std::string doc_path = mInstall_path + "/"
00241 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + "/";
00242 Utilities::MakeValidFileName(doc_path);
00243
00244 std::ofstream f;
00245 f.open(filename.c_str(), std::ios::out );
00246 f << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
00247 << "HTML 4.01 Transitional//EN\">"
00248 << "<html><head><title>Help Contents - bbtk "
00249 << GetVersion() << "</title>"
00250 << "<meta http-equiv=\"Content-Type\" content=\"text/html; "
00251 << "charset=iso-8859-1\"></head><H1>Help Contents</H1>"
00252 << "<a href=\"bbdoc/index-category.html#demo\" "
00253 << "target=\"information\">Demos</a><br>"
00254 << "<a href=\"bbdoc/index-category.html#example\" "
00255 << "target=\"information\">Examples</a>"
00256 << "<H2>Guides</H2>"
00257 << "<a href=\""
00258 << doc_path
00259 << "bbtkUsersGuide/bbtkUsersGuide.pdf"
00260 << "\" target=\"information\">User's Guide</a><br>"
00261 << "<a href=\""
00262 << doc_path
00263 << "bbtkDevelopersGuide/bbtkDevelopersGuide.pdf"
00264 << "\" target=\"information\">Developer's Guide</a><br>"
00265 << "<a href=\""
00266 << doc_path
00267 << "bbtkPackageDevelopersGuide/bbtkPackageDevelopersGuide.pdf"
00268 << "\" target=\"information\">Package Developer's Guide</a><br>"
00269 << "<a href=\""
00270 << doc_path
00271 << "doxygen/bbtk/main.html"
00272 << "\" target=\"information\">bbtk library doxygen doc</a><br>"
00273 << "<H2>Boxes</H2>"
00274 << "<a target=\"information\" href=\"bbdoc/index-alpha.html\">"
00275 << "Alphabetical list</a><br>"
00276 << "<a target=\"information\" href=\"bbdoc/index-package.html\">"
00277 << "List by package</a><br>"
00278 << "<a target=\"information\" href=\"bbdoc/index-category.html\">"
00279 << "List by category</a><br>"
00280 << "<a target=\"information\" href=\"bbdoc/index-adaptors.html\">"
00281 << "List of adaptors</a><br>"
00282 << "</body>"
00283 << "</html>";
00284 }
00285
00286 mTemp_path = Utilities::MakeUserSettingsFullFileName("tmp");
00287 Utilities::CreateDirectoryIfNeeded(mTemp_path);
00288
00289 }
00290
00291
00292
00293 void ConfigurationFile::CreateConfigXML( char *rootDirectory )
00294 {
00295 FILE *fp;
00296 char configXml[250];
00297 sprintf (configXml , "%s/bbtk_config.xml", rootDirectory);
00298 bbtkDebugMessage("config",1, "in CreateConfigXML[" << configXml << "]" << std::endl);
00299 fp = fopen (configXml, "w");
00300 fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
00301 fprintf(fp, "<config>\n");
00302 fprintf(fp, " <bbs_path> </bbs_path>\n");
00303 fprintf(fp, " <package_path> </package_path>\n");
00304 fprintf(fp, "</config>\n");
00305 fclose(fp);
00306 }
00307
00308
00309
00310
00311
00312
00313 void ConfigurationFile::InstallPath ()
00314 {
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 #if defined(__GNUC__)
00335
00336
00337
00338 char bbtk_path[1000];
00339 strcpy(bbtk_path, Utilities::GetExecutablePath().c_str());
00340
00341
00342
00343 char rootDirectory[200];
00344 sprintf( rootDirectory, "%s/.bbtk", getenv("HOME"));
00345
00346
00347 char configPath[200];
00348 sprintf(configPath, "%s/bbtk_config.xml",rootDirectory);
00349
00350
00351 char configXmlTmp[250];
00352 sprintf(configXmlTmp, "%s/bbtk_config.xml.tmp", bbtk_path);
00353
00354
00355 char copyFile[250];
00356
00357 if (!Utilities::FileExists(configXmlTmp))
00358 {
00359
00360 CreateConfigXML( rootDirectory );
00361 }
00362 else
00363 {
00364 sprintf(copyFile,"cp %s %s/bbtk_config.xml ",configXmlTmp,rootDirectory );
00365 if (!Utilities::FileExists(configPath))
00366 {
00367 system(copyFile);
00368 }
00369 }
00370 return;
00371
00372
00373 #elif defined(WIN32)
00374
00375
00376
00377 char bbtk_path[100];
00378 strcpy(bbtk_path, "\"c:\\Program Files\\BBTK\\bin\"");
00379 char bbtk_path2[100];
00380 strcpy(bbtk_path2, "c:\\Program Files\\BBTK\\bin");
00381
00382
00383 char rootDirectory[200];
00384 sprintf(rootDirectory, "%s\\.bbtk",getenv("USERPROFILE"));
00385
00386
00387
00388 char configPath[200];
00389 sprintf(configPath, "%s\\bbtk_config.xml",rootDirectory);
00390
00391
00392 char makeDir[250];
00393 sprintf( makeDir, "mkdir \"%s\" ", rootDirectory);
00394
00395
00396 char configXmlTmp[250];
00397 sprintf(configXmlTmp, "%s\\bbtk_config.xml.tmp", bbtk_path2);
00398
00399
00400 char copyFile[250];
00401
00402 if (!Utilities::FileExists(configXmlTmp))
00403 {
00404
00405 CreateConfigXML( rootDirectory );
00406 return;
00407 }
00408
00409 sprintf(copyFile,"copy %s\\bbtk_config.xml.tmp \"%s\"\\bbtk_config.xml ",bbtk_path,rootDirectory );
00410
00411 int attribs = GetFileAttributes (rootDirectory);
00412 bbtkMessage("config",1,std::hex << attribs << " " << FILE_ATTRIBUTE_DIRECTORY << std::endl);
00413 if ( attribs != 0xFFFFFFFF)
00414 {
00415 if ((attribs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY )
00416 {
00417 if ( GetFileAttributes( configPath ) == 0xFFFFFFFF)
00418 {
00419 system(copyFile);
00420 }
00421 }
00422 }
00423 else
00424 {
00425 system(makeDir);
00426 system(copyFile);
00427 }
00428 return;
00429
00430 #else
00432
00433 return;
00434 #endif
00435
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445 void ConfigurationFile::Read(const std::string& filename)
00446 {
00447
00448 bbtkDebugMessage("config",1,"ConfigurationFile::Read(" <<filename << ")" << std::endl);
00449
00450 mConfig_xml_full_path = filename;
00451 XMLResults* res = new XMLResults;
00452 XMLNode BB = XMLNode::parseFile((XMLCSTR)filename.c_str(),(XMLCSTR)"config",res);
00453
00454 if ( res->error != eXMLErrorNone )
00455 {
00456 std::string mess = GetErrorMessage(res,filename);
00457 delete res;
00458 bbtkDebugMessage("config",1,mess<< std::endl);
00459 bbtkError(mess);
00460 }
00461 delete res;
00462
00463 bbtkDebugMessage("config",1,"OK" << std::endl);
00464
00465 int i,j;
00466
00467
00468 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"description"); i++)
00469 {
00470 std::string val;
00471 GetTextOrClear(BB.getChildNode((XMLCSTR)"description",&j),val);
00472 mDescription += val;
00473 }
00474
00475
00476 if( BB.nChildNode((XMLCSTR)"url") )
00477 GetTextOrClear(BB.getChildNode((XMLCSTR)"url"),mUrl);
00478
00479
00480 if( BB.nChildNode((XMLCSTR)"data_path") )
00481 GetTextOrClear(BB.getChildNode((XMLCSTR)"data_path"),mData_path);
00482
00483
00484
00485
00486
00487
00488 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"bbs_path"); i++)
00489 {
00490 std::string val;
00491 GetTextOrClear(BB.getChildNode((XMLCSTR)"bbs_path",&j),val);
00492 mBbs_paths.push_back(val);
00493 }
00494
00495
00496
00497
00498 for (i=0,j=0; i<BB.nChildNode((XMLCSTR)"package_path"); i++)
00499 {
00500 std::string val;
00501 GetTextOrClear(BB.getChildNode((XMLCSTR)"package_path",&j),val);
00502 mPackage_paths.push_back(val);
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517 GetHelp(2);
00518 }
00519
00520
00521
00522 bool ConfigurationFile::AddPackagePathsAndWrite( const std::string& path )
00523 {
00524 bbtkDebugMessageInc("config",9,
00525 "ConfigurationFile::AddPackagePathsAndWrite("
00526 <<path<<")"<<std::endl);
00527
00528 XMLResults* res = new XMLResults;
00529 XMLNode BB =
00530 XMLNode::parseFile((XMLCSTR)Get_config_xml_full_path().c_str(),
00531 (XMLCSTR)"config",res);
00532
00533 if ( res->error != eXMLErrorNone )
00534 {
00535 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
00536 delete res;
00537 bbtkDebugMessage("config",1,mess<< std::endl);
00538 bbtkError(mess);
00539 }
00540 delete res;
00541
00542 #ifdef _WIN32
00543 std::string bbs_path = path + "/bbs";
00544 #else
00545 std::string bbs_path = path + "/share/bbtk/bbs" ;
00546 #endif
00547 XMLNode BBSPATH = BB.addChild((XMLCSTR)"bbs_path");
00548 BBSPATH.addText((XMLCSTR)bbs_path.c_str());
00549 Utilities::replace(bbs_path, INVALID_FILE_SEPARATOR, VALID_FILE_SEPARATOR);
00550 mBbs_paths.push_back(bbs_path);
00551
00552 #ifdef _WIN32
00553 std::string pack_path = path + "/bin";
00554 #else
00555 std::string pack_path = path ;
00556 #endif
00557 XMLNode PACKPATH = BB.addChild((XMLCSTR)"package_path");
00558 PACKPATH.addText((XMLCSTR)pack_path.c_str());
00559 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
00560 mPackage_paths.push_back(pack_path);
00561
00562 #ifdef _WIN32
00563 pack_path = path + "/RelWithDebInfo";
00564 PACKPATH = BB.addChild((XMLCSTR)"package_path");
00565 PACKPATH.addText((XMLCSTR)pack_path.c_str());
00566 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
00567 mPackage_paths.push_back(pack_path);
00568 pack_path = path + "/Debug";
00569 PACKPATH = BB.addChild((XMLCSTR)"package_path");
00570 PACKPATH.addText((XMLCSTR)pack_path.c_str());
00571 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
00572 mPackage_paths.push_back(pack_path);
00573 pack_path = path + "/Release";
00574 PACKPATH = BB.addChild((XMLCSTR)"package_path");
00575 PACKPATH.addText((XMLCSTR)pack_path.c_str());
00576 Utilities::replace(pack_path,INVALID_FILE_SEPARATOR,VALID_FILE_SEPARATOR);
00577 mPackage_paths.push_back(pack_path);
00578 #endif
00579
00580
00581 XMLError err = BB.writeToFile((XMLCSTR)Get_config_xml_full_path().c_str());
00582 if ( err != eXMLErrorNone )
00583 {
00584 std::string mess = GetErrorMessage(res,Get_config_xml_full_path());
00585 bbtkDebugMessage("config",1,mess<< std::endl);
00586 bbtkError(mess);
00587 }
00588
00589 return true;
00590 }
00591
00592
00593
00594 void ConfigurationFile::GetHelp(int level) const
00595 {
00596 bbtkDebugMessageInc("config",9,"ConfigurationFile::GetHelp("<<level
00597 <<")"<<std::endl);
00598
00599 const std::string config_xml_full_path = Get_config_xml_full_path();
00600 const std::string description = Get_description();
00601 const std::string url = Get_doc_path();
00602 const std::string data_path = Get_data_path();
00603 const std::string default_temp_dir = Get_default_temp_dir();
00604 const std::string file_separator = Get_file_separator();
00605 const std::vector<std::string>bbs_paths = Get_bbs_paths();
00606 const std::vector<std::string>package_paths = Get_package_paths();
00607
00608 bbtkMessage("help",level, "=============" << std::endl);
00609 bbtkMessage("help",level, "Configuration" << std::endl);
00610 bbtkMessage("help",level, "=============" << std::endl);
00611 bbtkMessage("help",level, "bbtk_config.xml : [" << config_xml_full_path << "]" << std::endl);
00612 bbtkMessage("help",level, "Documentation Path : [" << url << "]" << std::endl);
00613 bbtkMessage("help",level, "Data Path : [" << data_path << "]" << std::endl);
00614 bbtkMessage("help",level, "Temp Directory : [" << default_temp_dir << "]" << std::endl);
00615 bbtkMessage("help",level, "File Separator : [" << file_separator << "]" << std::endl);
00616
00617 std::vector<std::string>::const_iterator i;
00618
00619 bbtkMessage("help",level, "BBS Paths " << std::endl);
00620 for (i = bbs_paths.begin(); i!=bbs_paths.end(); ++i )
00621 {
00622 bbtkMessage("help",level,"--- ["<<*i<<"]"<<std::endl);
00623 }
00624
00625 bbtkMessage("help",level, "PACKAGE Paths : " << std::endl);
00626 for (i = package_paths.begin(); i!=package_paths.end(); ++i )
00627 {
00628 bbtkMessage("help",level,"--- ["<<*i<<"]"<<std::endl);
00629 }
00630
00631 bbtkDebugDecTab("config",9);
00632 }
00633
00634
00635
00636 }