checking in the recent efforts at optimizing clam
[feisty_meow.git] / nucleus / library / configuration / application_configuration.cpp
index 16a1bea33d42dfd8a4c7ce2bf184d15efbb45388..89670bcefe86655bbcf47cf52b7872b825b90eb6 100644 (file)
 #include <basis/guards.h>
 #include <basis/mutex.h>
 #include <basis/utf_conversion.h>
+#include <filesystem/directory.h>
 #include <filesystem/filename.h>
 #include <mathematics/chaos.h>
 #include <structures/static_memory_gremlin.h>
 #include <textual/parser_bits.h>
+#include <system_helper.h>
 
 #ifdef __APPLE__
   #include <mach-o/dyld.h>
   #include <limits.h>
 #endif
-#ifdef __WIN32__
-  #include <direct.h>
-  #include <process.h>
-#else
+//#ifdef _MSC_VER
+//  #include <direct.h>
+//  #include <process.h>
+//#else
   #include <dirent.h>
-#endif
-#ifdef __UNIX__
   #include <sys/utsname.h>
   #include <unistd.h>
-#endif
+//#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -57,7 +57,7 @@ namespace configuration {
 const int MAXIMUM_COMMAND_LINE = 32 * KILOBYTE;
   // maximum command line that we'll deal with here.
 
