ui/common/properties.c

changeset 431
bb7da585debc
parent 395
b8277deb75b8
--- a/ui/common/properties.c	Sun May 23 09:44:43 2021 +0200
+++ b/ui/common/properties.c	Sat Jan 04 16:38:48 2025 +0100
@@ -32,13 +32,18 @@
 #include <sys/stat.h>
 #include <errno.h>
 
+#include "../ui/toolkit.h"
+
+
 #include "properties.h"
-#include <ucx/string.h>
-#include <ucx/buffer.h>
-#include <ucx/properties.h>
+#include <cx/string.h>
+#include <cx/buffer.h>
 
-static UiProperties *application_properties;
-static UiProperties *language;
+#include <cx/hash_map.h>
+#include "ucx_properties.h"
+
+static CxMap *application_properties;
+static CxMap *language;
 
 #ifndef UI_COCOA
 
@@ -47,53 +52,66 @@
 
 #endif
 
-char* ui_getappdir() {
+
+char* ui_getappdir(void) {
     if(ui_appname() == NULL) {
         return NULL;
     }
-    
+
     return ui_configfile(NULL);
 }
 
+#ifndef _WIN32
+#define UI_PATH_SEPARATOR '/'
+#define UI_ENV_HOME "HOME"
+#else
+#define UI_PATH_SEPARATOR '\\'
+#define UI_ENV_HOME "USERPROFILE"
+#endif
+
 char* ui_configfile(char *name) {
-    char *appname = ui_appname();
+    const char *appname = ui_appname();
     if(!appname) {
         return NULL;
     }
     
-    UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
+    CxBuffer buf;
+    cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
     
     // add base dir
-    char *homeenv = getenv("HOME");
+    char *homeenv = getenv(UI_ENV_HOME);
     if(homeenv == NULL) {
-        ucx_buffer_free(buf);
+        cxBufferDestroy(&buf);
         return NULL;
     }
-    sstr_t home = sstr(homeenv);
+    cxstring home = cx_str(homeenv);
     
-    ucx_buffer_write(home.ptr, 1, home.length, buf);
-    if(home.ptr[home.length-1] != '/') {
-        ucx_buffer_putc(buf, '/');
+    cxBufferWrite(home.ptr, 1, home.length, &buf);
+    if(home.ptr[home.length-1] != UI_PATH_SEPARATOR) {
+        cxBufferPut(&buf, UI_PATH_SEPARATOR);
     }
     
 #ifdef UI_COCOA
     // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/
-    ucx_buffer_puts(buf, "Library/Application Support/");
+    cxBufferPutString(&buf, "Library/Application Support/");
+#elif defined(_WIN32)
+    // on Windows the app dir is $USERPROFILE/AppData/Local/$APPNAME/
+    cxBufferPutString(&buf, "AppData\\Local\\");
 #else
     // app dir is $HOME/.$APPNAME/
-    ucx_buffer_putc(buf, '.');
+    cxBufferPut(&buf, '.');
 #endif
-    ucx_buffer_puts(buf, appname);
-    ucx_buffer_putc(buf, '/');
+    cxBufferPutString(&buf, appname);
+    cxBufferPut(&buf, UI_PATH_SEPARATOR);
     
     // add file name
     if(name) {
-        ucx_buffer_puts(buf, name);
+        cxBufferPutString(&buf, name);
     }
     
-    char *path = buf->space;
-    free(buf);
-    return path;  
+    cxBufferPut(&buf, 0);
+    
+    return buf.space;
 }
 
 static int ui_mkdir(char *path) {
@@ -105,7 +123,7 @@
 } 
 
 void uic_load_app_properties() {
-    application_properties = ucx_map_new(128);
+    application_properties = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 128);
     
     if(!ui_appname()) {
         // applications without name cannot load app properties
@@ -144,58 +162,67 @@
     free(path);
 }
 
-void uic_store_app_properties() {
+int uic_store_app_properties() {
     char *path = ui_configfile("application.properties");
     if(!path) {
-        return;
+        return 1;
     }
     
     FILE *file = fopen(path, "w");
     if(!file) {
         fprintf(stderr, "Ui Error: Cannot open properties file: %s\n", path);
         free(path);
-        return;
+        return 1;
     }
     
+    int ret = 0;
     if(ucx_properties_store(application_properties, file)) {
         fprintf(stderr, "Ui Error: Cannot store application properties.\n");
+        ret = 1;
     }
     
     fclose(file);
     free(path);
+    
+    return ret;
 }
 
 
-char* ui_get_property(char *name) {
-    return ucx_map_cstr_get(application_properties, name);
+const char* ui_get_property(const char *name) {
+    return cxMapGet(application_properties, name);
 }
 
-void ui_set_property(char *name, char *value) {
-    ucx_map_cstr_put(application_properties, name, value);
+void ui_set_property(const char *name, const char *value) {
+    cxMapPut(application_properties, name, (char*)value);
 }
 
-void ui_set_default_property(char *name, char *value) {
-    char *v = ucx_map_cstr_get(application_properties, name);
+const char* ui_set_default_property(const char *name, const char *value) {
+    const char *v = cxMapGet(application_properties, name);
     if(!v) {
-        ucx_map_cstr_put(application_properties, name, value);
+        cxMapPut(application_properties, name, (char*)value);
+        v = value;
     }
+    return v;
 }
 
+int ui_properties_store(void) {
+    return uic_store_app_properties();
+}
 
 
 static char* uic_concat_path(const char *base, const char *p, const char *ext) {
     size_t baselen = strlen(base);
     
-    UcxBuffer *buf = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
+    CxBuffer *buf = cxBufferCreate(NULL, 32, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
     if(baselen > 0) {
-        ucx_buffer_write(base, 1, baselen, buf);
+        cxBufferWrite(base, 1, baselen, buf);
         if(base[baselen - 1] != '/') {
-            ucx_buffer_putc(buf, '/');
+            cxBufferPut(buf, '/');
         }
     }
-    ucx_buffer_write(p, 1, strlen(p), buf);
+    cxBufferWrite(p, 1, strlen(p), buf);
     if(ext) {
-        ucx_buffer_write(ext, 1, strlen(ext), buf);
+        cxBufferWrite(ext, 1, strlen(ext), buf);
     }
     
     char *str = buf->space;
@@ -263,7 +290,7 @@
 #endif
 
 int uic_load_language_file(const char *path) {
-    UcxMap *lang = ucx_map_new(256);
+    CxMap *lang = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 256);
     
     FILE *file = fopen(path, "r");
     if(!file) {
@@ -276,7 +303,7 @@
     
     fclose(file);
     
-    ucx_map_rehash(lang);
+    cxMapRehash(lang);
     
     language = lang;
     
@@ -292,6 +319,5 @@
     if(!language) {
         return NULL;
     }
-    return ucx_map_cstr_get(language, name);
+    return cxMapGet(language, name);
 }
-

mercurial