Back to index

nordugrid-arc-nox  1.1.0~rc6
ArcLocation.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 <unistd.h>
00008 #include <arc/Logger.h>
00009 #include <arc/Utils.h>
00010 
00011 #ifdef ENABLE_NLS
00012 #include <libintl.h>
00013 #endif
00014 
00015 #include <glibmm/miscutils.h>
00016 
00017 #include "ArcLocation.h"
00018 
00019 namespace Arc {
00020 
00021   std::string& ArcLocation::location(void) {
00022     static std::string* location_ = new std::string;
00023     return *location_;
00024   }
00025 
00026   void ArcLocation::Init(std::string path) {
00027     location().clear();
00028     location() = GetEnv("ARC_LOCATION");
00029     if (location().empty() && !path.empty()) {
00030       if (path.rfind(G_DIR_SEPARATOR_S) == std::string::npos)
00031         path = Glib::find_program_in_path(path);
00032       if (path.substr(0, 2) == std::string(".") + G_DIR_SEPARATOR_S) {
00033         char cwd[PATH_MAX];
00034         if (getcwd(cwd, PATH_MAX))
00035           path.replace(0, 1, cwd);
00036       }
00037       std::string::size_type pos = path.rfind(G_DIR_SEPARATOR_S);
00038       if (pos != std::string::npos && pos > 0) {
00039         pos = path.rfind(G_DIR_SEPARATOR_S, pos - 1);
00040         if (pos != std::string::npos)
00041           location() = path.substr(0, pos);
00042       }
00043     }
00044     if (location().empty()) {
00045       Logger::getRootLogger().msg(WARNING,
00046                                   "Can not determine the install location. "
00047                                   "Using %s. Please set ARC_LOCATION "
00048                                   "if this is not correct.", INSTPREFIX);
00049       location() = INSTPREFIX;
00050     }
00051 #ifdef ENABLE_NLS
00052     bindtextdomain(PACKAGE, (location() + G_DIR_SEPARATOR_S + "share" + G_DIR_SEPARATOR_S "locale").c_str());
00053 #endif
00054   }
00055 
00056   const std::string& ArcLocation::Get() {
00057     if (location().empty())
00058       Init("");
00059     return location();
00060   }
00061 
00062 
00063   std::list<std::string> ArcLocation::GetPlugins() {
00064     std::list<std::string> plist;
00065     std::string arcpluginpath = GetEnv("ARC_PLUGIN_PATH");
00066     if (!arcpluginpath.empty()) {
00067       std::string::size_type pos = 0;
00068       while (pos != std::string::npos) {
00069         std::string::size_type pos2 = arcpluginpath.find(G_SEARCHPATH_SEPARATOR, pos);
00070         plist.push_back(pos2 == std::string::npos ?
00071                         arcpluginpath.substr(pos) :
00072                         arcpluginpath.substr(pos, pos2 - pos));
00073         pos = pos2;
00074         if (pos != std::string::npos)
00075           pos++;
00076       }
00077     }
00078     else
00079       plist.push_back(Get() + G_DIR_SEPARATOR_S + PKGLIBSUBDIR);
00080     return plist;
00081   }
00082 
00083 } // namespace Arc