ui/common/properties.c

branch
newapi
changeset 174
0358f1d9c506
parent 157
0b33b9396851
child 254
13997c76859b
equal deleted inserted replaced
173:809581724cc7 174:0358f1d9c506
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 "properties.h" 35 #include "properties.h"
36 #include <ucx/string.h> 36 #include <cx/string.h>
37 #include <ucx/buffer.h> 37 #include <cx/buffer.h>
38 #include <ucx/properties.h> 38
39 39 #include "ucx_properties.h"
40 static UiProperties *application_properties; 40
41 static UiProperties *language; 41 static CxMap *application_properties;
42 static CxMap *language;
42 43
43 #ifndef UI_COCOA 44 #ifndef UI_COCOA
44 45
45 static char *locales_dir; 46 static char *locales_dir;
46 static char *pixmaps_dir; 47 static char *pixmaps_dir;
59 char *appname = ui_appname(); 60 char *appname = ui_appname();
60 if(!appname) { 61 if(!appname) {
61 return NULL; 62 return NULL;
62 } 63 }
63 64
64 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); 65 CxBuffer buf;
66 cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
65 67
66 // add base dir 68 // add base dir
67 char *homeenv = getenv("HOME"); 69 char *homeenv = getenv("HOME");
68 if(homeenv == NULL) { 70 if(homeenv == NULL) {
69 ucx_buffer_free(buf); 71 cxBufferDestroy(&buf);
70 return NULL; 72 return NULL;
71 } 73 }
72 sstr_t home = sstr(homeenv); 74 cxstring home = cx_str(homeenv);
73 75
74 ucx_buffer_write(home.ptr, 1, home.length, buf); 76 cxBufferWrite(home.ptr, 1, home.length, &buf);
75 if(home.ptr[home.length-1] != '/') { 77 if(home.ptr[home.length-1] != '/') {
76 ucx_buffer_putc(buf, '/'); 78 cxBufferPut(&buf, '/');
77 } 79 }
78 80
79 #ifdef UI_COCOA 81 #ifdef UI_COCOA
80 // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/ 82 // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/
81 ucx_buffer_puts(buf, "Library/Application Support/"); 83 ucx_buffer_puts(buf, "Library/Application Support/");
82 #else 84 #else
83 // app dir is $HOME/.$APPNAME/ 85 // app dir is $HOME/.$APPNAME/
84 ucx_buffer_putc(buf, '.'); 86 cxBufferPut(&buf, '.');
85 #endif 87 #endif
86 ucx_buffer_puts(buf, appname); 88 cxBufferPutString(&buf, appname);
87 ucx_buffer_putc(buf, '/'); 89 cxBufferPut(&buf, '/');
88 90
89 // add file name 91 // add file name
90 if(name) { 92 if(name) {
91 ucx_buffer_puts(buf, name); 93 cxBufferPutString(&buf, name);
92 } 94 }
93 95
94 char *path = buf->space; 96 cxBufferPut(&buf, 0);
95 free(buf); 97
96 return path; 98 return buf.space;
97 } 99 }
98 100
99 static int ui_mkdir(char *path) { 101 static int ui_mkdir(char *path) {
100 #ifdef _WIN32 102 #ifdef _WIN32
101 return mkdir(path); 103 return mkdir(path);
103 return mkdir(path, S_IRWXU); 105 return mkdir(path, S_IRWXU);
104 #endif 106 #endif
105 } 107 }
106 108
107 void uic_load_app_properties() { 109 void uic_load_app_properties() {
108 application_properties = ucx_map_new(128); 110 application_properties = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 128);
109 111
110 if(!ui_appname()) { 112 if(!ui_appname()) {
111 // applications without name cannot load app properties 113 // applications without name cannot load app properties
112 return; 114 return;
113 } 115 }
165 free(path); 167 free(path);
166 } 168 }
167 169
168 170
169 char* ui_get_property(char *name) { 171 char* ui_get_property(char *name) {
170 return ucx_map_cstr_get(application_properties, name); 172 return cxMapGet(application_properties, name);
171 } 173 }
172 174
173 void ui_set_property(char *name, char *value) { 175 void ui_set_property(char *name, char *value) {
174 ucx_map_cstr_put(application_properties, name, value); 176 cxMapPut(application_properties, name, value);
175 } 177 }
176 178
177 void ui_set_default_property(char *name, char *value) { 179 void ui_set_default_property(char *name, char *value) {
178 char *v = ucx_map_cstr_get(application_properties, name); 180 char *v = cxMapGet(application_properties, name);
179 if(!v) { 181 if(!v) {
180 ucx_map_cstr_put(application_properties, name, value); 182 cxMapPut(application_properties, name, value);
181 } 183 }
182 } 184 }
183 185
184 186
185 187
186 static char* uic_concat_path(const char *base, const char *p, const char *ext) { 188 static char* uic_concat_path(const char *base, const char *p, const char *ext) {
187 size_t baselen = strlen(base); 189 size_t baselen = strlen(base);
188 190
189 UcxBuffer *buf = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); 191 CxBuffer *buf = cxBufferCreate(NULL, 32, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
190 if(baselen > 0) { 192 if(baselen > 0) {
191 ucx_buffer_write(base, 1, baselen, buf); 193 cxBufferWrite(base, 1, baselen, buf);
192 if(base[baselen - 1] != '/') { 194 if(base[baselen - 1] != '/') {
193 ucx_buffer_putc(buf, '/'); 195 cxBufferPut(buf, '/');
194 } 196 }
195 } 197 }
196 ucx_buffer_write(p, 1, strlen(p), buf); 198 cxBufferWrite(p, 1, strlen(p), buf);
197 if(ext) { 199 if(ext) {
198 ucx_buffer_write(ext, 1, strlen(ext), buf); 200 cxBufferWrite(ext, 1, strlen(ext), buf);
199 } 201 }
200 202
201 char *str = buf->space; 203 char *str = buf->space;
202 free(buf); 204 free(buf);
203 return str; 205 return str;
261 } 263 }
262 264
263 #endif 265 #endif
264 266
265 int uic_load_language_file(const char *path) { 267 int uic_load_language_file(const char *path) {
266 UcxMap *lang = ucx_map_new(256); 268 CxMap *lang = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 256);
267 269
268 FILE *file = fopen(path, "r"); 270 FILE *file = fopen(path, "r");
269 if(!file) { 271 if(!file) {
270 return 1; 272 return 1;
271 } 273 }
274 fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path); 276 fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path);
275 } 277 }
276 278
277 fclose(file); 279 fclose(file);
278 280
279 ucx_map_rehash(lang); 281 cxMapRehash(lang);
280 282
281 language = lang; 283 language = lang;
282 284
283 return 0; 285 return 0;
284 } 286 }
290 292
291 char* uistr_n(char *name) { 293 char* uistr_n(char *name) {
292 if(!language) { 294 if(!language) {
293 return NULL; 295 return NULL;
294 } 296 }
295 return ucx_map_cstr_get(language, name); 297 return cxMapGet(language, name);
296 } 298 }
297 299

mercurial