ui/common/properties.c

changeset 431
bb7da585debc
parent 395
b8277deb75b8
equal deleted inserted replaced
169:fe49cff3c571 431:bb7da585debc
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 #include <sys/stat.h> 32 #include <sys/stat.h>
33 #include <errno.h> 33 #include <errno.h>
34 34
35 #include "../ui/toolkit.h"
36
37
35 #include "properties.h" 38 #include "properties.h"
36 #include <ucx/string.h> 39 #include <cx/string.h>
37 #include <ucx/buffer.h> 40 #include <cx/buffer.h>
38 #include <ucx/properties.h> 41
39 42 #include <cx/hash_map.h>
40 static UiProperties *application_properties; 43 #include "ucx_properties.h"
41 static UiProperties *language; 44
45 static CxMap *application_properties;
46 static CxMap *language;
42 47
43 #ifndef UI_COCOA 48 #ifndef UI_COCOA
44 49
45 static char *locales_dir; 50 static char *locales_dir;
46 static char *pixmaps_dir; 51 static char *pixmaps_dir;
47 52
48 #endif 53 #endif
49 54
50 char* ui_getappdir() { 55
56 char* ui_getappdir(void) {
51 if(ui_appname() == NULL) { 57 if(ui_appname() == NULL) {
52 return NULL; 58 return NULL;
53 } 59 }
54 60
55 return ui_configfile(NULL); 61 return ui_configfile(NULL);
56 } 62 }
57 63
64 #ifndef _WIN32
65 #define UI_PATH_SEPARATOR '/'
66 #define UI_ENV_HOME "HOME"
67 #else
68 #define UI_PATH_SEPARATOR '\\'
69 #define UI_ENV_HOME "USERPROFILE"
70 #endif
71
58 char* ui_configfile(char *name) { 72 char* ui_configfile(char *name) {
59 char *appname = ui_appname(); 73 const char *appname = ui_appname();
60 if(!appname) { 74 if(!appname) {
61 return NULL; 75 return NULL;
62 } 76 }
63 77
64 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); 78 CxBuffer buf;
79 cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
65 80
66 // add base dir 81 // add base dir
67 char *homeenv = getenv("HOME"); 82 char *homeenv = getenv(UI_ENV_HOME);
68 if(homeenv == NULL) { 83 if(homeenv == NULL) {
69 ucx_buffer_free(buf); 84 cxBufferDestroy(&buf);
70 return NULL; 85 return NULL;
71 } 86 }
72 sstr_t home = sstr(homeenv); 87 cxstring home = cx_str(homeenv);
73 88
74 ucx_buffer_write(home.ptr, 1, home.length, buf); 89 cxBufferWrite(home.ptr, 1, home.length, &buf);
75 if(home.ptr[home.length-1] != '/') { 90 if(home.ptr[home.length-1] != UI_PATH_SEPARATOR) {
76 ucx_buffer_putc(buf, '/'); 91 cxBufferPut(&buf, UI_PATH_SEPARATOR);
77 } 92 }
78 93
79 #ifdef UI_COCOA 94 #ifdef UI_COCOA
80 // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/ 95 // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/
81 ucx_buffer_puts(buf, "Library/Application Support/"); 96 cxBufferPutString(&buf, "Library/Application Support/");
97 #elif defined(_WIN32)
98 // on Windows the app dir is $USERPROFILE/AppData/Local/$APPNAME/
99 cxBufferPutString(&buf, "AppData\\Local\\");
82 #else 100 #else
83 // app dir is $HOME/.$APPNAME/ 101 // app dir is $HOME/.$APPNAME/
84 ucx_buffer_putc(buf, '.'); 102 cxBufferPut(&buf, '.');
85 #endif 103 #endif
86 ucx_buffer_puts(buf, appname); 104 cxBufferPutString(&buf, appname);
87 ucx_buffer_putc(buf, '/'); 105 cxBufferPut(&buf, UI_PATH_SEPARATOR);
88 106
89 // add file name 107 // add file name
90 if(name) { 108 if(name) {
91 ucx_buffer_puts(buf, name); 109 cxBufferPutString(&buf, name);
92 } 110 }
93 111
94 char *path = buf->space; 112 cxBufferPut(&buf, 0);
95 free(buf); 113
96 return path; 114 return buf.space;
97 } 115 }
98 116
99 static int ui_mkdir(char *path) { 117 static int ui_mkdir(char *path) {
100 #ifdef _WIN32 118 #ifdef _WIN32
101 return mkdir(path); 119 return mkdir(path);
103 return mkdir(path, S_IRWXU); 121 return mkdir(path, S_IRWXU);
104 #endif 122 #endif
105 } 123 }
106 124
107 void uic_load_app_properties() { 125 void uic_load_app_properties() {
108 application_properties = ucx_map_new(128); 126 application_properties = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 128);
109 127
110 if(!ui_appname()) { 128 if(!ui_appname()) {
111 // applications without name cannot load app properties 129 // applications without name cannot load app properties
112 return; 130 return;
113 } 131 }
142 160
143 fclose(file); 161 fclose(file);
144 free(path); 162 free(path);
145 } 163 }
146 164
147 void uic_store_app_properties() { 165 int uic_store_app_properties() {
148 char *path = ui_configfile("application.properties"); 166 char *path = ui_configfile("application.properties");
149 if(!path) { 167 if(!path) {
150 return; 168 return 1;
151 } 169 }
152 170
153 FILE *file = fopen(path, "w"); 171 FILE *file = fopen(path, "w");
154 if(!file) { 172 if(!file) {
155 fprintf(stderr, "Ui Error: Cannot open properties file: %s\n", path); 173 fprintf(stderr, "Ui Error: Cannot open properties file: %s\n", path);
156 free(path); 174 free(path);
157 return; 175 return 1;
158 } 176 }
159 177
178 int ret = 0;
160 if(ucx_properties_store(application_properties, file)) { 179 if(ucx_properties_store(application_properties, file)) {
161 fprintf(stderr, "Ui Error: Cannot store application properties.\n"); 180 fprintf(stderr, "Ui Error: Cannot store application properties.\n");
181 ret = 1;
162 } 182 }
163 183
164 fclose(file); 184 fclose(file);
165 free(path); 185 free(path);
166 } 186
167 187 return ret;
168 188 }
169 char* ui_get_property(char *name) { 189
170 return ucx_map_cstr_get(application_properties, name); 190
171 } 191 const char* ui_get_property(const char *name) {
172 192 return cxMapGet(application_properties, name);
173 void ui_set_property(char *name, char *value) { 193 }
174 ucx_map_cstr_put(application_properties, name, value); 194
175 } 195 void ui_set_property(const char *name, const char *value) {
176 196 cxMapPut(application_properties, name, (char*)value);
177 void ui_set_default_property(char *name, char *value) { 197 }
178 char *v = ucx_map_cstr_get(application_properties, name); 198
199 const char* ui_set_default_property(const char *name, const char *value) {
200 const char *v = cxMapGet(application_properties, name);
179 if(!v) { 201 if(!v) {
180 ucx_map_cstr_put(application_properties, name, value); 202 cxMapPut(application_properties, name, (char*)value);
181 } 203 v = value;
182 } 204 }
183 205 return v;
206 }
207
208 int ui_properties_store(void) {
209 return uic_store_app_properties();
210 }
184 211
185 212
186 static char* uic_concat_path(const char *base, const char *p, const char *ext) { 213 static char* uic_concat_path(const char *base, const char *p, const char *ext) {
187 size_t baselen = strlen(base); 214 size_t baselen = strlen(base);
188 215
189 UcxBuffer *buf = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); 216 CxBuffer *buf = cxBufferCreate(NULL, 32, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
190 if(baselen > 0) { 217 if(baselen > 0) {
191 ucx_buffer_write(base, 1, baselen, buf); 218 cxBufferWrite(base, 1, baselen, buf);
192 if(base[baselen - 1] != '/') { 219 if(base[baselen - 1] != '/') {
193 ucx_buffer_putc(buf, '/'); 220 cxBufferPut(buf, '/');
194 } 221 }
195 } 222 }
196 ucx_buffer_write(p, 1, strlen(p), buf); 223 cxBufferWrite(p, 1, strlen(p), buf);
197 if(ext) { 224 if(ext) {
198 ucx_buffer_write(ext, 1, strlen(ext), buf); 225 cxBufferWrite(ext, 1, strlen(ext), buf);
199 } 226 }
200 227
201 char *str = buf->space; 228 char *str = buf->space;
202 free(buf); 229 free(buf);
203 return str; 230 return str;
261 } 288 }
262 289
263 #endif 290 #endif
264 291
265 int uic_load_language_file(const char *path) { 292 int uic_load_language_file(const char *path) {
266 UcxMap *lang = ucx_map_new(256); 293 CxMap *lang = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 256);
267 294
268 FILE *file = fopen(path, "r"); 295 FILE *file = fopen(path, "r");
269 if(!file) { 296 if(!file) {
270 return 1; 297 return 1;
271 } 298 }
274 fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path); 301 fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path);
275 } 302 }
276 303
277 fclose(file); 304 fclose(file);
278 305
279 ucx_map_rehash(lang); 306 cxMapRehash(lang);
280 307
281 language = lang; 308 language = lang;
282 309
283 return 0; 310 return 0;
284 } 311 }
290 317
291 char* uistr_n(char *name) { 318 char* uistr_n(char *name) {
292 if(!language) { 319 if(!language) {
293 return NULL; 320 return NULL;
294 } 321 }
295 return ucx_map_cstr_get(language, name); 322 return cxMapGet(language, name);
296 } 323 }
297

mercurial