ui/common/properties.c

changeset 0
2483f517c562
child 29
3fc287f06305
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34
35 #include "properties.h"
36 #include <cx/string.h>
37 #include <cx/buffer.h>
38
39 #include "ucx_properties.h"
40
41 static CxMap *application_properties;
42 static CxMap *language;
43
44 #ifndef UI_COCOA
45
46 static char *locales_dir;
47 static char *pixmaps_dir;
48
49 #endif
50
51 char* ui_getappdir() {
52 if(ui_appname() == NULL) {
53 return NULL;
54 }
55
56 return ui_configfile(NULL);
57 }
58
59 char* ui_configfile(char *name) {
60 char *appname = ui_appname();
61 if(!appname) {
62 return NULL;
63 }
64
65 CxBuffer buf;
66 cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
67
68 // add base dir
69 char *homeenv = getenv("HOME");
70 if(homeenv == NULL) {
71 cxBufferDestroy(&buf);
72 return NULL;
73 }
74 cxstring home = cx_str(homeenv);
75
76 cxBufferWrite(home.ptr, 1, home.length, &buf);
77 if(home.ptr[home.length-1] != '/') {
78 cxBufferPut(&buf, '/');
79 }
80
81 #ifdef UI_COCOA
82 // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/
83 ucx_buffer_puts(buf, "Library/Application Support/");
84 #else
85 // app dir is $HOME/.$APPNAME/
86 cxBufferPut(&buf, '.');
87 #endif
88 cxBufferPutString(&buf, appname);
89 cxBufferPut(&buf, '/');
90
91 // add file name
92 if(name) {
93 cxBufferPutString(&buf, name);
94 }
95
96 cxBufferPut(&buf, 0);
97
98 return buf.space;
99 }
100
101 static int ui_mkdir(char *path) {
102 #ifdef _WIN32
103 return mkdir(path);
104 #else
105 return mkdir(path, S_IRWXU);
106 #endif
107 }
108
109 void uic_load_app_properties() {
110 application_properties = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 128);
111
112 if(!ui_appname()) {
113 // applications without name cannot load app properties
114 return;
115 }
116
117 char *dir = ui_configfile(NULL);
118 if(!dir) {
119 return;
120 }
121 if(ui_mkdir(dir)) {
122 if(errno != EEXIST) {
123 fprintf(stderr, "Ui Error: Cannot create directory %s\n", dir);
124 free(dir);
125 return;
126 }
127 }
128 free(dir);
129
130 char *path = ui_configfile("application.properties");
131 if(!path) {
132 return;
133 }
134
135 FILE *file = fopen(path, "r");
136 if(!file) {
137 free(path);
138 return;
139 }
140
141 if(ucx_properties_load(application_properties, file)) {
142 fprintf(stderr, "Ui Error: Cannot load application properties.\n");
143 }
144
145 fclose(file);
146 free(path);
147 }
148
149 void uic_store_app_properties() {
150 char *path = ui_configfile("application.properties");
151 if(!path) {
152 return;
153 }
154
155 FILE *file = fopen(path, "w");
156 if(!file) {
157 fprintf(stderr, "Ui Error: Cannot open properties file: %s\n", path);
158 free(path);
159 return;
160 }
161
162 if(ucx_properties_store(application_properties, file)) {
163 fprintf(stderr, "Ui Error: Cannot store application properties.\n");
164 }
165
166 fclose(file);
167 free(path);
168 }
169
170
171 char* ui_get_property(char *name) {
172 return cxMapGet(application_properties, name);
173 }
174
175 void ui_set_property(char *name, char *value) {
176 cxMapPut(application_properties, name, value);
177 }
178
179 void ui_set_default_property(char *name, char *value) {
180 char *v = cxMapGet(application_properties, name);
181 if(!v) {
182 cxMapPut(application_properties, name, value);
183 }
184 }
185
186
187
188 static char* uic_concat_path(const char *base, const char *p, const char *ext) {
189 size_t baselen = strlen(base);
190
191 CxBuffer *buf = cxBufferCreate(NULL, 32, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
192 if(baselen > 0) {
193 cxBufferWrite(base, 1, baselen, buf);
194 if(base[baselen - 1] != '/') {
195 cxBufferPut(buf, '/');
196 }
197 }
198 cxBufferWrite(p, 1, strlen(p), buf);
199 if(ext) {
200 cxBufferWrite(ext, 1, strlen(ext), buf);
201 }
202
203 char *str = buf->space;
204 free(buf);
205 return str;
206 }
207
208 #ifndef UI_COCOA
209
210 void ui_locales_dir(char *path) {
211 locales_dir = path;
212 }
213
214 void ui_pixmaps_dir(char *path) {
215 pixmaps_dir = path;
216 }
217
218 char* uic_get_image_path(const char *imgfilename) {
219 if(pixmaps_dir) {
220 return uic_concat_path(pixmaps_dir, imgfilename, NULL);
221 } else {
222 return NULL;
223 }
224 }
225
226 void ui_load_lang(char *locale) {
227 if(!locale) {
228 locale = "en_EN";
229 }
230
231 char *path = uic_concat_path(locales_dir, locale, ".properties");
232
233 uic_load_language_file(path);
234 free(path);
235 }
236
237 void ui_load_lang_def(char *locale, char *default_locale) {
238 char tmp[6];
239 if(!locale) {
240 char *lang = getenv("LANG");
241 if(lang && strlen(lang) >= 5) {
242 memcpy(tmp, lang, 5);
243 tmp[5] = '\0';
244 locale = tmp;
245 } else {
246 locale = default_locale;
247 }
248 }
249
250 char *path = uic_concat_path(locales_dir, locale, ".properties");
251
252 if(uic_load_language_file(path)) {
253 if(default_locale) {
254 ui_load_lang_def(default_locale, NULL);
255 } else {
256 // cannot find any language file
257 fprintf(stderr, "Ui Error: Cannot load language.\n");
258 free(path);
259 exit(-1);
260 }
261 }
262 free(path);
263 }
264
265 #endif
266
267 int uic_load_language_file(const char *path) {
268 CxMap *lang = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 256);
269
270 FILE *file = fopen(path, "r");
271 if(!file) {
272 return 1;
273 }
274
275 if(ucx_properties_load(lang, file)) {
276 fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path);
277 }
278
279 fclose(file);
280
281 cxMapRehash(lang);
282
283 language = lang;
284
285 return 0;
286 }
287
288 char* uistr(char *name) {
289 char *value = uistr_n(name);
290 return value ? value : "missing string";
291 }
292
293 char* uistr_n(char *name) {
294 if(!language) {
295 return NULL;
296 }
297 return cxMapGet(language, name);
298 }
299

mercurial