changeset 154:e8f67e344aaf

Get correct directory for saving configuration under Windows.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 03 Nov 2010 00:03:20 +0200
parents f1684884fd56
children 0720ca51673e
files nnchat.c
diffstat 1 files changed, 22 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Wed Nov 03 00:03:02 2010 +0200
+++ b/nnchat.c	Wed Nov 03 00:03:20 2010 +0200
@@ -12,6 +12,7 @@
 #ifdef __WIN32
 /* Undefine because both windows.h and curses.h #define it */
 #undef MOUSE_MOVED
+#include <shlwapi.h>
 #else
 #include <sys/wait.h>
 #endif
@@ -805,6 +806,9 @@
     nn_editbuf_t *editBuf = nn_editbuf_new(SET_BUFSIZE);
     nn_editbuf_t *histBuf[SET_MAX_HISTORY+2];
     int histPos = 0, histMax = 0;
+
+    cfgitem_t *tmpcfg;
+    char *homeDir = NULL;
     
     memset(histBuf, 0, sizeof(histBuf));
     
@@ -815,24 +819,6 @@
     th_verbosityLevel = 0;
 
     /* Read config */
-    {
-    cfgitem_t *tmpcfg;
-    FILE *cfgfile;
-    char *homeDir;
-
-#ifdef __WIN32
-    
-#else
-    homeDir = getenv("HOME");
-#endif
-    
-    if (homeDir != NULL)
-        setConfigFile = th_strdup_printf("%s" SET_DIR_SEPARATOR "%s", homeDir, SET_CONFIG_FILE);
-    else
-        setConfigFile = th_strdup(SET_CONFIG_FILE);
-
-        THMSG(0, "Reading configuration from '%s'.\n", setConfigFile);
-
     tmpcfg = NULL;
     th_cfg_add_comment(&tmpcfg, "General settings");
     th_cfg_add_string(&tmpcfg, "username", &optUserName, NULL);
@@ -853,8 +839,24 @@
     th_cfg_add_int(&tmpcfg, "port", &optPort, optPort);
     th_cfg_add_section(&cfg, "server", tmpcfg);
 
-    if ((cfgfile = fopen(setConfigFile, "r")) != NULL)
-        th_cfg_read(cfgfile, setConfigFile, cfg);
+#ifdef __WIN32
+    {
+    char tmpPath[MAX_PATH];
+    if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK)
+        homeDir = th_strdup(tmpPath);
+    }
+#else
+    homeDir = getenv("HOME");
+#endif
+    
+    if (homeDir != NULL) {
+        FILE *cfgfile;
+        setConfigFile = th_strdup_printf("%s" SET_DIR_SEPARATOR "%s", homeDir, SET_CONFIG_FILE);
+
+        THMSG(0, "Reading configuration from '%s'.\n", setConfigFile);
+
+        if ((cfgfile = fopen(setConfigFile, "r")) != NULL)
+            th_cfg_read(cfgfile, setConfigFile, cfg);
     }
     
     setBrowser = getenv("BROWSER");