2 * Initialization-File Functions.
4 * From the Wine project
6 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
7 2007 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <sys/types.h>
36 #define overflow (next == &CharBuffer [STRSIZE-1])
38 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyDefOnKey
, KeyValue
};
40 typedef struct TKeys
{
46 typedef struct TSecHeader
{
49 struct TSecHeader
*link
;
52 typedef struct TProfile
{
55 struct TProfile
*link
;
58 static TProfile
*Base
= 0;
61 find_loaded (const char *FileName
, TSecHeader
** section
)
66 if (!g_strcasecmp (FileName
, p
->FileName
)) {
67 *section
= p
->Section
;
75 #define TRANSLATION_CHAR '\200'
78 str_untranslate_newline_dup (char *s
)
82 g_return_val_if_fail(s
, NULL
);
85 l
+= (*p
== '\n' || *p
== TRANSLATION_CHAR
);
88 q
= p
= g_malloc (l
+ 1);
94 *p
++ = TRANSLATION_CHAR
;
97 case TRANSLATION_CHAR
:
98 if (s
[1] == 'n' || s
[1] == TRANSLATION_CHAR
)
99 *p
++ = TRANSLATION_CHAR
;
100 *p
++ = TRANSLATION_CHAR
;
111 return 0; /* not reached */
115 str_translate_newline_dup (char *s
)
118 g_return_val_if_fail(s
,NULL
);
119 q
= p
= g_malloc (strlen (s
) + 1);
123 if (*s
== TRANSLATION_CHAR
) {
128 case TRANSLATION_CHAR
:
129 *p
++ = TRANSLATION_CHAR
;
132 *p
++ = TRANSLATION_CHAR
;
136 *p
++ = TRANSLATION_CHAR
;
145 return q
; /* not reached */
148 static TSecHeader
*load (const char *file
)
152 TSecHeader
*SecHeader
= 0;
153 char CharBuffer
[STRSIZE
];
154 char *next
= NULL
; /* Not needed */
157 if ((f
= fopen (file
, "r"))==NULL
)
161 while ((c
= getc (f
)) != EOF
){
162 if (c
== '\r') /* Ignore Carriage Return */
168 if (c
== ']' || overflow
){
171 SecHeader
->AppName
= g_strdup (CharBuffer
);
191 SecHeader
= g_new (TSecHeader
, 1);
192 SecHeader
->link
= temp
;
198 if (state
== FirstBrace
) /* On first pass, don't allow dangling keys */
201 if ((c
== ' ' && state
!= KeyDefOnKey
) || c
== '\t')
204 if (c
== '\n' || overflow
) {
205 /* Abort Definition */
210 if (c
== '=' || overflow
){
213 temp
= SecHeader
->Keys
;
215 SecHeader
->Keys
=g_new (TKeys
, 1);
216 SecHeader
->Keys
->link
= temp
;
217 SecHeader
->Keys
->KeyName
= g_strdup (CharBuffer
);
227 if (overflow
|| c
== '\n'){
229 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
230 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
233 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
234 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
242 } /* while ((c = getc (f)) != EOF) */
247 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
249 case OnSecHeader
: { /* Broken initialization file */
250 TSecHeader
*link
= SecHeader
->link
;
253 fprintf (stderr
, "Warning: Corrupted initialization file `%s'\n",
263 static void new_key (TSecHeader
*section
, const char *KeyName
, const char *Value
)
267 key
= g_new (TKeys
, 1);
268 key
->KeyName
= g_strdup (KeyName
);
269 key
->Value
= g_strdup (Value
);
270 key
->link
= section
->Keys
;
275 GetSetProfileChar (int set
, const char *AppName
, const char *KeyName
,
276 const char *Default
, const char *FileName
)
283 Current
= find_loaded (FileName
, §ion
);
285 Current
= g_new (TProfile
, 1);
286 Current
->link
= Base
;
287 Current
->FileName
= g_strdup (FileName
);
288 Current
->Section
= load (FileName
);
290 section
= Current
->Section
;
294 for (; section
; section
= section
->link
){
295 if (section
->AppName
== 0 || g_strcasecmp (section
->AppName
, AppName
))
297 for (key
= section
->Keys
; key
; key
= key
->link
){
298 if ( g_strcasecmp (key
->KeyName
, KeyName
))
302 key
->Value
= g_strdup (Default
);
306 /* If getting the information, then don't write the information
307 to the INI file, need to run a couple of tests with windog */
310 new_key (section
, KeyName
, Default
);
315 /* Non existent section */
317 section
= g_new (TSecHeader
, 1);
318 section
->AppName
= g_strdup (AppName
);
320 new_key (section
, KeyName
, Default
);
321 section
->link
= Current
->Section
;
322 Current
->Section
= section
;
327 static short GetSetProfile (int set
, const char * AppName
, const char * KeyName
,
328 const char * Default
, char * ReturnedString
,
329 short Size
, const char * FileName
)
334 s
= GetSetProfileChar (set
, AppName
, KeyName
, Default
, FileName
);
336 g_strlcpy (ReturnedString
, s
, Size
);
341 short GetPrivateProfileString (const char * AppName
, const char * KeyName
,
342 const char * Default
, char * ReturnedString
,
343 short Size
, const char * FileName
)
345 return (GetSetProfile (0, AppName
, KeyName
, Default
, ReturnedString
, Size
, FileName
));
348 const char *get_profile_string (const char *AppName
, const char *KeyName
, const char *Default
,
349 const char *FileName
)
351 return GetSetProfileChar (0, AppName
, KeyName
, Default
, FileName
);
354 int GetPrivateProfileInt (const char * AppName
, const char * KeyName
, int Default
,
357 char IntBuf
[BUF_TINY
];
360 g_snprintf (buf
, sizeof (buf
), "%d", Default
);
362 /* Check the exact semantic with the SDK */
363 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, BUF_TINY
, File
);
364 if (! g_strcasecmp (IntBuf
, "true"))
366 if (! g_strcasecmp (IntBuf
, "yes"))
368 return (int) atol (IntBuf
);
371 int WritePrivateProfileString (const char * AppName
, const char * KeyName
, const char * String
,
372 const char * FileName
)
374 return GetSetProfile (1, AppName
, KeyName
, String
, NULL
, 0, FileName
);
377 static void dump_keys (FILE * profile
, TKeys
* p
)
382 dump_keys (profile
, p
->link
);
383 t
= str_untranslate_newline_dup (p
->Value
);
384 fprintf (profile
, "%s=%s\n", p
->KeyName
, t
);
388 static void dump_sections (FILE *profile
, TSecHeader
*p
)
392 dump_sections (profile
, p
->link
);
394 fprintf (profile
, "\n[%s]\n", p
->AppName
);
395 dump_keys (profile
, p
->Keys
);
399 static void dump_profile (TProfile
*p
)
405 dump_profile (p
->link
);
406 /* .ado: p->FileName can be empty, it's better to jump over */
407 if (p
->FileName
[0] != (char) 0)
408 if ((profile
= fopen (p
->FileName
, "w")) != NULL
){
409 dump_sections (profile
, p
->Section
);
415 * Must be called at the end of wine run
418 void sync_profiles (void)
423 static void free_keys (TKeys
*p
)
433 static void free_sections (TSecHeader
*p
)
437 free_sections (p
->link
);
445 static void free_profile (TProfile
*p
)
449 free_profile (p
->link
);
450 free_sections (p
->Section
);
451 g_free (p
->FileName
);
455 void free_profile_name (const char *s
)
462 for (p
= Base
; p
; p
= p
->link
){
463 if (strcmp (s
, p
->FileName
) == 0){
464 free_sections (p
->Section
);
472 void free_profiles (void)
477 void *profile_init_iterator (const char *appname
, const char *file
)
482 Current
= find_loaded (file
, §ion
);
484 Current
= g_new (TProfile
, 1);
485 Current
->link
= Base
;
486 Current
->FileName
= g_strdup (file
);
487 Current
->Section
= load (file
);
489 section
= Current
->Section
;
491 for (; section
; section
= section
->link
){
492 if ( g_strcasecmp (section
->AppName
, appname
))
494 return section
->Keys
;
499 void *profile_iterator_next (void *s
, char **key
, char **value
)
501 TKeys
*keys
= (TKeys
*) s
;
504 *key
= keys
->KeyName
;
505 *value
= keys
->Value
;
511 void profile_clean_section (const char *appname
, const char *file
)
515 /* We assume the user has called one of the other initialization funcs */
516 if (!find_loaded (file
, §ion
)){
517 fprintf (stderr
,"Warning: profile_clean_section called before init\n");
520 /* We only disable the section, so it will still be freed, but it */
521 /* won't be find by further walks of the structure */
523 for (; section
; section
= section
->link
){
524 if ( g_strcasecmp (section
->AppName
, appname
))
526 section
->AppName
[0] = 0;
530 int profile_has_section (const char *section_name
, const char *profile
)
534 /* We assume the user has called one of the other initialization funcs */
535 if (!find_loaded (profile
, §ion
)){
538 for (; section
; section
= section
->link
){
539 if ( g_strcasecmp (section
->AppName
, section_name
))
546 void profile_forget_profile (const char *file
)
550 for (p
= Base
; p
; p
= p
->link
){
551 if ( g_strcasecmp (file
, p
->FileName
))