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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <sys/types.h>
35 #define overflow (next == &CharBuffer [STRSIZE-1])
37 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyDefOnKey
, KeyValue
};
39 typedef struct TKeys
{
45 typedef struct TSecHeader
{
48 struct TSecHeader
*link
;
51 typedef struct TProfile
{
54 struct TProfile
*link
;
57 static TProfile
*Base
= 0;
60 find_loaded (const char *FileName
, TSecHeader
** section
)
65 if (!g_strcasecmp (FileName
, p
->FileName
)) {
66 *section
= p
->Section
;
74 #define TRANSLATION_CHAR '\200'
77 str_untranslate_newline_dup (char *s
)
81 g_return_val_if_fail(s
, NULL
);
84 l
+= (*p
== '\n' || *p
== TRANSLATION_CHAR
);
87 q
= p
= g_malloc (l
+ 1);
93 *p
++ = TRANSLATION_CHAR
;
96 case TRANSLATION_CHAR
:
97 if (s
[1] == 'n' || s
[1] == TRANSLATION_CHAR
)
98 *p
++ = TRANSLATION_CHAR
;
99 *p
++ = TRANSLATION_CHAR
;
110 return 0; /* not reached */
114 str_translate_newline_dup (char *s
)
117 g_return_val_if_fail(s
,NULL
);
118 q
= p
= g_malloc (strlen (s
) + 1);
122 if (*s
== TRANSLATION_CHAR
) {
127 case TRANSLATION_CHAR
:
128 *p
++ = TRANSLATION_CHAR
;
131 *p
++ = TRANSLATION_CHAR
;
135 *p
++ = TRANSLATION_CHAR
;
144 return q
; /* not reached */
147 static TSecHeader
*load (const char *file
)
151 TSecHeader
*SecHeader
= 0;
152 char CharBuffer
[STRSIZE
];
153 char *next
= NULL
; /* Not needed */
156 if ((f
= fopen (file
, "r"))==NULL
)
160 while ((c
= getc (f
)) != EOF
){
161 if (c
== '\r') /* Ignore Carriage Return */
167 if (c
== ']' || overflow
){
170 SecHeader
->AppName
= g_strdup (CharBuffer
);
190 SecHeader
= g_new (TSecHeader
, 1);
191 SecHeader
->link
= temp
;
197 if (state
== FirstBrace
) /* On first pass, don't allow dangling keys */
200 if ((c
== ' ' && state
!= KeyDefOnKey
) || c
== '\t')
203 if (c
== '\n' || overflow
) {
204 /* Abort Definition */
209 if (c
== '=' || overflow
){
212 temp
= SecHeader
->Keys
;
214 SecHeader
->Keys
=g_new (TKeys
, 1);
215 SecHeader
->Keys
->link
= temp
;
216 SecHeader
->Keys
->KeyName
= g_strdup (CharBuffer
);
226 if (overflow
|| c
== '\n'){
228 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
229 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
232 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
233 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
241 } /* while ((c = getc (f)) != EOF) */
246 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
248 case OnSecHeader
: { /* Broken initialization file */
249 TSecHeader
*link
= SecHeader
->link
;
252 fprintf (stderr
, "Warning: Corrupted initialization file `%s'\n",
262 static void new_key (TSecHeader
*section
, const char *KeyName
, const char *Value
)
266 key
= g_new (TKeys
, 1);
267 key
->KeyName
= g_strdup (KeyName
);
268 key
->Value
= g_strdup (Value
);
269 key
->link
= section
->Keys
;
274 GetSetProfileChar (int set
, const char *AppName
, const char *KeyName
,
275 const char *Default
, const char *FileName
)
282 Current
= find_loaded (FileName
, §ion
);
284 Current
= g_new (TProfile
, 1);
285 Current
->link
= Base
;
286 Current
->FileName
= g_strdup (FileName
);
287 Current
->Section
= load (FileName
);
289 section
= Current
->Section
;
293 for (; section
; section
= section
->link
){
294 if (section
->AppName
== 0 || g_strcasecmp (section
->AppName
, AppName
))
296 for (key
= section
->Keys
; key
; key
= key
->link
){
297 if ( g_strcasecmp (key
->KeyName
, KeyName
))
301 key
->Value
= g_strdup (Default
);
305 /* If getting the information, then don't write the information
306 to the INI file, need to run a couple of tests with windog */
309 new_key (section
, KeyName
, Default
);
314 /* Non existent section */
316 section
= g_new (TSecHeader
, 1);
317 section
->AppName
= g_strdup (AppName
);
319 new_key (section
, KeyName
, Default
);
320 section
->link
= Current
->Section
;
321 Current
->Section
= section
;
326 static short GetSetProfile (int set
, const char * AppName
, const char * KeyName
,
327 const char * Default
, char * ReturnedString
,
328 short Size
, const char * FileName
)
333 s
= GetSetProfileChar (set
, AppName
, KeyName
, Default
, FileName
);
335 g_strlcpy (ReturnedString
, s
, Size
);
340 short GetPrivateProfileString (const char * AppName
, const char * KeyName
,
341 const char * Default
, char * ReturnedString
,
342 short Size
, const char * FileName
)
344 return (GetSetProfile (0, AppName
, KeyName
, Default
, ReturnedString
, Size
, FileName
));
347 const char *get_profile_string (const char *AppName
, const char *KeyName
, const char *Default
,
348 const char *FileName
)
350 return GetSetProfileChar (0, AppName
, KeyName
, Default
, FileName
);
353 int GetPrivateProfileInt (const char * AppName
, const char * KeyName
, int Default
,
356 char IntBuf
[BUF_TINY
];
359 g_snprintf (buf
, sizeof (buf
), "%d", Default
);
361 /* Check the exact semantic with the SDK */
362 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, BUF_TINY
, File
);
363 if (! g_strcasecmp (IntBuf
, "true"))
365 if (! g_strcasecmp (IntBuf
, "yes"))
367 return (int) atol (IntBuf
);
370 int WritePrivateProfileString (const char * AppName
, const char * KeyName
, const char * String
,
371 const char * FileName
)
373 return GetSetProfile (1, AppName
, KeyName
, String
, NULL
, 0, FileName
);
376 static void dump_keys (FILE * profile
, TKeys
* p
)
381 dump_keys (profile
, p
->link
);
382 t
= str_untranslate_newline_dup (p
->Value
);
383 fprintf (profile
, "%s=%s\n", p
->KeyName
, t
);
387 static void dump_sections (FILE *profile
, TSecHeader
*p
)
391 dump_sections (profile
, p
->link
);
393 fprintf (profile
, "\n[%s]\n", p
->AppName
);
394 dump_keys (profile
, p
->Keys
);
398 static void dump_profile (TProfile
*p
)
404 dump_profile (p
->link
);
405 /* .ado: p->FileName can be empty, it's better to jump over */
406 if (p
->FileName
[0] != (char) 0)
407 if ((profile
= fopen (p
->FileName
, "w")) != NULL
){
408 dump_sections (profile
, p
->Section
);
414 * Must be called at the end of wine run
417 void sync_profiles (void)
422 static void free_keys (TKeys
*p
)
432 static void free_sections (TSecHeader
*p
)
436 free_sections (p
->link
);
444 static void free_profile (TProfile
*p
)
448 free_profile (p
->link
);
449 free_sections (p
->Section
);
450 g_free (p
->FileName
);
454 void free_profile_name (const char *s
)
461 for (p
= Base
; p
; p
= p
->link
){
462 if (strcmp (s
, p
->FileName
) == 0){
463 free_sections (p
->Section
);
471 void free_profiles (void)
476 void *profile_init_iterator (const char *appname
, const char *file
)
481 Current
= find_loaded (file
, §ion
);
483 Current
= g_new (TProfile
, 1);
484 Current
->link
= Base
;
485 Current
->FileName
= g_strdup (file
);
486 Current
->Section
= load (file
);
488 section
= Current
->Section
;
490 for (; section
; section
= section
->link
){
491 if ( g_strcasecmp (section
->AppName
, appname
))
493 return section
->Keys
;
498 void *profile_iterator_next (void *s
, char **key
, char **value
)
500 TKeys
*keys
= (TKeys
*) s
;
503 *key
= keys
->KeyName
;
504 *value
= keys
->Value
;
510 void profile_clean_section (const char *appname
, const char *file
)
514 /* We assume the user has called one of the other initialization funcs */
515 if (!find_loaded (file
, §ion
)){
516 fprintf (stderr
,"Warning: profile_clean_section called before init\n");
519 /* We only disable the section, so it will still be freed, but it */
520 /* won't be find by further walks of the structure */
522 for (; section
; section
= section
->link
){
523 if ( g_strcasecmp (section
->AppName
, appname
))
525 section
->AppName
[0] = 0;
529 int profile_has_section (const char *section_name
, const char *profile
)
533 /* We assume the user has called one of the other initialization funcs */
534 if (!find_loaded (profile
, §ion
)){
537 for (; section
; section
= section
->link
){
538 if ( g_strcasecmp (section
->AppName
, section_name
))
545 void profile_forget_profile (const char *file
)
549 for (p
= Base
; p
; p
= p
->link
){
550 if ( g_strcasecmp (file
, p
->FileName
))