00001 #ifndef PATH_CONFIGURATION_IMPLEMENTATION_FILE
00002 #define PATH_CONFIGURATION_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00061
00062 istring installation_root()
00063 {
00064 istring to_return = read_item(LOCAL_FOLDER_NAME());
00065 if (!to_return) {
00066
00067
00068 to_return = filename(path_configuration_file()).dirname();
00069 }
00070 return to_return;
00071 }
00072
00073 istring get_logging_directory()
00074 {
00075
00076 istring def_log = installation_root();
00077
00078 def_log += "/logs";
00079
00080
00081
00082 istring log_dir = read_item(LOGGING_FOLDER_NAME());
00083
00084 if (!log_dir) {
00085
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
00092
00093 log_dir = parser_bits::substitute_env_vars(log_dir);
00094 }
00095
00096
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
00101
00102
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
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
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
00153
00154 to_return = parser_bits::substitute_env_vars(to_return);
00155 }
00156 return to_return;
00157 }
00158
00159 }
00160
00161
00162 #endif //PATH_CONFIGURATION_IMPLEMENTATION_FILE
00163