it.po converted in UTF-8, changed some upper/lower convention in a more 'italian...
[midnight-commander.git] / src / profile.c
blob6eed6ad6bb9aff1ee72ccc195105029453a91c3f
1 /*
2 * Initialization-File Functions.
4 * From the Wine project
6 Copyright (C) 1993, 1994 Miguel de Icaza.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include "global.h"
28 #include "profile.h"
30 #define STRSIZE 4096
31 #define overflow (next == &CharBuffer [STRSIZE-1])
33 enum { FirstBrace, OnSecHeader, IgnoreToEOL, KeyDef, KeyDefOnKey, KeyValue };
35 typedef struct TKeys {
36 char *KeyName;
37 char *Value;
38 struct TKeys *link;
39 } TKeys;
41 typedef struct TSecHeader {
42 char *AppName;
43 TKeys *Keys;
44 struct TSecHeader *link;
45 } TSecHeader;
47 typedef struct TProfile {
48 char *FileName;
49 TSecHeader *Section;
50 struct TProfile *link;
51 } TProfile;
53 static TProfile *Base = 0;
55 static TProfile *
56 find_loaded (const char *FileName, TSecHeader ** section)
58 TProfile *p = Base;
60 while (p) {
61 if (!g_strcasecmp (FileName, p->FileName)) {
62 *section = p->Section;
63 return p;
65 p = p->link;
67 return NULL;
70 #define TRANSLATION_CHAR '\200'
72 static char *
73 str_untranslate_newline_dup (char *s)
75 int l = 0;
76 char *p = s, *q;
77 g_return_val_if_fail(s, NULL);
78 while (*p) {
79 l++;
80 l += (*p == '\n' || *p == TRANSLATION_CHAR);
81 p++;
83 q = p = g_malloc (l + 1);
84 if (!q)
85 return 0;
86 for (;;) {
87 switch (*s) {
88 case '\n':
89 *p++ = TRANSLATION_CHAR;
90 *p++ = 'n';
91 break;
92 case TRANSLATION_CHAR:
93 if (s[1] == 'n' || s[1] == TRANSLATION_CHAR)
94 *p++ = TRANSLATION_CHAR;
95 *p++ = TRANSLATION_CHAR;
96 break;
97 case '\0':
98 *p = '\0';
99 return q;
100 break;
101 default:
102 *p++ = *s;
104 s++;
106 return 0; /* not reached */
109 static char *
110 str_translate_newline_dup (char *s)
112 char *p, *q;
113 g_return_val_if_fail(s,NULL);
114 q = p = g_malloc (strlen (s) + 1);
115 if (!q)
116 return 0;
117 while (*s) {
118 if (*s == TRANSLATION_CHAR) {
119 switch (*(++s)) {
120 case 'n':
121 *p++ = '\n';
122 break;
123 case TRANSLATION_CHAR:
124 *p++ = TRANSLATION_CHAR;
125 break;
126 case '\0':
127 *p++ = TRANSLATION_CHAR;
128 *p++ = '\0';
129 return q;
130 default:
131 *p++ = TRANSLATION_CHAR;
132 *p++ = *s;
134 } else {
135 *p++ = *s;
137 s++;
139 *p = '\0';
140 return q; /* not reached */
143 static TSecHeader *load (const char *file)
145 FILE *f;
146 int state;
147 TSecHeader *SecHeader = 0;
148 char CharBuffer [STRSIZE];
149 char *next = ""; /* Not needed */
150 int c;
152 if ((f = fopen (file, "r"))==NULL)
153 return NULL;
155 state = FirstBrace;
156 while ((c = getc (f)) != EOF){
157 if (c == '\r') /* Ignore Carriage Return */
158 continue;
160 switch (state){
162 case OnSecHeader:
163 if (c == ']' || overflow){
164 *next = '\0';
165 next = CharBuffer;
166 SecHeader->AppName = g_strdup (CharBuffer);
167 state = IgnoreToEOL;
168 } else
169 *next++ = c;
170 break;
172 case IgnoreToEOL:
173 if (c == '\n'){
174 state = KeyDef;
175 next = CharBuffer;
177 break;
179 case FirstBrace:
180 case KeyDef:
181 case KeyDefOnKey:
182 if (c == '['){
183 TSecHeader *temp;
185 temp = SecHeader;
186 SecHeader = g_new (TSecHeader, 1);
187 SecHeader->link = temp;
188 SecHeader->Keys = 0;
189 state = OnSecHeader;
190 next = CharBuffer;
191 break;
193 if (state == FirstBrace) /* On first pass, don't allow dangling keys */
194 break;
196 if ((c == ' ' && state != KeyDefOnKey) || c == '\t')
197 break;
199 if (c == '\n' || overflow) /* Abort Definition */
200 next = CharBuffer;
202 if (c == '=' || overflow){
203 TKeys *temp;
205 temp = SecHeader->Keys;
206 *next = '\0';
207 SecHeader->Keys =g_new (TKeys, 1);
208 SecHeader->Keys->link = temp;
209 SecHeader->Keys->KeyName = g_strdup (CharBuffer);
210 state = KeyValue;
211 next = CharBuffer;
212 } else {
213 *next++ = c;
214 state = KeyDefOnKey;
216 break;
218 case KeyValue:
219 if (overflow || c == '\n'){
220 *next = '\0';
221 SecHeader->Keys->Value = str_translate_newline_dup (CharBuffer);
222 state = c == '\n' ? KeyDef : IgnoreToEOL;
223 next = CharBuffer;
224 #ifdef DEBUG
225 printf ("[%s] (%s)=%s\n", SecHeader->AppName,
226 SecHeader->Keys->KeyName, SecHeader->Keys->Value);
227 #endif
228 } else
229 *next++ = c;
230 break;
232 } /* switch */
234 } /* while ((c = getc (f)) != EOF) */
236 switch (state) {
237 case KeyValue:
238 *next = '\0';
239 SecHeader->Keys->Value = str_translate_newline_dup (CharBuffer);
240 break;
241 case OnSecHeader: { /* Broken initialization file */
242 TSecHeader *link = SecHeader->link;
243 g_free (SecHeader);
244 SecHeader = link;
245 fprintf (stderr, "Warning: Corrupted initialization file `%s'\n",
246 file);
247 break;
251 fclose (f);
252 return SecHeader;
255 static void new_key (TSecHeader *section, const char *KeyName, const char *Value)
257 TKeys *key;
259 key = g_new (TKeys, 1);
260 key->KeyName = g_strdup (KeyName);
261 key->Value = g_strdup (Value);
262 key->link = section->Keys;
263 section->Keys = key;
266 static char *
267 GetSetProfileChar (int set, const char *AppName, char *KeyName,
268 char *Default, char *FileName)
271 TProfile *Current;
272 TSecHeader *section;
273 TKeys *key;
275 Current = find_loaded (FileName, &section);
276 if (!Current) {
277 Current = g_new (TProfile, 1);
278 Current->link = Base;
279 Current->FileName = g_strdup (FileName);
280 Current->Section = load (FileName);
281 Base = Current;
282 section = Current->Section;
285 /* Start search */
286 for (; section; section = section->link){
287 if (section->AppName == 0 || g_strcasecmp (section->AppName, AppName))
288 continue;
289 for (key = section->Keys; key; key = key->link){
290 if ( g_strcasecmp (key->KeyName, KeyName))
291 continue;
292 if (set){
293 g_free (key->Value);
294 key->Value = g_strdup (Default);
296 return key->Value;
298 /* If getting the information, then don't write the information
299 to the INI file, need to run a couple of tests with windog */
300 /* No key found */
301 if (set){
302 new_key (section, KeyName, Default);
303 return 0;
307 /* Non existent section */
308 if (set && Default){
309 section = g_new (TSecHeader, 1);
310 section->AppName = g_strdup (AppName);
311 section->Keys = 0;
312 new_key (section, KeyName, Default);
313 section->link = Current->Section;
314 Current->Section = section;
316 return Default;
319 static short GetSetProfile (int set, const char * AppName, char * KeyName,
320 char * Default, char * ReturnedString,
321 short Size, char * FileName)
324 char *s;
326 s = GetSetProfileChar (set, AppName, KeyName, Default, FileName);
327 if (!set){
328 ReturnedString [Size-1] = 0;
329 strncpy (ReturnedString, s, Size-1);
331 return 1;
334 short GetPrivateProfileString (const char * AppName, char * KeyName,
335 char * Default, char * ReturnedString,
336 short Size, char * FileName)
338 return (GetSetProfile (0, AppName, KeyName, Default, ReturnedString, Size, FileName));
341 char *get_profile_string (const char *AppName, char *KeyName, char *Default,
342 char *FileName)
344 return GetSetProfileChar (0, AppName, KeyName, Default, FileName);
347 int GetPrivateProfileInt (const char * AppName, char * KeyName, int Default,
348 char * File)
350 char IntBuf [BUF_TINY];
351 char buf [BUF_TINY];
353 g_snprintf (buf, sizeof (buf), "%d", Default);
355 /* Check the exact semantic with the SDK */
356 GetPrivateProfileString (AppName, KeyName, buf, IntBuf, BUF_TINY, File);
357 if (! g_strcasecmp (IntBuf, "true"))
358 return 1;
359 if (! g_strcasecmp (IntBuf, "yes"))
360 return 1;
361 return (int) atol (IntBuf);
364 int WritePrivateProfileString (const char * AppName, char * KeyName, char * String,
365 char * FileName)
367 return GetSetProfile (1, AppName, KeyName, String, "", 0, FileName);
370 static void dump_keys (FILE * profile, TKeys * p)
372 char *t;
373 if (!p)
374 return;
375 dump_keys (profile, p->link);
376 t = str_untranslate_newline_dup (p->Value);
377 fprintf (profile, "%s=%s\n", p->KeyName, t);
378 g_free (t);
381 static void dump_sections (FILE *profile, TSecHeader *p)
383 if (!p)
384 return;
385 dump_sections (profile, p->link);
386 if (p->AppName [0]){
387 fprintf (profile, "\n[%s]\n", p->AppName);
388 dump_keys (profile, p->Keys);
392 static void dump_profile (TProfile *p)
394 FILE *profile;
396 if (!p)
397 return;
398 dump_profile (p->link);
399 /* .ado: p->FileName can be empty, it's better to jump over */
400 if (p->FileName[0] != (char) 0)
401 if ((profile = fopen (p->FileName, "w")) != NULL){
402 dump_sections (profile, p->Section);
403 fclose (profile);
408 * Must be called at the end of wine run
411 void sync_profiles (void)
413 dump_profile (Base);
416 static void free_keys (TKeys *p)
418 if (!p)
419 return;
420 free_keys (p->link);
421 g_free (p->KeyName);
422 g_free (p->Value);
423 g_free (p);
426 static void free_sections (TSecHeader *p)
428 if (!p)
429 return;
430 free_sections (p->link);
431 free_keys (p->Keys);
432 g_free (p->AppName);
433 p->link = 0;
434 p->Keys = 0;
435 g_free (p);
438 static void free_profile (TProfile *p)
440 if (!p)
441 return;
442 free_profile (p->link);
443 free_sections (p->Section);
444 g_free (p->FileName);
445 g_free (p);
448 void free_profile_name (char *s)
450 TProfile *p;
452 if (!s)
453 return;
455 for (p = Base; p; p = p->link){
456 if (strcmp (s, p->FileName) == 0){
457 free_sections (p->Section);
458 p->Section = 0;
459 p->FileName [0] = 0;
460 return;
465 void free_profiles (void)
467 free_profile (Base);
470 void *profile_init_iterator (char *appname, char *file)
472 TProfile *Current;
473 TSecHeader *section;
475 Current = find_loaded (file, &section);
476 if (!Current) {
477 Current = g_new (TProfile, 1);
478 Current->link = Base;
479 Current->FileName = g_strdup (file);
480 Current->Section = load (file);
481 Base = Current;
482 section = Current->Section;
484 for (; section; section = section->link){
485 if ( g_strcasecmp (section->AppName, appname))
486 continue;
487 return section->Keys;
489 return 0;
492 void *profile_iterator_next (void *s, char **key, char **value)
494 TKeys *keys = (TKeys *) s;
496 if (keys){
497 *key = keys->KeyName;
498 *value = keys->Value;
499 keys = keys->link;
501 return keys;
504 void profile_clean_section (char *appname, char *file)
506 TSecHeader *section;
508 /* We assume the user has called one of the other initialization funcs */
509 if (!find_loaded (file, &section)){
510 fprintf (stderr,"Warning: profile_clean_section called before init\n");
511 return;
513 /* We only disable the section, so it will still be freed, but it */
514 /* won't be find by further walks of the structure */
516 for (; section; section = section->link){
517 if ( g_strcasecmp (section->AppName, appname))
518 continue;
519 section->AppName [0] = 0;
523 int profile_has_section (char *section_name, char *profile)
525 TSecHeader *section;
527 /* We assume the user has called one of the other initialization funcs */
528 if (!find_loaded (profile, &section)){
529 return 0;
531 for (; section; section = section->link){
532 if ( g_strcasecmp (section->AppName, section_name))
533 continue;
534 return 1;
536 return 0;
539 void profile_forget_profile (char *file)
541 TProfile *p;
543 for (p = Base; p; p = p->link){
544 if ( g_strcasecmp (file, p->FileName))
545 continue;
546 p->FileName [0] = 0;