|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2014 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 "../../ucx/string.h" |
|
37 #include "../../ucx/buffer.h" |
|
38 #include "../../ucx/properties.h" |
|
39 |
|
40 static UiProperties *application_properties; |
|
41 |
|
42 char* ui_getappdir() { |
|
43 if(ui_appname() == NULL) { |
|
44 return NULL; |
|
45 } |
|
46 |
|
47 return ui_configfile(NULL); |
|
48 } |
|
49 |
|
50 char* ui_configfile(char *name) { |
|
51 char *appname = ui_appname(); |
|
52 if(!appname) { |
|
53 return NULL; |
|
54 } |
|
55 |
|
56 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); |
|
57 |
|
58 // add base dir |
|
59 char *homeenv = getenv("HOME"); |
|
60 if(homeenv == NULL) { |
|
61 ucx_buffer_free(buf); |
|
62 return NULL; |
|
63 } |
|
64 sstr_t home = sstr(homeenv); |
|
65 |
|
66 ucx_buffer_write(home.ptr, 1, home.length, buf); |
|
67 if(home.ptr[home.length-1] != '/') { |
|
68 ucx_buffer_putc(buf, '/'); |
|
69 } |
|
70 |
|
71 #ifdef UI_COCOA |
|
72 // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/ |
|
73 ucx_buffer_puts(buf, "Library/Application Support/"); |
|
74 #else |
|
75 // app dir is $HOME/.$APPNAME/ |
|
76 ucx_buffer_putc(buf, '.'); |
|
77 #endif |
|
78 ucx_buffer_puts(buf, appname); |
|
79 ucx_buffer_putc(buf, '/'); |
|
80 |
|
81 // add file name |
|
82 if(name) { |
|
83 ucx_buffer_puts(buf, name); |
|
84 } |
|
85 |
|
86 char *path = buf->space; |
|
87 free(buf); |
|
88 return path; |
|
89 } |
|
90 |
|
91 static int ui_mkdir(char *path) { |
|
92 #ifdef _WIN32 |
|
93 return mkdir(path); |
|
94 #else |
|
95 return mkdir(path, S_IRWXU); |
|
96 #endif |
|
97 } |
|
98 |
|
99 void uic_load_app_properties() { |
|
100 application_properties = ucx_map_new(128); |
|
101 |
|
102 if(!ui_appname()) { |
|
103 // applications without name cannot load app properties |
|
104 return; |
|
105 } |
|
106 |
|
107 char *dir = ui_configfile(NULL); |
|
108 if(!dir) { |
|
109 return; |
|
110 } |
|
111 if(ui_mkdir(dir)) { |
|
112 if(errno != EEXIST) { |
|
113 fprintf(stderr, "Ui Error: Cannot create directory %s\n", dir); |
|
114 free(dir); |
|
115 return; |
|
116 } |
|
117 } |
|
118 free(dir); |
|
119 |
|
120 char *path = ui_configfile("application.properties"); |
|
121 if(!path) { |
|
122 return; |
|
123 } |
|
124 |
|
125 FILE *file = fopen(path, "r"); |
|
126 if(!file) { |
|
127 free(path); |
|
128 return; |
|
129 } |
|
130 |
|
131 if(ucx_properties_load(application_properties, file)) { |
|
132 fprintf(stderr, "Ui Error: Cannot load application properties.\n"); |
|
133 } |
|
134 |
|
135 fclose(file); |
|
136 free(path); |
|
137 } |
|
138 |
|
139 void uic_store_app_properties() { |
|
140 char *path = ui_configfile("application.properties"); |
|
141 if(!path) { |
|
142 return; |
|
143 } |
|
144 |
|
145 FILE *file = fopen(path, "w"); |
|
146 if(!file) { |
|
147 fprintf(stderr, "Ui Error: Cannot open properties file: %s\n", path); |
|
148 free(path); |
|
149 return; |
|
150 } |
|
151 |
|
152 if(ucx_properties_store(application_properties, file)) { |
|
153 fprintf(stderr, "Ui Error: Cannot store application properties.\n"); |
|
154 } |
|
155 |
|
156 fclose(file); |
|
157 free(path); |
|
158 } |
|
159 |
|
160 |
|
161 char* ui_get_property(char *name) { |
|
162 return ucx_map_cstr_get(application_properties, name); |
|
163 } |
|
164 |
|
165 void ui_set_property(char *name, char *value) { |
|
166 ucx_map_cstr_put(application_properties, name, value); |
|
167 } |
|
168 |
|
169 void ui_set_default_property(char *name, char *value) { |
|
170 char *v = ucx_map_cstr_get(application_properties, name); |
|
171 if(!v) { |
|
172 ucx_map_cstr_put(application_properties, name, value); |
|
173 } |
|
174 } |