Revision 2836
Added by stephane over 13 years ago
scol_glib_keyfile.c | ||
---|---|---|
23 | 23 |
http://www.gnu.org/copyleft/lesser.txt |
24 | 24 |
|
25 | 25 |
For others informations, please contact us from http://www.scolring.org/ |
26 |
*/
|
|
27 |
|
|
28 |
#include "../include/scol_glib_keyfile.h"
|
|
29 |
|
|
30 |
#if ((defined _WIN32) || (defined __WIN32__))
|
|
31 |
cbmachine ww;
|
|
32 |
#endif
|
|
33 |
mmachine mm;
|
|
34 |
|
|
35 |
|
|
36 |
/**
|
|
37 |
* \brief GKF_VALUE_RAW : I : flag used to load a key file. The raw value associated with key under group.
|
|
38 |
* The value can be escaped string
|
|
39 |
*/
|
|
40 |
int GKF_VALUE_RAW (mmachine m) { return MMpush (m, ITOM (1)); }
|
|
41 |
/**
|
|
42 |
* \brief GKF_VALUE_STRING : I : flag used to load a key file. The string value associated with key under group.
|
|
43 |
* Unlike GKF_VALUE_RAW, any escaped sequence are unescaped
|
|
44 |
*/
|
|
45 |
int GKF_VALUE_STRING (mmachine m) { return MMpush (m, ITOM (2)); }
|
|
46 |
/**
|
|
47 |
* \brief GKF_VALUE_INTEGER : Not implemented yet
|
|
48 |
*/
|
|
49 |
int GKF_VALUE_INTEGER (mmachine m) { return MMpush (m, ITOM (3)); }
|
|
50 |
/**
|
|
51 |
* \brief GKF_VALUE_INTEGER64 : Not implemented yet
|
|
52 |
*/
|
|
53 |
int GKF_VALUE_INTEGER64 (mmachine m) { return MMpush (m, ITOM (4)); }
|
|
54 |
/**
|
|
55 |
* \brief GKF_VALUE_UINTEGER64 : Not implemented yet
|
|
56 |
*/
|
|
57 |
int GKF_VALUE_UINTEGER64 (mmachine m) { return MMpush (m, ITOM (5)); }
|
|
58 |
/**
|
|
59 |
* \brief GKF_VALUE_DOUBLE : Not implemented yet
|
|
60 |
*/
|
|
61 |
int GKF_VALUE_DOUBLE (mmachine m) { return MMpush (m, ITOM (6)); }
|
|
62 |
/**
|
|
63 |
* \brief GKF_VALUE_BOOLEAN : Not implemented yet
|
|
64 |
*/
|
|
65 |
int GKF_VALUE_BOOLEAN (mmachine m) { return MMpush (m, ITOM (7)); }
|
|
66 |
|
|
67 |
|
|
68 |
/**
|
|
69 |
* \brief _gkeyFileReadP : read a key file
|
|
70 |
* fun [P S] [[S [[S S] r1]] r1]
|
|
71 |
*
|
|
72 |
* \param P : any file (read reference only)
|
|
73 |
* \param S : the list seperator, or nil (default ";")
|
|
74 |
* \return [[S [[S S] r1]] r1] : the list of datas : group names and for each group name, a list of tuple [key value]
|
|
75 |
*/
|
|
76 |
int SCOL_gkeyFileReadP (mmachine m)
|
|
77 |
{
|
|
78 |
int mfile, msep;
|
|
79 |
gboolean b;
|
|
80 |
GKeyFile *ini;
|
|
81 |
GError *err = NULL;
|
|
82 |
|
|
83 |
msep = MTOP (MMpull (m));
|
|
84 |
mfile = MMpull (m);
|
|
85 |
if (mfile == NIL)
|
|
86 |
{
|
|
87 |
MMpush (m, NIL);
|
|
88 |
return 0;
|
|
89 |
}
|
|
90 |
|
|
91 |
ini = g_key_file_new ();
|
|
92 |
if (msep != NIL)
|
|
93 |
g_key_file_set_list_separator (ini, (MMstart (m, msep))[0]);
|
|
94 |
b = g_key_file_load_from_file (ini, MMstartstr (m, MTOP (mfile)), G_KEY_FILE_NONE, &err);
|
|
95 |
if (!b)
|
|
96 |
{
|
|
97 |
MMechostr (0, "SCOL_gkeyFileReadP load error : %d .:. %s\n", err->code, err->message);
|
|
98 |
g_error_free (err);
|
|
99 |
MMpush (m, NIL);
|
|
100 |
return 0;
|
|
101 |
}
|
|
102 |
g_key_file_free (ini);
|
|
103 |
return scol_g_key_file_read (ini, m);
|
|
104 |
}
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
/* ========================================================================= */
|
|
114 |
|
|
115 |
|
|
26 |
*/ |
|
27 |
|
|
28 |
#include "../include/scol_glib_keyfile.h" |
|
29 |
|
|
30 |
#if ((defined _WIN32) || (defined __WIN32__)) |
|
31 |
cbmachine ww; |
|
32 |
#endif |
|
33 |
mmachine mm; |
|
34 |
|
|
35 |
|
|
36 |
/** |
|
37 |
* \brief GKF_VALUE_RAW : I : flag used to load a key file. The raw value associated with key under group. |
|
38 |
* The value can be escaped string |
|
39 |
*/ |
|
40 |
int GKF_VALUE_RAW (mmachine m) { return MMpush (m, ITOM (1)); } |
|
41 |
/** |
|
42 |
* \brief GKF_VALUE_STRING : I : flag used to load a key file. The string value associated with key under group. |
|
43 |
* Unlike GKF_VALUE_RAW, any escaped sequence are unescaped |
|
44 |
*/ |
|
45 |
int GKF_VALUE_STRING (mmachine m) { return MMpush (m, ITOM (2)); } |
|
46 |
/** |
|
47 |
* \brief GKF_VALUE_INTEGER : Not implemented yet |
|
48 |
*/ |
|
49 |
int GKF_VALUE_INTEGER (mmachine m) { return MMpush (m, ITOM (3)); } |
|
50 |
/** |
|
51 |
* \brief GKF_VALUE_INTEGER64 : Not implemented yet |
|
52 |
*/ |
|
53 |
int GKF_VALUE_INTEGER64 (mmachine m) { return MMpush (m, ITOM (4)); } |
|
54 |
/** |
|
55 |
* \brief GKF_VALUE_UINTEGER64 : Not implemented yet |
|
56 |
*/ |
|
57 |
int GKF_VALUE_UINTEGER64 (mmachine m) { return MMpush (m, ITOM (5)); } |
|
58 |
/** |
|
59 |
* \brief GKF_VALUE_DOUBLE : Not implemented yet |
|
60 |
*/ |
|
61 |
int GKF_VALUE_DOUBLE (mmachine m) { return MMpush (m, ITOM (6)); } |
|
62 |
/** |
|
63 |
* \brief GKF_VALUE_BOOLEAN : Not implemented yet |
|
64 |
*/ |
|
65 |
int GKF_VALUE_BOOLEAN (mmachine m) { return MMpush (m, ITOM (7)); } |
|
66 |
|
|
67 |
|
|
68 |
/** |
|
69 |
* \brief _gkeyFileReadP : read a key file |
|
70 |
* fun [P S] [[S [[S S] r1]] r1] |
|
71 |
* |
|
72 |
* \param P : any file (read reference only) |
|
73 |
* \param S : the list seperator, or nil (default ";") |
|
74 |
* \return [[S [[S S] r1]] r1] : the list of datas = group names and for each group name, a list of tuple [key value]
|
|
75 |
*/ |
|
76 |
int SCOL_gkeyFileReadP (mmachine m) |
|
77 |
{ |
|
78 |
int mfile, msep; |
|
79 |
gboolean b; |
|
80 |
GKeyFile *ini; |
|
81 |
GError *err = NULL; |
|
82 |
|
|
83 |
msep = MTOP (MMpull (m)); |
|
84 |
mfile = MMpull (m); |
|
85 |
if (mfile == NIL) |
|
86 |
{ |
|
87 |
MMpush (m, NIL); |
|
88 |
return 0; |
|
89 |
} |
|
90 |
|
|
91 |
ini = g_key_file_new (); |
|
92 |
if (msep != NIL) |
|
93 |
g_key_file_set_list_separator (ini, (MMstart (m, msep))[0]); |
|
94 |
b = g_key_file_load_from_file (ini, MMstartstr (m, MTOP (mfile)), G_KEY_FILE_NONE, &err); |
|
95 |
if (!b) |
|
96 |
{ |
|
97 |
MMechostr (0, "SCOL_gkeyFileReadP load error : %d .:. %s\n", err->code, err->message); |
|
98 |
g_error_free (err); |
|
99 |
MMpush (m, NIL); |
|
100 |
return 0; |
|
101 |
} |
|
102 |
g_key_file_free (ini); |
|
103 |
return scol_g_key_file_read (ini, m); |
|
104 |
} |
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
/* ========================================================================= */ |
|
114 |
|
|
115 |
|
|
116 | 116 |
/** \private |
117 | 117 |
* internal function : get groups names |
118 |
*/
|
|
119 |
int scol_g_key_file_get_groups (mmachine m, int flag)
|
|
120 |
{
|
|
121 |
int mini;
|
|
122 |
int i = 0;
|
|
123 |
gsize len = 0;
|
|
124 |
GKeyFile *ini;
|
|
125 |
gchar **groups;
|
|
126 |
|
|
127 |
mini = MMpull (m);
|
|
128 |
if (mini == NIL)
|
|
129 |
{
|
|
118 |
*/ |
|
119 |
int scol_g_key_file_get_groups (mmachine m, int flag) |
|
120 |
{ |
|
121 |
int mini; |
|
122 |
int i = 0; |
|
123 |
gsize len = 0; |
|
124 |
GKeyFile *ini; |
|
125 |
gchar **groups; |
|
126 |
|
|
127 |
mini = MMpull (m); |
|
128 |
if (mini == NIL) |
|
129 |
{ |
|
130 | 130 |
MMechostr (0, "scol_g_key_file_get_groups error : object is nil\n"); |
131 |
MMpush (m, NIL);
|
|
132 |
return 0;
|
|
133 |
}
|
|
134 |
|
|
135 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE);
|
|
136 |
groups = g_key_file_get_groups (ini, &len);
|
|
137 |
if (flag)
|
|
138 |
MMpush (m, ITOM ((int) len)); /* I */
|
|
139 |
else
|
|
140 |
{
|
|
141 |
for (i = 0; i < (int) len; i++)
|
|
142 |
Mpushstrbloc (m, groups[i]);
|
|
143 |
MMpush (m, NIL);
|
|
144 |
for (i = 0; i < (int) len; i++)
|
|
145 |
{
|
|
146 |
MMpush (m, ITOM (2));
|
|
147 |
MBdeftab (m);
|
|
148 |
} /* [S r1] */
|
|
149 |
}
|
|
150 |
g_strfreev (groups);
|
|
151 |
return 0;
|
|
152 |
}
|
|
131 |
MMpush (m, NIL); |
|
132 |
return 0; |
|
133 |
} |
|
134 |
|
|
135 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
136 |
groups = g_key_file_get_groups (ini, &len); |
|
137 |
if (flag) |
|
138 |
MMpush (m, ITOM ((int) len)); /* I */ |
|
139 |
else |
|
140 |
{ |
|
141 |
for (i = 0; i < (int) len; i++) |
|
142 |
Mpushstrbloc (m, groups[i]); |
|
143 |
MMpush (m, NIL); |
|
144 |
for (i = 0; i < (int) len; i++) |
|
145 |
{ |
|
146 |
MMpush (m, ITOM (2)); |
|
147 |
MBdeftab (m); |
|
148 |
} /* [S r1] */ |
|
149 |
} |
|
150 |
g_strfreev (groups); |
|
151 |
return 0; |
|
152 |
} |
|
153 | 153 |
/** \private |
154 | 154 |
* internal function : return a list of the entire content ( [[S [[S S] r1]] r1] ) |
155 |
*/
|
|
156 |
int scol_g_key_file_read (GKeyFile *ini, mmachine m)
|
|
157 |
{
|
|
158 |
int i, j;
|
|
159 |
gsize ngroups, nkeys;
|
|
160 |
gchar **groups;
|
|
161 |
|
|
162 |
groups = g_key_file_get_groups (ini, &ngroups);
|
|
163 |
for (i = 0; i < (int) ngroups; i++)
|
|
164 |
{
|
|
165 |
gchar **keys;
|
|
166 |
|
|
167 |
Mpushstrbloc (m, groups[i]); /* S */
|
|
168 |
|
|
169 |
keys = g_key_file_get_keys (ini, groups[i], &nkeys, NULL);
|
|
170 |
for (j = 0; j < (int) nkeys; j++)
|
|
171 |
{
|
|
172 |
gchar *value;
|
|
173 |
|
|
174 |
value = g_key_file_get_value (ini, groups[i], keys[j], NULL);
|
|
175 |
Mpushstrbloc (m, keys[j]);
|
|
176 |
Mpushstrbloc (m, value);
|
|
177 |
MMpush (m, ITOM (2));
|
|
178 |
MBdeftab (m); /* [S S] */
|
|
179 |
g_free (value);
|
|
180 |
}
|
|
181 |
g_strfreev (keys);
|
|
182 |
MMpush (m, NIL);
|
|
183 |
for (j = 0; j < (int) nkeys; j++)
|
|
184 |
{
|
|
185 |
MMpush (m, ITOM (2));
|
|
186 |
MBdeftab (m);
|
|
187 |
} /* [[S S] r1] */
|
|
188 |
MMpush (m, ITOM (2));
|
|
189 |
MBdeftab (m); /* [S [[S S] r1]] */
|
|
190 |
}
|
|
191 |
g_strfreev (groups);
|
|
192 |
MMpush (m, NIL);
|
|
193 |
for (i = 0; i < (int) ngroups; i++)
|
|
194 |
{
|
|
195 |
MMpush (m, ITOM (2));
|
|
196 |
MBdeftab (m);
|
|
197 |
} /* [[S [[S S] r1]] r1] */
|
|
198 |
return 0;
|
|
199 |
}
|
|
200 |
|
|
155 |
*/ |
|
156 |
int scol_g_key_file_read (GKeyFile *ini, mmachine m) |
|
157 |
{ |
|
158 |
int i, j; |
|
159 |
gsize ngroups, nkeys; |
|
160 |
gchar **groups; |
|
161 |
|
|
162 |
groups = g_key_file_get_groups (ini, &ngroups); |
|
163 |
for (i = 0; i < (int) ngroups; i++) |
|
164 |
{ |
|
165 |
gchar **keys; |
|
166 |
|
|
167 |
Mpushstrbloc (m, groups[i]); /* S */ |
|
168 |
|
|
169 |
keys = g_key_file_get_keys (ini, groups[i], &nkeys, NULL); |
|
170 |
for (j = 0; j < (int) nkeys; j++) |
|
171 |
{ |
|
172 |
gchar *value; |
|
173 |
|
|
174 |
value = g_key_file_get_value (ini, groups[i], keys[j], NULL); |
|
175 |
Mpushstrbloc (m, keys[j]); |
|
176 |
Mpushstrbloc (m, value); |
|
177 |
MMpush (m, ITOM (2)); |
|
178 |
MBdeftab (m); /* [S S] */ |
|
179 |
g_free (value); |
|
180 |
} |
|
181 |
g_strfreev (keys); |
|
182 |
MMpush (m, NIL); |
|
183 |
for (j = 0; j < (int) nkeys; j++) |
|
184 |
{ |
|
185 |
MMpush (m, ITOM (2)); |
|
186 |
MBdeftab (m); |
|
187 |
} /* [[S S] r1] */ |
|
188 |
MMpush (m, ITOM (2)); |
|
189 |
MBdeftab (m); /* [S [[S S] r1]] */ |
|
190 |
} |
|
191 |
g_strfreev (groups); |
|
192 |
MMpush (m, NIL); |
|
193 |
for (i = 0; i < (int) ngroups; i++) |
|
194 |
{ |
|
195 |
MMpush (m, ITOM (2)); |
|
196 |
MBdeftab (m); |
|
197 |
} /* [[S [[S S] r1]] r1] */ |
|
198 |
return 0; |
|
199 |
} |
|
200 |
|
|
201 | 201 |
/** \private |
202 | 202 |
* internal function : load a file or data |
203 |
*/
|
|
204 |
int scol_g_key_file_load (mmachine m, int typ)
|
|
205 |
{
|
|
206 |
int mchannel, mfile, msep, mflag;
|
|
207 |
int initab;
|
|
208 |
gboolean b;
|
|
209 |
GKeyFileFlags flag = G_KEY_FILE_NONE;
|
|
210 |
GKeyFile *ini;
|
|
211 |
GError *err = NULL;
|
|
212 |
|
|
213 |
mflag = MTOI (MMpull (m));
|
|
214 |
msep = MTOP (MMpull (m));
|
|
215 |
mfile = MMpull (m);
|
|
216 |
mchannel = MMget (m, 0);
|
|
217 |
|
|
218 |
if (mchannel == NIL)
|
|
219 |
{
|
|
220 |
MMechostr (0, "scol_g_key_file_load error : channel is nil\n");
|
|
221 |
MMpush (m, NIL);
|
|
222 |
return 0;
|
|
223 |
}
|
|
224 |
if (mfile == NIL)
|
|
225 |
{
|
|
226 |
MMechostr (0, "scol_g_key_file_load error : file is nil\n");
|
|
227 |
MMpush (m, NIL);
|
|
228 |
return 0;
|
|
229 |
}
|
|
230 |
|
|
231 |
ini = g_key_file_new ();
|
|
232 |
if (msep != NIL)
|
|
233 |
g_key_file_set_list_separator (ini, (MMstart (m, msep))[0]);
|
|
234 |
|
|
235 |
if (mflag == 1)
|
|
236 |
flag = G_KEY_FILE_KEEP_COMMENTS;
|
|
237 |
else if (mflag == 2)
|
|
238 |
flag = G_KEY_FILE_KEEP_TRANSLATIONS;
|
|
239 |
else if (mflag == 3)
|
|
240 |
flag = G_KEY_FILE_KEEP_TRANSLATIONS|G_KEY_FILE_KEEP_COMMENTS;
|
|
241 |
|
|
242 |
if (typ)
|
|
243 |
b = g_key_file_load_from_data (ini, MMstartstr (m, MTOP (mfile)), MMsizestr (m, MTOP (mfile)), flag, &err);
|
|
244 |
else
|
|
245 |
b = g_key_file_load_from_file (ini, MMstartstr (m, MTOP (mfile)), flag, &err);
|
|
246 |
if ((!b) || (err != NULL))
|
|
247 |
{
|
|
248 |
MMechostr (0, "scol_g_key_file_load error : loading is failed -> %s\n", err->message);
|
|
249 |
g_error_free (err);
|
|
250 |
MMpush (m, NIL);
|
|
251 |
return 0;
|
|
252 |
}
|
|
253 |
|
|
254 |
initab = MMmalloc (m, sizeof (ini) + 1, TYPETAB);
|
|
255 |
if (initab == NIL)
|
|
256 |
{
|
|
203 |
*/ |
|
204 |
int scol_g_key_file_load (mmachine m, int typ) |
|
205 |
{ |
|
206 |
int mchannel, mfile, msep, mflag; |
|
207 |
int initab; |
|
208 |
gboolean b; |
|
209 |
GKeyFileFlags flag = G_KEY_FILE_NONE; |
|
210 |
GKeyFile *ini; |
|
211 |
GError *err = NULL; |
|
212 |
|
|
213 |
mflag = MTOI (MMpull (m)); |
|
214 |
msep = MTOP (MMpull (m)); |
|
215 |
mfile = MMpull (m); |
|
216 |
mchannel = MMget (m, 0); |
|
217 |
|
|
218 |
if (mchannel == NIL) |
|
219 |
{ |
|
220 |
MMechostr (0, "scol_g_key_file_load error : channel is nil\n"); |
|
221 |
MMpush (m, NIL); |
|
222 |
return 0; |
|
223 |
} |
|
224 |
if (mfile == NIL) |
|
225 |
{ |
|
226 |
MMechostr (0, "scol_g_key_file_load error : file is nil\n"); |
|
227 |
MMpush (m, NIL); |
|
228 |
return 0; |
|
229 |
} |
|
230 |
|
|
231 |
ini = g_key_file_new (); |
|
232 |
if (msep != NIL) |
|
233 |
g_key_file_set_list_separator (ini, (MMstart (m, msep))[0]); |
|
234 |
|
|
235 |
if (mflag == 1) |
|
236 |
flag = G_KEY_FILE_KEEP_COMMENTS; |
|
237 |
else if (mflag == 2) |
|
238 |
flag = G_KEY_FILE_KEEP_TRANSLATIONS; |
|
239 |
else if (mflag == 3) |
|
240 |
flag = G_KEY_FILE_KEEP_TRANSLATIONS|G_KEY_FILE_KEEP_COMMENTS; |
|
241 |
|
|
242 |
if (typ) |
|
243 |
b = g_key_file_load_from_data (ini, MMstartstr (m, MTOP (mfile)), MMsizestr (m, MTOP (mfile)), flag, &err); |
|
244 |
else |
|
245 |
b = g_key_file_load_from_file (ini, MMstartstr (m, MTOP (mfile)), flag, &err); |
|
246 |
if ((!b) || (err != NULL)) |
|
247 |
{ |
|
248 |
MMechostr (0, "scol_g_key_file_load error : loading is failed -> %s\n", err->message); |
|
249 |
g_error_free (err); |
|
250 |
MMpush (m, NIL); |
|
251 |
return 0; |
|
252 |
} |
|
253 |
|
|
254 |
initab = MMmalloc (m, sizeof (ini) + 1, TYPETAB); |
|
255 |
if (initab == NIL) |
|
256 |
{ |
|
257 | 257 |
MMechostr (0, "scol_g_key_file_load error : not enough memory\n"); |
258 | 258 |
MMpull (m); |
259 | 259 |
MMpush (m, NIL); |
260 |
g_key_file_free (ini); |
|
261 |
return 0; |
|
262 |
} |
|
263 |
MMstore (m, initab, OBJGKEYFILE_HANDLE, (int) ini); |
|
264 |
MMpush (m, PTOM (initab)); |
|
265 |
OBJcreate (m, ObjKeyFile, (int) ini, -1, -1); |
|
266 |
return 0; |
|
267 |
} |
|
268 |
|
|
269 |
/** |
|
270 |
* \brief _gkeyFileLoadP : Loads a key file from any valid file |
|
271 |
* |
|
272 |
* fun [Chn P S I] ObjKeyFile |
|
273 |
* |
|
274 |
* \param Chn : the channel |
|
275 |
* \param P : any read referenced file |
|
276 |
* \param S : the list separator or nil (default ";"). One character only (if more, they are ignored) |
|
277 |
* \param I : flag : |
|
278 |
* 0 -> No flags, default behaviour |
|
279 |
* 1 -> Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; |
|
280 |
* otherwise all comments will be lost when the key file is written back. |
|
281 |
* 2 -> Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; |
|
282 |
* otherwise only the translations for the current language will be written back. |
|
283 |
* 3 -> 1 and 2 |
|
284 |
* \return ObjKeyFile : the new object, or nil if error |
|
285 |
* |
|
286 |
* Importante note : if you want get/set any localized key, be sure set the flag to 2 (or 3) ! |
|
287 |
*/ |
|
288 |
int SCOL_gkeyFileLoadP (mmachine m) |
|
289 |
{ |
|
290 |
return scol_g_key_file_load (m, 0); |
|
291 |
} |
|
292 |
|
|
293 |
/** |
|
294 |
* \brief _gkeyFileLoadS : Loads a key file from any valid data |
|
295 |
* |
|
296 |
* fun [Chn P S I] ObjKeyFile |
|
297 |
* |
|
298 |
* \param Chn : the channel |
|
299 |
* \param S : any valid content |
|
300 |
* \param S : the list separator or nil (default ";"). One character only (if more, they are ignored) |
|
301 |
* \param I : flag : |
|
302 |
* 0 -> No flags, default behaviour |
|
303 |
* 1 -> Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; |
|
304 |
* otherwise all comments will be lost when the key file is written back. |
|
305 |
* 2 -> Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; |
|
306 |
* otherwise only the translations for the current language will be written back. |
|
307 |
* 3 -> 1 and 2 |
|
308 |
* \return ObjKeyFile : the new object, or nil if error |
|
309 |
*/ |
|
310 |
int SCOL_gkeyFileLoadS (mmachine m) |
|
311 |
{ |
|
312 |
return scol_g_key_file_load (m, 1); |
|
260 |
g_key_file_free (ini); |
|
261 |
return 0; |
|
262 |
} |
|
263 |
MMstore (m, initab, OBJGKEYFILE_HANDLE, (int) ini); |
|
264 |
MMpush (m, PTOM (initab)); |
|
265 |
OBJcreate (m, ObjKeyFile, (int) ini, -1, -1); |
|
266 |
return 0; |
|
313 | 267 |
} |
314 |
|
|
315 |
|
|
316 |
/** |
|
317 |
* \brief _gkeyFileSave : save a keyfile to a filename |
|
318 |
* |
|
319 |
* fun [ObjKeyFile W] I |
|
320 |
* |
|
321 |
* \param ObjKeyFile : a valid object |
|
322 |
* \param W : a Scol write referenced filename |
|
323 |
* \return I : 0 if success or nil if error (see log message) |
|
324 |
*/ |
|
325 |
int SCOL_gkeyFileSave (mmachine m) |
|
326 |
{ |
|
327 |
int mini, mwfile; |
|
328 |
gboolean b; |
|
329 |
GKeyFile *ini; |
|
330 |
gchar *content, *wfile; |
|
331 |
gsize len = 0; |
|
332 |
GError *err = NULL; |
|
333 |
|
|
334 |
mwfile = MMpull (m); |
|
335 |
mini = MMpull (m); |
|
336 |
|
|
337 |
if ((mini == NIL) || (mwfile == NIL)) |
|
338 |
{ |
|
339 |
MMechostr (0, "SCOL_gkeyFileSave error : bad(s) argument(s) !\n"); |
|
340 |
MMpush (m, NIL); |
|
341 |
return 0; |
|
342 |
} |
|
343 |
|
|
344 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
345 |
wfile = MMstartstr (m, MTOP (mwfile)); |
|
346 |
content = g_key_file_to_data (ini, &len, &err); |
|
347 |
if ((content == NULL) || (err != NULL)) |
|
348 |
{ |
|
349 |
MMechostr (0, "SCOL_gkeyFileSave error : %d -> %s !\n", err->code, err->message); |
|
350 |
g_error_free (err); |
|
351 |
MMpush (m, NIL); |
|
352 |
return 0; |
|
353 |
} |
|
354 |
b = g_file_set_contents (wfile, content, len, &err); |
|
355 |
if ((!b) || (err != NULL)) |
|
356 |
{ |
|
357 |
MMechostr (0, "SCOL_gkeyFileSave error : %d -> %s !\n", err->code, err->message); |
|
358 |
g_error_free (err); |
|
359 |
MMpush (m, NIL); |
|
360 |
return 0; |
|
361 |
} |
|
362 |
g_free (content); |
|
363 |
MMpush (m, ITOM (0)); |
|
364 |
return 0; |
|
365 |
} |
|
366 |
|
|
367 |
/** |
|
368 |
*\brief _gkeyFileDestroy : Destroys an object |
|
369 |
* |
|
370 |
* \param ObjKeyFile |
|
371 |
* \return I : O or nil if error |
|
372 |
*/ |
|
373 |
int SCOL_gkeyFileDestroy (mmachine m) |
|
374 |
{ |
|
375 |
int mini; |
|
376 |
GKeyFile *ini; |
|
377 |
|
|
378 |
mini = MMpull (m); |
|
379 |
if (mini == NIL) |
|
380 |
{ |
|
268 |
|
|
269 |
/** |
|
270 |
* \brief _gkeyFileLoadP : Loads a key file from any valid file |
|
271 |
* |
|
272 |
* fun [Chn P S I] ObjKeyFile |
|
273 |
* |
|
274 |
* \param Chn : the channel |
|
275 |
* \param P : any read referenced file |
|
276 |
* \param S : the list separator or nil (default ";"). One character only (if more, they are ignored) |
|
277 |
* \param I : flag : |
|
278 |
* 0 -> No flags, default behaviour |
|
279 |
* 1 -> Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; |
|
280 |
* otherwise all comments will be lost when the key file is written back. |
|
281 |
* 2 -> Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; |
|
282 |
* otherwise only the translations for the current language will be written back. |
|
283 |
* 3 -> 1 and 2 |
|
284 |
* \return ObjKeyFile : the new object, or nil if error |
|
285 |
* |
|
286 |
* Importante note : if you want get/set any localized key, be sure set the flag to 2 (or 3) ! |
|
287 |
*/ |
|
288 |
int SCOL_gkeyFileLoadP (mmachine m) |
|
289 |
{ |
|
290 |
return scol_g_key_file_load (m, 0); |
|
291 |
} |
|
292 |
|
|
293 |
/** |
|
294 |
* \brief _gkeyFileLoadS : Loads a key file from any valid data |
|
295 |
* |
|
296 |
* fun [Chn P S I] ObjKeyFile |
|
297 |
* |
|
298 |
* \param Chn : the channel |
|
299 |
* \param S : any valid content |
|
300 |
* \param S : the list separator or nil (default ";"). One character only (if more, they are ignored) |
|
301 |
* \param I : flag : |
|
302 |
* 0 -> No flags, default behaviour |
|
303 |
* 1 -> Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; |
|
304 |
* otherwise all comments will be lost when the key file is written back. |
|
305 |
* 2 -> Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; |
|
306 |
* otherwise only the translations for the current language will be written back. |
|
307 |
* 3 -> 1 and 2 |
|
308 |
* \return ObjKeyFile : the new object, or nil if error |
|
309 |
*/ |
|
310 |
int SCOL_gkeyFileLoadS (mmachine m) |
|
311 |
{ |
|
312 |
return scol_g_key_file_load (m, 1); |
|
313 |
} |
|
314 |
|
|
315 |
|
|
316 |
/** |
|
317 |
* \brief _gkeyFileSave : save a keyfile to a filename |
|
318 |
* |
|
319 |
* fun [ObjKeyFile W] I |
|
320 |
* |
|
321 |
* \param ObjKeyFile : a valid object |
|
322 |
* \param W : a Scol write referenced filename |
|
323 |
* \return I : 0 if success or nil if error (see log message) |
|
324 |
*/ |
|
325 |
int SCOL_gkeyFileSave (mmachine m) |
|
326 |
{ |
|
327 |
int mini, mwfile; |
|
328 |
gboolean b; |
|
329 |
GKeyFile *ini; |
|
330 |
gchar *content, *wfile; |
|
331 |
gsize len = 0; |
|
332 |
GError *err = NULL; |
|
333 |
|
|
334 |
mwfile = MMpull (m); |
|
335 |
mini = MMpull (m); |
|
336 |
|
|
337 |
if ((mini == NIL) || (mwfile == NIL)) |
|
338 |
{ |
|
339 |
MMechostr (0, "SCOL_gkeyFileSave error : bad(s) argument(s) !\n"); |
|
340 |
MMpush (m, NIL); |
|
341 |
return 0; |
|
342 |
} |
|
343 |
|
|
344 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
345 |
wfile = MMstartstr (m, MTOP (mwfile)); |
|
346 |
content = g_key_file_to_data (ini, &len, &err); |
|
347 |
if ((content == NULL) || (err != NULL)) |
|
348 |
{ |
|
349 |
MMechostr (0, "SCOL_gkeyFileSave error : %d -> %s !\n", err->code, err->message); |
|
350 |
g_error_free (err); |
|
351 |
MMpush (m, NIL); |
|
352 |
return 0; |
|
353 |
} |
|
354 |
b = g_file_set_contents (wfile, content, len, &err); |
|
355 |
if ((!b) || (err != NULL)) |
|
356 |
{ |
|
357 |
MMechostr (0, "SCOL_gkeyFileSave error : %d -> %s !\n", err->code, err->message); |
|
358 |
g_error_free (err); |
|
359 |
MMpush (m, NIL); |
|
360 |
return 0; |
|
361 |
} |
|
362 |
g_free (content); |
|
363 |
MMpush (m, ITOM (0)); |
|
364 |
return 0; |
|
365 |
} |
|
366 |
|
|
367 |
/** |
|
368 |
*\brief _gkeyFileDestroy : Destroys an object |
|
369 |
* |
|
370 |
* \param ObjKeyFile |
|
371 |
* \return I : O or nil if error |
|
372 |
*/ |
|
373 |
int SCOL_gkeyFileDestroy (mmachine m) |
|
374 |
{ |
|
375 |
int mini; |
|
376 |
GKeyFile *ini; |
|
377 |
|
|
378 |
mini = MMpull (m); |
|
379 |
if (mini == NIL) |
|
380 |
{ |
|
381 | 381 |
MMechostr (0, "SCOL_gkeyFileDestroy error : object is nil\n"); |
382 |
MMpush (m, NIL);
|
|
383 |
return 0;
|
|
384 |
}
|
|
385 |
|
|
386 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE);
|
|
387 |
g_key_file_free (ini);
|
|
388 |
OBJdelTM (m, ObjKeyFile, mini);
|
|
389 |
MMpush (m, 0);
|
|
390 |
|
|
391 |
return 0;
|
|
392 |
}
|
|
393 |
|
|
394 |
/**
|
|
395 |
* \brief _gkeyFileGetContent : Returns the content of key file
|
|
396 |
*
|
|
397 |
* fun [ObjKeyFile] [S I]
|
|
398 |
*
|
|
399 |
* \param ObjKeyFile : any valid object
|
|
400 |
* \return [S I] : a tuple : the content and its length or nil if error
|
|
401 |
*/
|
|
402 |
int SCOL_gkeyFileGetContent (mmachine m)
|
|
403 |
{
|
|
404 |
int mini;
|
|
405 |
gchar *content;
|
|
406 |
gsize len = 0;
|
|
407 |
GKeyFile *ini;
|
|
408 |
|
|
409 |
mini = MMpull (m);
|
|
410 |
if (mini == NIL)
|
|
411 |
{
|
|
382 |
MMpush (m, NIL); |
|
383 |
return 0; |
|
384 |
} |
|
385 |
|
|
386 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
387 |
g_key_file_free (ini); |
|
388 |
OBJdelTM (m, ObjKeyFile, mini); |
|
389 |
MMpush (m, 0); |
|
390 |
|
|
391 |
return 0; |
|
392 |
} |
|
393 |
|
|
394 |
/** |
|
395 |
* \brief _gkeyFileGetContent : Returns the content of key file |
|
396 |
* |
|
397 |
* fun [ObjKeyFile] [S I] |
|
398 |
* |
|
399 |
* \param ObjKeyFile : any valid object |
|
400 |
* \return [S I] : a tuple : the content and its length or nil if error |
|
401 |
*/ |
|
402 |
int SCOL_gkeyFileGetContent (mmachine m) |
|
403 |
{ |
|
404 |
int mini; |
|
405 |
gchar *content; |
|
406 |
gsize len = 0; |
|
407 |
GKeyFile *ini; |
|
408 |
|
|
409 |
mini = MMpull (m); |
|
410 |
if (mini == NIL) |
|
411 |
{ |
|
412 | 412 |
MMechostr (0, "SCOL_gkeyFileGetContent error : object is nil\n"); |
413 |
MMpush (m, NIL);
|
|
414 |
return 0;
|
|
415 |
}
|
|
416 |
|
|
417 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE);
|
|
418 |
content = g_key_file_to_data (ini, &len, NULL);
|
|
419 |
Mpushstrbloc (m, content);
|
|
420 |
MMpush (m, ITOM ((int) len));
|
|
421 |
MMpush (m, ITOM (2));
|
|
422 |
MBdeftab (m);
|
|
423 |
g_free (content);
|
|
424 |
return 0;
|
|
425 |
}
|
|
426 |
|
|
427 |
/**
|
|
428 |
* \brief _gkeyFileGetDatas : Return a list of the content
|
|
429 |
*
|
|
430 |
* fun [ObjKeyFile] [[S [[S S] r1]] r1]
|
|
431 |
*
|
|
432 |
* \param ObjKeyFile : an object already created
|
|
433 |
* \return [[S [[S S] r1]] r1] : a list of group names and for each group name, a list of tuple [key value] or nil if error
|
|
434 |
*/
|
|
435 |
int SCOL_gkeyFileGetDatas (mmachine m)
|
|
436 |
{
|
|
437 |
int mini;
|
|
438 |
GKeyFile *ini;
|
|
439 |
|
|
440 |
mini = MMpull (m);
|
|
441 |
if (mini == NIL)
|
|
442 |
{
|
|
413 |
MMpush (m, NIL); |
|
414 |
return 0; |
|
415 |
} |
|
416 |
|
|
417 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
418 |
content = g_key_file_to_data (ini, &len, NULL); |
|
419 |
Mpushstrbloc (m, content); |
|
420 |
MMpush (m, ITOM ((int) len)); |
|
421 |
MMpush (m, ITOM (2)); |
|
422 |
MBdeftab (m); |
|
423 |
g_free (content); |
|
424 |
return 0; |
|
425 |
} |
|
426 |
|
|
427 |
/** |
|
428 |
* \brief _gkeyFileGetDatas : Return a list of the content |
|
429 |
* |
|
430 |
* fun [ObjKeyFile] [[S [[S S] r1]] r1] |
|
431 |
* |
|
432 |
* \param ObjKeyFile : an object already created |
|
433 |
* \return [[S [[S S] r1]] r1] : a list of group names and for each group name, a list of tuple [key value] or nil if error |
|
434 |
*/ |
|
435 |
int SCOL_gkeyFileGetDatas (mmachine m) |
|
436 |
{ |
|
437 |
int mini; |
|
438 |
GKeyFile *ini; |
|
439 |
|
|
440 |
mini = MMpull (m); |
|
441 |
if (mini == NIL) |
|
442 |
{ |
|
443 | 443 |
MMechostr (0, "SCOL_gkeyFileGetDatas error : object is nil\n"); |
444 |
MMpush (m, NIL); |
|
445 |
return 0; |
|
446 |
} |
|
447 |
|
|
448 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
449 |
return scol_g_key_file_read (ini, m); |
|
450 |
} |
|
451 |
|
|
452 |
/** |
|
453 |
* \brief _gkeyFileGetGroups : Returns a list of all groups |
|
454 |
* |
|
455 |
* fun [ObjKeyFile] [S r1] |
|
456 |
* |
|
457 |
* \param ObjKeyFile : any object already created |
|
458 |
* \return [S r1] : this list or nil if error |
|
459 |
*/ |
|
460 |
int SCOL_gkeyFileGetGroups (mmachine m) |
|
461 |
{ |
|
462 |
return scol_g_key_file_get_groups (m, 0); |
|
463 |
} |
|
464 |
|
|
465 |
/** |
|
466 |
* \brief _gkeyFileGetNbGroups : Returns the number of group |
|
467 |
* |
|
468 |
* fun [ObjKeyFile] I |
|
469 |
* |
|
470 |
* \param ObjKeyFile : any object already created |
|
471 |
* \return I : this number or nil if error |
|
472 |
*/ |
|
473 |
int SCOL_gkeyFileGetNbGroups (mmachine m) |
|
474 |
{ |
|
475 |
return scol_g_key_file_get_groups (m, 1); |
|
476 |
} |
|
477 |
|
|
478 |
/** |
|
479 |
* \brief _gkeyFileDataExist : Looks whether the key file has the datas |
|
480 |
* |
|
481 |
* fun [ObjKeyFile S S] I |
|
482 |
* |
|
483 |
* \param ObjKeyFile : any object already created |
|
484 |
* \param S : a group name or nil |
|
485 |
* \param S : a key name or nil |
|
486 |
* \return I : the result (see below) or nil if error |
|
487 |
* |
|
488 |
* If the group name and the key name are not nil, this function searches the key into the group. |
|
489 |
* If found, 1 is returned, else 0 |
|
490 |
* If the group name is nil, this function searches the key into all groups. |
|
491 |
* The returned value is the number of this key, 0 if not found. |
|
492 |
* If the key name is nil, this function searches the group into the keyfile. |
|
493 |
* If found, 1 is returned, else 0. |
|
494 |
* If the keyfile is nil or if the group and key names are nil, nil is returned. |
|
495 |
*/ |
|
496 |
int SCOL_gkeyFileDataExist (mmachine m) |
|
497 |
{ |
|
498 |
int mini, mgroup, mkey; |
|
499 |
GKeyFile *ini; |
|
500 |
gboolean b; |
|
501 |
|
|
502 |
mkey = MMpull (m); |
|
503 |
mgroup = MMpull (m); |
|
504 |
mini = MMpull (m); |
|
505 |
|
|
506 |
if (mini == NIL) |
|
507 |
{ |
|
508 |
MMechostr (0, "SCOL_gkeyFileDataExist error : object is nil\n"); |
|
509 |
MMpush (m, NIL); |
|
510 |
return 0; |
|
511 |
} |
|
512 |
|
|
513 |
if ((mkey == NIL) && (mgroup == NIL)) |
|
514 |
{ |
|
515 |
MMechostr (0, "SCOL_gkeyFileDataExist error : group and key are nil\n"); |
|
516 |
MMpush (m, NIL); |
|
517 |
return 0; |
|
518 |
} |
|
519 |
|
|
520 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
521 |
|
|
522 |
if (mkey == NIL) /* search a group only */ |
|
523 |
{ |
|
524 |
b = g_key_file_has_group (ini, MMstartstr (m, MTOP (mgroup))); |
|
525 |
MMpush (m, ITOM (b)); |
|
526 |
return 0; |
|
527 |
} |
|
528 |
|
|
529 |
if (mgroup == NIL) /* search a key in all groups */ |
|
530 |
{ |
|
531 |
gchar ** groups; |
|
532 |
gsize len = 0; |
|
533 |
int i, r = 0; |
|
534 |
|
|
535 |
groups = g_key_file_get_groups (ini, &len); |
|
536 |
for (i = 0; i < len; i++) |
|
537 |
{ |
|
538 |
if (g_key_file_has_key (ini, groups[i], MMstartstr (m, MTOP (mkey)), NULL)) |
|
539 |
r++; |
|
540 |
} |
|
541 |
MMpush (m, ITOM (r)); |
|
542 |
g_strfreev (groups); |
|
543 |
return 0; |
|
544 |
} |
|
545 |
|
|
546 |
/* search a key in a group */ |
|
547 |
b = g_key_file_has_key (ini, MMstartstr (m, MTOP (mgroup)), MMstartstr (m, MTOP (mkey)), NULL); |
|
548 |
MMpush (m, ITOM (b)); |
|
549 |
return 0; |
|
550 |
} |
|
551 |
|
|
552 |
/** |
|
553 |
* \brief _keyFileGetValue : Returns the value for any key in any group |
|
554 |
* |
|
555 |
* fun [ObjKeyFile S S I] S |
|
556 |
* |
|
557 |
* \param ObjKeyFile : any valid object |
|
558 |
* \param S : any group name |
|
559 |
* \param S : any key name |
|
560 |
* \param I : flag : GKF_VALUE_RAW, GKF_VALUE_STRING, GKF_VALUE_INTEGER, GKF_VALUE_INTEGER64, GKF_VALUE_UINTEGER64, GKF_VALUE_DOUBLE, GKF_VALUE_BOOLEAN |
|
561 |
* GKF_VALUE_RAW and GKF_VALUE_STRING have a difference, others always return a string |
|
562 |
* \return S : the value or nil if error or not found |
|
563 |
*/ |
|
564 |
int SCOL_keyFileGetValue (mmachine m) |
|
565 |
{ |
|
566 |
int mini, mgroup, mkey, mflag; |
|
567 |
GKeyFile *ini; |
|
568 |
gchar *value, *group, *key; |
|
569 |
GError *err = NULL; |
|
570 |
|
|
571 |
mflag = MTOI (MMpull (m)); |
|
572 |
mkey = MMpull (m); |
|
573 |
mgroup = MMpull (m); |
|
574 |
mini = MMpull (m); |
|
575 |
|
|
576 |
if ((mini == NIL) || (mkey == NIL) || (mgroup == NIL)) |
|
577 |
{ |
|
578 |
MMechostr (0, "SCOL_keyFileGetValue error : bad(s) argument(s) !\n"); |
|
579 |
MMpush (m, NIL); |
|
580 |
return 0; |
|
581 |
} |
|
582 |
|
|
583 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
584 |
group = MMstartstr (m, MTOP (mgroup)); |
|
585 |
key = MMstartstr (m, MTOP (mkey)); |
|
586 |
|
|
587 |
switch (mflag) |
|
588 |
{ |
|
589 |
case (2) : |
|
590 |
value = g_key_file_get_string (ini, group, key, &err); |
|
591 |
break; |
|
592 |
|
|
593 |
case (3) : |
|
594 |
{ |
|
595 |
int v; |
|
596 |
|
|
597 |
v = g_key_file_get_integer (ini, group, key, &err); |
|
598 |
value = g_malloc (sizeof (gchar) * 64); |
|
599 |
snprintf (value, 64, "%d", v); |
|
600 |
break; |
|
601 |
} |
|
602 |
#ifndef GLIB_224 |
|
603 |
case (4) : |
|
604 |
{ |
|
605 |
gint64 v; |
|
606 |
|
|
607 |
v = g_key_file_get_int64 (ini, group, key, &err); |
|
608 |
value = g_malloc (sizeof (gchar) * 64); |
|
609 |
snprintf (value, 64, "%" G_GINT64_MODIFIER "x", v); |
|
610 |
break; |
|
611 |
} |
|
612 |
|
|
613 |
case (5) : |
|
614 |
{ |
|
615 |
guint64 v; |
|
616 |
|
|
617 |
v = g_key_file_get_uint64 (ini, group, key, &err); |
|
618 |
value = g_malloc (sizeof (gchar) * 64); |
|
619 |
snprintf (value, 64, "%" G_GUINT64_FORMAT "x", v); |
|
620 |
break; |
|
621 |
} |
|
622 |
#endif |
|
623 |
case (6) : |
|
624 |
{ |
|
625 |
double v; |
|
626 |
|
|
627 |
v = g_key_file_get_double (ini, group, key, &err); |
|
628 |
value = g_malloc (sizeof (gchar) * 64); |
|
629 |
snprintf (value, 64, "%f", v); |
|
630 |
break; |
|
631 |
} |
|
632 |
|
|
633 |
case (7) : |
|
634 |
{ |
|
635 |
gboolean v; |
|
636 |
|
|
637 |
v = g_key_file_get_boolean (ini, group, key, &err); |
|
638 |
value = g_malloc (sizeof (gchar) * 64); |
|
639 |
snprintf (value, 64, "%d", v); |
|
640 |
break; |
|
641 |
} |
|
642 |
|
|
643 |
default : |
|
644 |
value = g_key_file_get_value (ini, group, key, &err); |
|
645 |
|
|
646 |
} |
|
647 |
if ((value == NULL) || (err != NULL)) |
|
648 |
{ |
|
649 |
MMechostr (0, "SCOL_keyFileGetValue error : %d -> %s !\n", err->code, err->message); |
|
650 |
g_error_free (err); |
|
651 |
MMpush (m, NIL); |
|
652 |
} |
|
653 |
else |
|
654 |
{ |
|
655 |
Mpushstrbloc (m, value); |
|
656 |
g_free (value); |
|
657 |
} |
|
658 |
return 0; |
|
659 |
} |
|
660 |
|
|
661 |
/** |
|
662 |
* \brief _keyFileGetLocaleValue : Returns the locale value for any key in any group |
|
663 |
* |
|
664 |
* fun [ObjKeyFile S S S] S |
|
665 |
* |
|
666 |
* \param ObjKeyFile : any valid object |
|
667 |
* \param S : any group name |
|
668 |
* \param S : any key name |
|
669 |
* \param S : any locale identifier or nil |
|
670 |
* \return S : the value or nil if error or not found |
|
671 |
* |
|
672 |
* If locale is nil or not found, the value of the default key (without localization) will be returned |
|
673 |
* When the ObjKeyFile has been created, if the flag is at 0 or 1, the current locale can be used only. |
|
674 |
* In this last case, to get a value of an other locale, the flag should have set to 2 or 3. |
|
675 |
*/ |
|
676 |
int SCOL_keyFileGetLocaleValue (mmachine m) |
|
677 |
{ |
|
678 |
int mini, mgroup, mkey, mlang; |
|
679 |
GKeyFile *ini; |
|
680 |
gchar *value, *group, *key, *lang = NULL; |
|
681 |
GError *err = NULL; |
|
682 |
|
|
683 |
mlang = MMpull (m); |
|
684 |
mkey = MMpull (m); |
|
685 |
mgroup = MMpull (m); |
|
686 |
mini = MMpull (m); |
|
687 |
|
|
688 |
if ((mini == NIL) || (mkey == NIL) || (mgroup == NIL)) |
|
689 |
{ |
|
690 |
MMechostr (0, "SCOL_keyFileGetLocaleValue error : bad(s) argument(s) !\n"); |
|
691 |
MMpush (m, NIL); |
|
692 |
return 0; |
|
693 |
} |
|
694 |
|
|
695 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
696 |
group = MMstartstr (m, MTOP (mgroup)); |
|
697 |
key = MMstartstr (m, MTOP (mkey)); |
|
698 |
if (mlang != NIL) |
|
699 |
lang = MMstartstr (m, MTOP (mlang)); |
|
700 |
|
|
701 |
value = g_key_file_get_locale_string (ini, group, key, lang, &err); |
|
702 |
if ((value == NULL) || (err != NULL)) |
|
703 |
{ |
|
704 |
MMechostr (0, "SCOL_keyFileGetLocaleValue error : %d -> %s !\n", err->code, err->message); |
|
705 |
g_error_free (err); |
|
706 |
MMpush (m, NIL); |
|
707 |
} |
|
708 |
|
|
709 |
Mpushstrbloc (m, value); |
|
710 |
g_free (value); |
|
711 |
return 0; |
|
712 |
} |
|
713 |
|
|
714 |
/** |
|
715 |
* \brief _gkeyFileGetValueList : Returns the values associated with key under group |
|
716 |
* |
|
717 |
* fun [ObjKeyFile S S] [S r1] |
|
718 |
* |
|
719 |
* \param ObjKeyFile : any valid object |
|
720 |
* \param S : any group name |
|
721 |
* \param S : any key name |
|
722 |
* \return [S r1] : the value or nil if error or not found |
|
723 |
*/ |
|
724 |
int SCOL_gkeyFileGetValueList (mmachine m) |
|
725 |
{ |
|
726 |
int mini, mgroup, mkey; |
|
727 |
int i; |
|
728 |
GKeyFile *ini; |
|
729 |
gchar *group, *key; |
|
730 |
gchar **list; |
|
731 |
gsize len = 0; |
|
732 |
GError *err = NULL; |
|
733 |
|
|
734 |
mkey = MMpull (m); |
|
735 |
mgroup = MMpull (m); |
|
736 |
mini = MMpull (m); |
|
737 |
|
|
738 |
if ((mini == NIL) || (mkey == NIL) || (mgroup == NIL)) |
|
739 |
{ |
|
740 |
MMechostr (0, "SCOL_gkeyFileGetValueList error : bad(s) argument(s) !\n"); |
|
741 |
MMpush (m, NIL); |
|
742 |
return 0; |
|
743 |
} |
|
744 |
|
|
745 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
746 |
group = MMstartstr (m, MTOP (mgroup)); |
|
747 |
key = MMstartstr (m, MTOP (mkey)); |
|
748 |
|
|
749 |
list = g_key_file_get_string_list (ini, group, key, &len, &err); |
|
750 |
if ((list == NULL) || (err != NULL)) |
|
751 |
{ |
|
752 |
MMechostr (0, "SCOL_gkeyFileGetValueList error : %d -> %s !\n", err->code, err->message); |
|
753 |
g_error_free (err); |
|
754 |
MMpush (m, NIL); |
|
755 |
} |
|
756 |
|
|
757 |
for (i = 0; i < (int) len; i++) |
|
758 |
Mpushstrbloc (m, list[i]); |
|
759 |
MMpush (m, NIL); |
|
760 |
for (i = 0; i < (int) len; i++) |
|
761 |
{ |
|
762 |
MMpush (m, ITOM (2)); |
|
763 |
MBdeftab (m); |
|
764 |
} |
|
765 |
g_strfreev (list); |
|
766 |
return 0; |
|
767 |
} |
|
768 |
|
|
769 |
/** |
|
770 |
* \brief _gkeyFileGetValueLocaleList : Returns the locales values associated with key under group |
|
771 |
* |
|
772 |
* fun [ObjKeyFile S S S] [S r1] |
|
773 |
* |
|
774 |
* \param ObjKeyFile : any valid object |
|
775 |
* \param S : any group name |
|
776 |
* \param S : any key name |
|
777 |
* \param S : any locale identifier or nil |
|
778 |
* \return [S r1] : the value or nil if error or not found |
|
779 |
* |
|
780 |
* If locale is nil or not found, the values of the default key (without localization) will be returned |
|
781 |
* When the ObjKeyFile has been created, if the flag is at 0 or 1, the current locale can be used only. |
|
782 |
* In this last case, to get the values of an other locale, the flag should have set to 2 or 3. |
|
783 |
*/ |
|
784 |
int SCOL_gkeyFileGetValueLocaleList (mmachine m) |
|
785 |
{ |
|
786 |
int mini, mgroup, mkey, mlang; |
|
787 |
int i; |
|
788 |
GKeyFile *ini; |
|
789 |
gchar *group, *key, *lang = NULL; |
|
790 |
gchar **list; |
|
791 |
gsize len = 0; |
|
792 |
GError *err = NULL; |
|
793 |
|
|
794 |
mlang = MMpull (m); |
|
795 |
mkey = MMpull (m); |
|
796 |
mgroup = MMpull (m); |
|
797 |
mini = MMpull (m); |
|
798 |
|
|
799 |
if ((mini == NIL) || (mkey == NIL) || (mgroup == NIL)) |
|
800 |
{ |
|
801 |
MMechostr (0, "SCOL_gkeyFileGetValueLocaleList error : bad(s) argument(s) !\n"); |
|
802 |
MMpush (m, NIL); |
|
803 |
return 0; |
|
804 |
} |
|
805 |
|
|
806 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
807 |
group = MMstartstr (m, MTOP (mgroup)); |
|
808 |
key = MMstartstr (m, MTOP (mkey)); |
|
809 |
if (mlang != NIL) |
|
810 |
lang = MMstartstr (m, MTOP (mlang)); |
|
811 |
|
|
812 |
list = g_key_file_get_locale_string_list (ini, group, key, lang, &len, &err); |
|
813 |
if ((list == NULL) || (err != NULL)) |
|
814 |
{ |
|
815 |
MMechostr (0, "SCOL_gkeyFileGetValueLocaleList error : %d -> %s !\n", err->code, err->message); |
|
816 |
g_error_free (err); |
|
817 |
MMpush (m, NIL); |
|
818 |
} |
|
819 |
|
|
820 |
for (i = 0; i < (int) len; i++) |
|
821 |
Mpushstrbloc (m, list[i]); |
|
822 |
MMpush (m, NIL); |
|
823 |
for (i = 0; i < (int) len; i++) |
|
824 |
{ |
|
825 |
MMpush (m, ITOM (2)); |
|
826 |
MBdeftab (m); |
|
827 |
} |
|
828 |
g_strfreev (list); |
|
829 |
return 0; |
|
830 |
} |
|
831 |
|
|
832 |
/** |
|
833 |
* \brief _gkeyFileGetComment : Return a comment |
|
834 |
* |
|
835 |
* fun [ObjKeyFile S S] S |
|
836 |
* |
|
837 |
* \param ObjKeyFile : an object |
|
838 |
* \param S : a group name |
|
839 |
* \param S : a key name |
|
840 |
* \return S : the comment or nil if error or not found |
|
841 |
* |
|
842 |
* Retrieves a comment above key name from group name. If key is NULL then comment will be read from above group name. |
|
843 |
* If both key name and group name are NULL, then comment will be read from above the first group in the file. |
|
844 |
*/ |
|
845 |
int SCOL_gkeyFileGetComment (mmachine m) |
|
846 |
{ |
|
847 |
int mini, mgroup, mkey; |
|
848 |
GKeyFile *ini; |
|
849 |
gchar *group = NULL, *key = NULL, *comment; |
|
850 |
GError *err = NULL; |
|
851 |
|
|
852 |
mkey = MMpull (m); |
|
853 |
mgroup = MMpull (m); |
|
854 |
mini = MMpull (m); |
|
855 |
|
|
856 |
if (mini == NIL) |
|
857 |
{ |
|
858 |
MMechostr (0, "SCOL_gkeyFileGetComment error : bad(s) argument(s) !\n"); |
|
859 |
MMpush (m, NIL); |
|
860 |
return 0; |
|
861 |
} |
|
862 |
|
|
863 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
864 |
if (mgroup != NIL) |
|
865 |
group = MMstartstr (m, MTOP (mgroup)); |
|
866 |
if (mkey != NIL) |
|
867 |
key = MMstartstr (m, MTOP (mkey)); |
|
868 |
|
|
869 |
comment = g_key_file_get_comment (ini, group, key, &err); |
|
870 |
if (err != NULL) |
|
871 |
{ |
|
872 |
MMechostr (0, "SCOL_gkeyFileGetComment error : %d -> %s !\n", err->code, err->message); |
|
873 |
g_error_free (err); |
|
874 |
MMpush (m, NIL); |
|
875 |
} |
|
876 |
Mpushstrbloc (m, comment); |
|
877 |
g_free (comment); |
|
878 |
return 0; |
|
879 |
} |
|
880 |
|
|
881 |
/** |
|
882 |
* \brief _gkeyFileSetValue : Associates a new value with key under group. |
|
883 |
* |
|
884 |
* Fun [ObjKeyFile S S S I] ObjKeyFile |
|
885 |
* If key cannot be found then it is created. If group cannot be found then it is created. |
|
886 |
* |
|
887 |
* \param ObjKeyFile : a valid object |
|
888 |
* \param S : a group name |
|
889 |
* \param S : a key name |
|
890 |
* \param S : the new value |
|
891 |
* \param I : a flag : GKF_VALUE_RAW or GKF_VALUE_STRING (others flags aren't implemented yet) |
|
892 |
* \return ObjKeyFile : the same object or nil if error |
|
893 |
* |
|
894 |
* If key or group is nil, the command is ignored but a message is written to the log |
|
895 |
*/ |
|
896 |
int SCOL_gkeyFileSetValue (mmachine m) |
|
897 |
{ |
|
898 |
int mini, mgroup, mkey, mvalue, mflag; |
|
899 |
GKeyFile *ini; |
|
900 |
gchar *group = NULL, *key = NULL, *value = NULL; |
|
901 |
|
|
902 |
mflag = MTOI (MMpull (m)); |
|
903 |
mvalue = MMpull (m); |
|
904 |
mkey = MMpull (m); |
|
905 |
mgroup = MMpull (m); |
|
906 |
mini = MMpull (m); |
|
907 |
|
|
908 |
if ((mini == NIL) || (mvalue == NIL)) |
|
909 |
{ |
|
910 |
MMechostr (0, "SCOL_gkeyFileSetValue error : bad(s) argument(s) !\n"); |
|
911 |
MMpush (m, NIL); |
|
912 |
return 0; |
|
913 |
} |
|
914 |
|
|
915 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
916 |
if (mgroup != NIL) |
|
917 |
group = MMstartstr (m, MTOP (mgroup)); |
|
918 |
if (mkey != NIL) |
|
919 |
key = MMstartstr (m, MTOP (mkey)); |
|
920 |
if (mvalue != NIL) |
|
921 |
value = MMstartstr (m, MTOP (mvalue)); |
|
922 |
|
|
923 |
switch (mflag) |
|
924 |
{ |
|
925 |
case (2) : |
|
926 |
g_key_file_set_string (ini, group, key, SCOLUTF8 (value, -1)); |
|
927 |
break; |
|
928 |
|
|
929 |
default : |
|
930 |
g_key_file_set_value (ini, group, key, value); |
|
931 |
} |
|
932 |
MMpush (m, mini); |
|
933 |
return 0; |
|
934 |
} |
|
935 |
/** |
|
936 |
* \brief _gkeyFileSetValue : Associates a new value with key under group. |
|
937 |
* |
|
938 |
* Fun [ObjKeyFile S S S I] ObjKeyFile |
|
939 |
* If key cannot be found then it is created. If group cannot be found then it is created. |
|
940 |
* |
|
941 |
* \param ObjKeyFile : a valid object |
|
942 |
* \param S : a group name |
|
943 |
* \param S : a key name |
|
944 |
* \param S : the new value |
|
945 |
* \param S : a locale identifier |
|
946 |
* \return ObjKeyFile : the same object or nil if error |
|
947 |
* |
|
948 |
* If key or group is nil, the command is ignored but a message is written to the log |
|
949 |
*/ |
|
950 |
int SCOL_gkeyFileSetLocaleValue (mmachine m) |
|
951 |
{ |
|
952 |
int mini, mgroup, mkey, mvalue, mlocale; |
|
953 |
GKeyFile *ini; |
|
954 |
gchar *group = NULL, *key = NULL, *value = NULL, *locale = NULL; |
|
955 |
|
|
956 |
mlocale = MMpull (m); |
|
957 |
mvalue = MMpull (m); |
|
958 |
mkey = MMpull (m); |
|
959 |
mgroup = MMpull (m); |
|
960 |
mini = MMpull (m); |
|
961 |
|
|
962 |
if ((mini == NIL) || (mvalue == NIL)) |
|
963 |
{ |
|
964 |
MMechostr (0, "SCOL_gkeyFileSetLocaleValue error : bad(s) argument(s) !\n"); |
|
965 |
MMpush (m, NIL); |
|
966 |
return 0; |
|
967 |
} |
|
968 |
|
|
969 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
970 |
if (mgroup != NIL) |
|
971 |
group = MMstartstr (m, MTOP (mgroup)); |
|
972 |
if (mkey != NIL) |
|
973 |
key = MMstartstr (m, MTOP (mkey)); |
|
974 |
if (mvalue != NIL) |
|
975 |
value = SCOLUTF8 (MMstartstr (m, MTOP (mvalue)), -1); |
|
976 |
if (mlocale != NIL) |
|
977 |
locale = MMstartstr (m, MTOP (mlocale)); |
|
978 |
|
|
979 |
g_key_file_set_locale_string (ini, group, key, locale, value); |
|
980 |
g_free (value); |
|
981 |
MMpush (m, mini); |
|
982 |
return 0; |
|
983 |
} |
|
984 |
|
|
985 |
/** |
|
986 |
* \brief _gkeyFileSetValueList : Associates a list of string for key under group. |
|
987 |
* |
|
988 |
* fun [ObjKeyFile S S [S r1] ObjKeyFile |
|
989 |
* If key cannot be found then it is created. If group cannot be found then it is created. |
|
990 |
* |
|
991 |
* \param : ObjKeyFile : an object |
|
992 |
* \param : S : a group |
|
993 |
* \param : S : a key |
|
994 |
* \param [S r1] : a list of values |
|
995 |
* \return ObjKeyFile : the same object or nil if error |
|
996 |
* |
|
997 |
* If key or group is nil, the command is ignored but a message is written to the log |
|
998 |
*/ |
|
999 |
int SCOL_gkeyFileSetValueList (mmachine m) |
|
1000 |
{ |
|
1001 |
int mini, mgroup, mkey, mvalues, mtmp; |
|
1002 |
int i = 0, size = 0; |
|
1003 |
GKeyFile *ini; |
|
1004 |
gchar *group = NULL, *key = NULL; |
|
1005 |
gchar ** values = NULL; |
|
1006 |
|
|
1007 |
mtmp = MMget (m, 0); /* blurps */ |
|
1008 |
mvalues = MMpull (m); |
|
1009 |
mkey = MMpull (m); |
|
1010 |
mgroup = MMpull (m); |
|
1011 |
mini = MMpull (m); |
|
1012 |
|
|
1013 |
if ((mini == NIL) || (mvalues == NIL)) |
|
1014 |
{ |
|
1015 |
MMechostr (0, "SCOL_gkeyFileSetValueList error : bad(s) argument(s) !\n"); |
|
1016 |
MMpush (m, NIL); |
|
1017 |
return 0; |
|
1018 |
} |
|
1019 |
|
|
1020 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
1021 |
if (mgroup != NIL) |
|
1022 |
group = MMstartstr (m, MTOP (mgroup)); |
|
1023 |
if (mkey != NIL) |
|
1024 |
key = MMstartstr (m, MTOP (mkey)); |
|
1025 |
|
|
1026 |
mvalues = MTOP (mvalues); |
|
1027 |
mtmp = MTOP (mtmp); |
|
1028 |
|
|
1029 |
while (mtmp != NIL) |
|
1030 |
{ |
|
1031 |
size++; |
|
1032 |
mtmp = MMfetch (m, mtmp, 1)>>1; |
|
1033 |
} |
|
1034 |
values = g_new0 (gchar *, size+1); |
|
1035 |
while (mvalues != NIL) |
|
1036 |
{ |
|
1037 |
values[i] = SCOLUTF8 (MMstartstr (m, MTOP (MMfetch (m, mvalues, 0))), -1); |
|
1038 |
i++; |
|
1039 |
mvalues = MMfetch (m, mvalues, 1)>>1; |
|
1040 |
} |
|
1041 |
|
|
1042 |
g_key_file_set_string_list (ini, group, key, (const gchar** const) values, size); |
|
1043 |
MMpush (m, mini); |
|
1044 |
g_strfreev (values); |
|
1045 |
return 0; |
|
1046 |
} |
|
1047 |
|
|
1048 |
/** |
|
1049 |
* \brief _gkeyFileSetValueList : Associates a list of string for key under group. |
|
1050 |
* |
|
1051 |
* fun [ObjKeyFile S S S [S r1] ObjKeyFile |
|
1052 |
* If key cannot be found then it is created. If group cannot be found then it is created. |
|
1053 |
* |
|
1054 |
* \param : ObjKeyFile : an object |
|
1055 |
* \param : S : a group |
|
1056 |
* \param : S : a key |
|
1057 |
* \param : S : a locale identifier |
|
1058 |
* \param [S r1] : a list of values |
|
1059 |
* \return ObjKeyFile : the same object or nil if error |
|
1060 |
* |
|
1061 |
* If key or group is nil, the command is ignored but a message is written to the log |
|
1062 |
*/ |
|
1063 |
int SCOL_gkeyFileSetLocaleValueList (mmachine m) |
|
1064 |
{ |
|
1065 |
int mini, mgroup, mkey, mlocale, mvalues, mtmp; |
|
1066 |
int i = 0, size = 0; |
|
1067 |
GKeyFile *ini; |
|
1068 |
gchar *group = NULL, *key = NULL, *locale = NULL; |
|
1069 |
gchar ** values = NULL; |
|
1070 |
|
|
1071 |
mtmp = MMget (m, 0); /* blurps */ |
|
1072 |
mvalues = MMpull (m); |
|
1073 |
mlocale = MMpull (m); |
|
1074 |
mkey = MMpull (m); |
|
1075 |
mgroup = MMpull (m); |
|
1076 |
mini = MMpull (m); |
|
1077 |
|
|
1078 |
if ((mini == NIL) || (mvalues == NIL)) |
|
1079 |
{ |
|
1080 |
MMechostr (0, "SCOL_gkeyFileSetLocaleValueList error : bad(s) argument(s) !\n"); |
|
1081 |
MMpush (m, NIL); |
|
1082 |
return 0; |
|
1083 |
} |
|
1084 |
|
|
1085 |
ini = (GKeyFile*) MMfetch (m, MTOP (mini), OBJGKEYFILE_HANDLE); |
|
1086 |
if (mgroup != NIL) |
|
1087 |
group = MMstartstr (m, MTOP (mgroup)); |
|
1088 |
if (mkey != NIL) |
|
1089 |
key = MMstartstr (m, MTOP (mkey)); |
|
1090 |
if (mlocale != NIL) |
|
1091 |
locale = MMstartstr (m, MTOP (mlocale)); |
|
1092 |
|
|
1093 |
mvalues = MTOP (mvalues); |
|
1094 |
mtmp = MTOP (mtmp); |
|
1095 |
|
|
1096 |
while (mtmp != NIL) |
|
1097 |
{ |
|
1098 |
size++; |
|
1099 |
mtmp = MMfetch (m, mtmp, 1)>>1; |
|
1100 |
} |
|
1101 |
values = g_new0 (gchar *, size+1); |
|
1102 |
while (mvalues != NIL) |
|
1103 |
{ |
|
1104 |
values[i] = SCOLUTF8 (MMstartstr (m, MTOP (MMfetch (m, mvalues, 0))), -1); |
|
1105 |
i++; |
|
1106 |
mvalues = MMfetch (m, mvalues, 1)>>1; |
|
1107 |
} |
|
1108 |
|
|
1109 |
g_key_file_set_locale_string_list (ini, group, key, locale, (const gchar** const) values, size); |
|
1110 |
MMpush (m, mini); |
|
1111 |
g_strfreev (values); |
|
1112 |
return 0; |
|
1113 |
} |
|
444 |
MMpush (m, NIL); |
Also available in: Unified diff
Utils_g