ui/common/properties.c

changeset 992
f421aef8f865
parent 974
dc72d666d721
child 993
8ebf55d78341
equal deleted inserted replaced
991:ab3125bd8b5f 992:f421aef8f865
42 #include "properties.h" 42 #include "properties.h"
43 #include <cx/string.h> 43 #include <cx/string.h>
44 #include <cx/buffer.h> 44 #include <cx/buffer.h>
45 45
46 #include <cx/hash_map.h> 46 #include <cx/hash_map.h>
47 #include "ucx_properties.h" 47 #include <cx/properties.h>
48 48
49 static CxMap *application_properties; 49 static CxMap *application_properties;
50 static CxMap *language; 50 static CxMap *language;
51 51
52 #ifndef UI_COCOA 52 #ifndef UI_COCOA
142 return _mkdir(path); 142 return _mkdir(path);
143 #else 143 #else
144 return mkdir(path, S_IRWXU); 144 return mkdir(path, S_IRWXU);
145 #endif 145 #endif
146 } 146 }
147
148 static int load_properties(FILE *file, CxMap *map) {
149 CxProperties prop;
150 cxPropertiesInitDefault(&prop);
151 char buf[8192];
152 size_t r;
153 CxPropertiesStatus status = CX_PROPERTIES_NO_ERROR;
154 while((r = fread(buf, 1, 8192, file)) > 0) {
155 cxPropertiesFilln(&prop, buf, r);
156 cxstring key;
157 cxstring value;
158 status = cxPropertiesNext(&prop, &key, &value);
159 if(status > CX_PROPERTIES_OK) {
160 break;
161 }
162 cxMapPut(map, key, cx_strdup(value).ptr);
163 }
164 return status == CX_PROPERTIES_NO_ERROR ? 0 : 1;
165 }
147 166
148 void uic_load_app_properties() { 167 void uic_load_app_properties() {
149 application_properties = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 128); 168 application_properties = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 128);
150 application_properties->collection.simple_destructor = free; 169 application_properties->collection.simple_destructor = free;
151 170
204 if(!file) { 223 if(!file) {
205 free(path); 224 free(path);
206 return; 225 return;
207 } 226 }
208 227
209 if(ucx_properties_load(application_properties, file)) { 228 if(load_properties(file, application_properties)) {
210 fprintf(stderr, "Ui Error: Cannot load application properties.\n"); 229 fprintf(stderr, "Error: Cannot load application properties.\n");
211 } 230 }
212 231
213 fclose(file); 232 fclose(file);
214 free(path); 233 free(path);
215 } 234 }
226 free(path); 245 free(path);
227 return 1; 246 return 1;
228 } 247 }
229 248
230 int ret = 0; 249 int ret = 0;
231 if(ucx_properties_store(application_properties, file)) { 250 CxMapIterator i = cxMapIterator(application_properties);
232 fprintf(stderr, "Ui Error: Cannot store application properties.\n"); 251 cx_foreach(CxMapEntry *, entry, i) {
233 ret = 1; 252 fprintf(file, "%.*s: %s\n", (int)entry->key->len, entry->key->data, entry->value);
234 } 253 }
254
255 cxMapRehash(application_properties);
235 256
236 fclose(file); 257 fclose(file);
237 free(path); 258 free(path);
238 259
239 return ret; 260 return ret;
358 FILE *file = fopen(path, "r"); 379 FILE *file = fopen(path, "r");
359 if(!file) { 380 if(!file) {
360 return 1; 381 return 1;
361 } 382 }
362 383
363 if(ucx_properties_load(lang, file)) { 384 if(load_properties(file, lang)) {
364 fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path); 385 fprintf(stderr, "Error: Cannot parse language file: %s.\n", path);
365 } 386 }
366 387
367 fclose(file); 388 fclose(file);
368 389
369 cxMapRehash(lang); 390 cxMapRehash(lang);

mercurial