-#ifdef __UNIX__
+#if defined(__UNIX__) || defined(__GNU_WINDOWS__)
 astring application_configuration::get_cmdline_from_proc()
 {
   FUNCDEF("get_cmdline_from_proc");
@@ -91,7 +91,7 @@ astring application_configuration::get_cmdline_from_proc()
   // command line are all separated by zero characters.
   __check_once_app_path = filebuff;
   delete [] filebuff;
-printf("got an app name before chewing: %s\n", __check_once_app_path.s());
+//printf("got an app name before chewing: %s\n", __check_once_app_path.s());
   // clean out quote characters from the name.
   for (int i = __check_once_app_path.length() - 1; i >= 0; i--) {
     if (__check_once_app_path[i] == '"') __check_once_app_path.zap(i, i);
@@ -101,11 +101,11 @@ printf("got an app name before chewing: %s\n", __check_once_app_path.s());
   filename testing(__check_once_app_path);
   if (testing.had_directory()) return __check_once_app_path;  // all set.
 
-//hmmm: the below might be better off as a find app in path method, which relies on which.
-printf("no dir part found, app name after chewing: %s\n", __check_once_app_path.s());
+//printf("no dir part found, app name after chewing: %s\n", __check_once_app_path.s());
 
+//hmmm: the below might be better off as a find app in path method, which relies on which.
   // there was no directory component, so we'll try to guess one.
-  astring temp_filename(environment::get("TMP")
+  astring temp_filename(environment::TMP()
       + a_sprintf("/zz_cmdfind.%d", chaos().inclusive(0, 999999999)));
   system((astring("which ") + __check_once_app_path + " >" + temp_filename).s());
   FILE *which_file = fopen(temp_filename.s(), "r");
@@ -136,7 +136,7 @@ printf("no dir part found, app name after chewing: %s\n", __check_once_app_path.
 // deprecated; better to use the /proc/pid/cmdline file.
 astring application_configuration::query_for_process_info()
 {
-//  FUNCDEF("query_for_process_info");
+  FUNCDEF("query_for_process_info");
   astring to_return = "unknown";
   // we ask the operating system about our process identifier and store
   // the results in a temporary file.
@@ -191,27 +191,29 @@ astring application_configuration::query_for_process_info()
     fclose(output); \
     unlink(tmpfile.s()); \
   } \
-  astring home_dir = env_string("HOME"); \
+  astring home_dir = environment::get("HOME"); \
   to_return = home_dir + "/failed_to_determine.exe"; \
 }
 
 astring application_configuration::application_name()
 {
-//  FUNCDEF("application_name");
+  FUNCDEF("application_name");
   astring to_return;
 #ifdef __APPLE__
   char buffer[MAX_ABS_PATH] = { '\0' };
   uint32_t buffsize = MAX_ABS_PATH - 1;
   _NSGetExecutablePath(buffer, &buffsize);
   to_return = (char *)buffer;
-#elif __UNIX__
+#elif defined(__UNIX__) || defined(__GNU_WINDOWS__)
   to_return = get_cmdline_from_proc();
-#elif defined(__WIN32__)
+/*
+#elif defined(_MSC_VER)
   flexichar low_buff[MAX_ABS_PATH + 1];
-  GetModuleFileName(NIL, low_buff, MAX_ABS_PATH - 1);
+  GetModuleFileName(NULL_POINTER, low_buff, MAX_ABS_PATH - 1);
   astring buff = from_unicode_temp(low_buff);
   buff.to_lower();  // we lower-case the name since windows seems to UC it.
   to_return = buff;
+*/
 #else
   #pragma error("hmmm: no means of finding app name is implemented.")
   SET_BOGUS_NAME("not_implemented_for_this_OS");
@@ -219,7 +221,8 @@ astring application_configuration::application_name()
   return to_return;
 }
 
-#if defined(__UNIX__) || defined(__WIN32__)
+#if defined(__UNIX__) || defined(__GNU_WINDOWS__)
+//defined(_MSC_VER) || 
   basis::un_int application_configuration::process_id() { return getpid(); }
 #else
   #pragma error("hmmm: need process id implementation for this OS!")
@@ -233,10 +236,10 @@ astring application_configuration::current_directory()
   char buff[MAX_ABS_PATH];
   getcwd(buff, MAX_ABS_PATH - 1);
   to_return = buff;
-#elif defined(__WIN32__)
-  flexichar low_buff[MAX_ABS_PATH + 1];
-  GetCurrentDirectory(MAX_ABS_PATH, low_buff);
-  to_return = from_unicode_temp(low_buff);
+//#elif defined(_MSC_VER)
+//  flexichar low_buff[MAX_ABS_PATH + 1];
+//  GetCurrentDirectory(MAX_ABS_PATH, low_buff);
+//  to_return = from_unicode_temp(low_buff);
 #else
   #pragma error("hmmm: need support for current directory on this OS.")
   to_return = ".";
@@ -264,13 +267,13 @@ structures::version application_configuration::get_OS_version()
   utsname kernel_parms;
   uname(&kernel_parms);
   to_return = version(kernel_parms.release);
-#elif defined(__WIN32__)
-  OSVERSIONINFO info;
-  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-  ::GetVersionEx(&info);
-  to_return = version(a_sprintf("%u.%u.%u.%u", basis::un_short(info.dwMajorVersion),
-      basis::un_short(info.dwMinorVersion), basis::un_short(info.dwPlatformId),
-      basis::un_short(info.dwBuildNumber)));
+//#elif defined(_MSC_VER)
+//  OSVERSIONINFO info;
+//  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+//  ::GetVersionEx(&info);
+//  to_return = version(a_sprintf("%u.%u.%u.%u", basis::un_short(info.dwMajorVersion),
+//      basis::un_short(info.dwMinorVersion), basis::un_short(info.dwPlatformId),
+//      basis::un_short(info.dwBuildNumber)));
 #else
   #pragma error("hmmm: need version info for this OS!")
 #endif
@@ -291,15 +294,57 @@ const astring &application_configuration::GLOBAL_SECTION_NAME() { STATIC_STRING(
 
 const astring &application_configuration::LOGGING_FOLDER_NAME() { STATIC_STRING("LogPath"); }
 
+//const astring &application_configuration::WINDOZE_VIRTUAL_ROOT_NAME()
+//{ STATIC_STRING("VirtualUnixRoot"); }
+
+const astring &application_configuration::DEFAULT_VIRTUAL_UNIX_ROOT()
+{ STATIC_STRING("c:/cygwin"); }
+
 //////////////
 
-const int MAX_LOG_PATH = 200;
+// static storage for virtual unix root, if it's used.
+// we don't expect it to change during runtime, right?  that would be fubar.
+// so we cache it once we retrieve it.
+SAFE_STATIC(astring, static_root_holder, )
+
+const astring &application_configuration::virtual_unix_root()
+{
+  // see if we already cached the root.  it shouldn't change during runtime.
+  if (static_root_holder().length()) {
+    return static_root_holder();
+  }
+#ifdef __UNIX__
+  // simple implementation for unix/linux; just tell the truth about the real root.
+  static_root_holder() = astring("/");
+  return static_root_holder();
+#endif
+#ifdef __WIN32__
+  /*
+   use the path in our system helpers header, which should have been set during the
+   build process if this is really windows.
+  */
+  astring virtual_root = FEISTY_MEOW_VIRTUAL_UNIX_ROOT;
+  if (!virtual_root) {
+    // if it has no length, we didn't get our setting!  we'll limp along with a guess.
+    return DEFAULT_VIRTUAL_UNIX_ROOT();
+  } else {
+    static_root_holder() = virtual_root;
+    return static_root_holder();
+  }
+
+#endif
+}
+
+//////////////
+
+////const int MAX_LOG_PATH = 512;
   // the maximum length of the entry stored for the log path.
 
 astring application_configuration::get_logging_directory()
 {
-  // start with the root of our installation.
-  astring def_log = application_directory();
+  // new scheme is to just use the temporary directory, which can vary per user
+  // and which hopefully is always set to something usable.
+  astring def_log = environment::TMP();
   // add logs directory underneath that.
   def_log += "/logs";
     // add the subdirectory for logs.
@@ -309,6 +354,7 @@ astring application_configuration::get_logging_directory()
     // get the entry for the logging path.
   if (!log_dir) {
     // if the entry was absent, we set it.
+//printf("did not find log dir in config file\n");
     ini_configurator ini(application_configuration_file(),
         ini_configurator::RETURN_ONLY,
         ini_configurator::APPLICATION_DIRECTORY);
@@ -317,25 +363,25 @@ astring application_configuration::get_logging_directory()
     // they gave us something.  let's replace the environment variables
     // in their string so we resolve paths and such.
     log_dir = parser_bits::substitute_env_vars(log_dir);
+//printf("%s", (char *)a_sprintf("got log dir with %s value\n", log_dir.s()).s());
   }
 
   // now we make sure the directory exists.
-  struct stat to_fill;
-  int stat_ret = stat(log_dir.observe(), &to_fill);
-  if (stat_ret || !(to_fill.st_mode & S_IFDIR) ) {
-    // if it's not anything yet or if it's not a directory, then we need
-    // to create it.
-//if it's something besides a directory... should it be deleted?
+  filename testing(log_dir);
+  if (!testing.exists()) {
+    bool okay = directory::recursive_create(log_dir);
+    if (!okay) {
+      LOG(astring("failed to create logging directory: ") + log_dir);
+      // return a directory almost guaranteed to exist; best we can do in this case.
 #ifdef __UNIX__
-    int mk_ret = mkdir(log_dir.s(), 0777);
+      return "/tmp";
 #endif
 #ifdef __WIN32__
-    int mk_ret = mkdir(log_dir.s());
+      return "c:/";
 #endif
-    if (mk_ret) return "";
-//can't have a log file if we can't make the directory successfully???
+    }
   }
-
+    
   return log_dir;
 }