path_configuration.cpp

Go to the documentation of this file.
00001 #ifndef PATH_CONFIGURATION_IMPLEMENTATION_FILE
00002 #define PATH_CONFIGURATION_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : path_configuration                                                *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "command_line.h"
00019 #include "filename.h"
00020 #include "ini_config.h"
00021 #include "path_configuration.h"
00022 
00023 #include <basis/function.h>
00024 #include <basis/guards.h>
00025 #include <basis/istring.h>
00026 #include <basis/mutex.h>
00027 #include <basis/object_base.h>
00028 #include <basis/portable.h>
00029 #include <data_struct/static_memory_gremlin.h>
00030 #include <textual/parser_bits.h>
00031 
00032 #ifdef __WIN32__
00033   #include <direct.h>
00034 #else
00035   #include <dirent.h>
00036 #endif
00037 #include <string.h>
00038 #include <sys/stat.h>
00039 
00040 namespace path_configuration {
00041 
00042 const char *PATH_CONFIGURATION_FILENAME() { return "paths.ini"; }
00043 
00044 istring path_configuration_file()
00045 {
00046   filename cfg_file(application_directory() + "/"
00047       + PATH_CONFIGURATION_FILENAME());
00048   return cfg_file.raw();
00049 }
00050 
00051 const istring &GLOBAL_SECTION_NAME() { STATIC_STRING("Common"); }
00052 
00053 const istring &LOCAL_FOLDER_NAME() { STATIC_STRING("LocalFiles"); }
00054 
00055 const istring &LOGGING_FOLDER_NAME() { STATIC_STRING("LogPath"); }
00056 
00058 
00059 const int MAX_LOG_PATH = 200;
00060   // the maximum length of the entry stored for the log path.
00061 
00062 istring installation_root()
00063 {
00064   istring to_return = read_item(LOCAL_FOLDER_NAME());
00065   if (!to_return) {
00066     // well, if this other guy has a path, we'll give that back.  otherwise,
00067     // we don't know what the path should be at all.
00068     to_return = filename(path_configuration_file()).dirname();
00069   }
00070   return to_return;
00071 }
00072 
00073 istring get_logging_directory()
00074 {
00075   // start with the root of our installation.
00076   istring def_log = installation_root();
00077   // add logs directory underneath that.
00078   def_log += "/logs";
00079     // add the subdirectory for logs.
00080 
00081   // now grab the current value for the name, if any.
00082   istring log_dir = read_item(LOGGING_FOLDER_NAME());
00083     // get the entry for the logging path.
00084   if (!log_dir) {
00085     // if the entry was absent, we set it.
00086     ini_configurator ini(path_configuration_file(),
00087         ini_configurator::RETURN_ONLY,
00088         ini_configurator::APPLICATION_DIRECTORY);
00089     ini.store(GLOBAL_SECTION_NAME(), LOGGING_FOLDER_NAME(), def_log);
00090   } else {
00091     // they gave us something.  let's replace the environment variables
00092     // in their string so we resolve paths and such.
00093     log_dir = parser_bits::substitute_env_vars(log_dir);
00094   }
00095 
00096   // now we make sure the directory exists.
00097   struct stat to_fill;
00098   int stat_ret = stat(log_dir.observe(), &to_fill);
00099   if (stat_ret || !(to_fill.st_mode & S_IFDIR) ) {
00100     // if it's not anything yet or if it's not a directory, then we need
00101     // to create it.
00102 //if it's something besides a directory... should it be deleted?
00103 #ifdef __UNIX__
00104     int mk_ret = mkdir(log_dir.s(), 0777);
00105 #endif
00106 #ifdef __WIN32__
00107     int mk_ret = mkdir(log_dir.s());
00108 #endif
00109     if (mk_ret) return "";
00110 //can't have a log file if we can't make the directory successfully???
00111   }
00112 
00113   return log_dir;
00114 }
00115 
00116 istring make_logfile_name(const istring &base_name)
00117 { return get_logging_directory() + "/" + base_name; }
00118 
00119 SAFE_STATIC(istring_object, _hidden_hardcoding_appname, )
00120 
00121 void hard_code_application_name(const istring &new_name)
00122 { _hidden_hardcoding_appname() = new_name; }
00123 
00124 istring application_name()
00125 {
00126   if (_hidden_hardcoding_appname().t()) return _hidden_hardcoding_appname();
00127   return portable::application_name();
00128 }
00129 
00130 SAFE_STATIC(istring_object, _hidden_hardcoding_dir, )
00131 
00132 void hard_code_application_dir(const istring &new_dir)
00133 { _hidden_hardcoding_dir() = new_dir; }
00134 
00135 istring application_directory()
00136 {
00137   // first check for the so-called hard-coded directory.
00138   if (_hidden_hardcoding_dir().t())
00139     return _hidden_hardcoding_dir();
00140   return filename(application_name()).dirname().raw();
00141 }
00142 
00143 istring core_bin_directory() { return read_item("core_bin"); }
00144 
00145 istring read_item(const istring &key_name)
00146 {
00147   filename ini_name = path_configuration_file();
00148   ini_configurator ini(ini_name, ini_configurator::RETURN_ONLY,
00149       ini_configurator::APPLICATION_DIRECTORY);
00150   istring to_return =  ini.load(GLOBAL_SECTION_NAME(), key_name, "");
00151   if (!!to_return) {
00152     // if the string has any length, then we process any environment
00153     // variables found encoded in the value.
00154     to_return = parser_bits::substitute_env_vars(to_return);
00155   }
00156   return to_return;
00157 }
00158 
00159 } // namespace.
00160 
00161 
00162 #endif //PATH_CONFIGURATION_IMPLEMENTATION_FILE
00163 

Generated on Fri Nov 28 04:29:29 2008 for HOOPLE Libraries by  doxygen 1.5.1