Back to index

nordugrid-arc-nox  1.1.0~rc6
IniConfig.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <fstream>
00008 #include <glibmm/fileutils.h>
00009 #include <glibmm/miscutils.h>
00010 
00011 #include <arc/ArcLocation.h>
00012 #include <arc/IniConfig.h>
00013 #include <arc/Profile.h>
00014 #include <arc/StringConv.h>
00015 
00016 namespace Arc {
00017 
00018   IniConfig::IniConfig(const std::string& filename)
00019     : XMLNode(NS(), "IniConfig") {
00020     std::ifstream is(filename.c_str());
00021     std::string line;
00022     XMLNode section;
00023     while (getline(is, line)) {
00024       line = trim(line, " \t");
00025       if (line.empty() || line[0] == '#')
00026         continue;
00027       if (line[0] == '[' && line[line.size() - 1] == ']') {
00028         std::string sectionname = trim(line.substr(1, line.size() - 2), " \t");
00029         section = (sectionname == "common" && Get("common") ? Get("common") : NewChild(sectionname));
00030         continue;
00031       }
00032       std::string::size_type sep = line.find('=');
00033       if (sep == std::string::npos) {
00034         continue;
00035       }
00036       std::string attr = trim(line.substr(0, sep), " \t");
00037       std::string value = trim(line.substr(sep + 1), " \t");
00038       if (!section)
00039         section = NewChild("common");
00040       section.NewChild(attr) = value;
00041     }
00042   }
00043 
00044   IniConfig::~IniConfig() {}
00045 
00046   bool IniConfig::Evaluate(Config &cfg) {
00047     std::string profilename = (*this)["common"]["profile"];
00048     if (profilename.empty())
00049       return false;
00050     if (Glib::file_test(profilename, Glib::FILE_TEST_EXISTS) == false) {
00051       // If profilename does not contain directory separators and do not have xml suffix, then look for the profile in ARC profile directory.
00052       if (profilename.find(G_DIR_SEPARATOR_S) == std::string::npos && profilename.substr(profilename.size()-4, 4) != ".xml") {
00053         const std::string pkgprofilename = ArcLocation::Get() +
00054                                             G_DIR_SEPARATOR_S PKGDATASUBDIR
00055                                               G_DIR_SEPARATOR_S "profiles"
00056                                                 G_DIR_SEPARATOR_S + profilename + ".xml";
00057         if (Glib::file_test(pkgprofilename, Glib::FILE_TEST_EXISTS)) {
00058           profilename = pkgprofilename;
00059         }
00060       }
00061       else {
00062         std::cerr << profilename << " does not exits" << std::endl;
00063         return false;
00064       }
00065     }
00066     Profile profile(profilename);
00067     profile.Evaluate(cfg, *this);
00068     return true;
00069   }
00070 
00071 } // namespace Arc