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.
29 #include <sys/types.h>
31 #include <mhl/memory.h>
32 #include <mhl/string.h>
38 #define overflow (next == &CharBuffer [STRSIZE-1])
40 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyDefOnKey
, KeyValue
};
42 typedef struct TKeys
{
48 typedef struct TSecHeader
{
51 struct TSecHeader
*link
;
54 typedef struct TProfile
{
57 struct TProfile
*link
;
60 static TProfile
*Base
= 0;
63 find_loaded (const char *FileName
, TSecHeader
** section
)
68 if (!g_strcasecmp (FileName
, p
->FileName
)) {
69 *section
= p
->Section
;
77 #define TRANSLATION_CHAR '\200'
80 str_untranslate_newline_dup (char *s
)
84 g_return_val_if_fail(s
, NULL
);
87 l
+= (*p
== '\n' || *p
== TRANSLATION_CHAR
);
90 q
= p
= g_malloc (l
+ 1);
96 *p
++ = TRANSLATION_CHAR
;
99 case TRANSLATION_CHAR
:
100 if (s
[1] == 'n' || s
[1] == TRANSLATION_CHAR
)
101 *p
++ = TRANSLATION_CHAR
;
102 *p
++ = TRANSLATION_CHAR
;
113 return 0; /* not reached */
117 str_translate_newline_dup (char *s
)
120 g_return_val_if_fail(s
,NULL
);
121 q
= p
= g_malloc (strlen (s
) + 1);
125 if (*s
== TRANSLATION_CHAR
) {
130 case TRANSLATION_CHAR
:
131 *p
++ = TRANSLATION_CHAR
;
134 *p
++ = TRANSLATION_CHAR
;
138 *p
++ = TRANSLATION_CHAR
;
147 return q
; /* not reached */
150 static TSecHeader
*load (const char *file
)
154 TSecHeader
*SecHeader
= 0;
155 char CharBuffer
[STRSIZE
];
156 char *next
= NULL
; /* Not needed */
159 if ((f
= fopen (file
, "r"))==NULL
)
163 while ((c
= getc (f
)) != EOF
){
164 if (c
== '\r') /* Ignore Carriage Return */
170 if (c
== ']' || overflow
){
173 SecHeader
->AppName
= g_strdup (CharBuffer
);
193 SecHeader
= g_new (TSecHeader
, 1);
194 SecHeader
->link
= temp
;
200 if (state
== FirstBrace
) /* On first pass, don't allow dangling keys */
203 if ((c
== ' ' && state
!= KeyDefOnKey
) || c
== '\t')
206 if (c
== '\n' || overflow
) {
207 /* Abort Definition */
212 if (c
== '=' || overflow
){
215 temp
= SecHeader
->Keys
;
217 SecHeader
->Keys
=g_new (TKeys
, 1);
218 SecHeader
->Keys
->link
= temp
;
219 SecHeader
->Keys
->KeyName
= g_strdup (CharBuffer
);
229 if (overflow
|| c
== '\n'){
231 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
232 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
235 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
236 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
244 } /* while ((c = getc (f)) != EOF) */
249 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
251 case OnSecHeader
: { /* Broken initialization file */
252 TSecHeader
*link
= SecHeader
->link
;
255 fprintf (stderr
, "Warning: Corrupted initialization file `%s'\n",
265 static void new_key (TSecHeader
*section
, const char *KeyName
, const char *Value
)
269 key
= g_new (TKeys
, 1);
270 key
->KeyName
= g_strdup (KeyName
);
271 key
->Value
= g_strdup (Value
);
272 key
->link
= section
->Keys
;
277 GetSetProfileChar (int set
, const char *AppName
, const char *KeyName
,
278 const char *Default
, const char *FileName
)
285 Current
= find_loaded (FileName
, §ion
);
287 Current
= g_new (TProfile
, 1);
288 Current
->link
= Base
;
289 Current
->FileName
= g_strdup (FileName
);
290 Current
->Section
= load (FileName
);
292 section
= Current
->Section
;
296 for (; section
; section
= section
->link
){
297 if (section
->AppName
== 0 || g_strcasecmp (section
->AppName
, AppName
))
299 for (key
= section
->Keys
; key
; key
= key
->link
){
300 if ( g_strcasecmp (key
->KeyName
, KeyName
))
304 key
->Value
= g_strdup (Default
);
308 /* If getting the information, then don't write the information
309 to the INI file, need to run a couple of tests with windog */
312 new_key (section
, KeyName
, Default
);
317 /* Non existent section */
319 section
= g_new (TSecHeader
, 1);
320 section
->AppName
= g_strdup (AppName
);
322 new_key (section
, KeyName
, Default
);
323 section
->link
= Current
->Section
;
324 Current
->Section
= section
;
329 static short GetSetProfile (int set
, const char * AppName
, const char * KeyName
,
330 const char * Default
, char * ReturnedString
,
331 short Size
, const char * FileName
)
336 s
= GetSetProfileChar (set
, AppName
, KeyName
, Default
, FileName
);
338 g_strlcpy (ReturnedString
, s
, Size
);
343 short GetPrivateProfileString (const char * AppName
, const char * KeyName
,
344 const char * Default
, char * ReturnedString
,
345 short Size
, const char * FileName
)
347 return (GetSetProfile (0, AppName
, KeyName
, Default
, ReturnedString
, Size
, FileName
));
350 const char *get_profile_string (const char *AppName
, const char *KeyName
, const char *Default
,
351 const char *FileName
)
353 return GetSetProfileChar (0, AppName
, KeyName
, Default
, FileName
);
356 int GetPrivateProfileInt (const char * AppName
, const char * KeyName
, int Default
,
359 char IntBuf
[BUF_TINY
];
362 snprintf (buf
, sizeof (buf
), "%d", Default
);
364 /* Check the exact semantic with the SDK */
365 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, BUF_TINY
, File
);
366 if (! g_strcasecmp (IntBuf
, "true"))
368 if (! g_strcasecmp (IntBuf
, "yes"))
370 return (int) atol (IntBuf
);
373 int WritePrivateProfileString (const char * AppName
, const char * KeyName
, const char * String
,
374 const char * FileName
)
376 return GetSetProfile (1, AppName
, KeyName
, String
, NULL
, 0, FileName
);
379 static void dump_keys (FILE * profile
, TKeys
* p
)
384 dump_keys (profile
, p
->link
);
385 t
= str_untranslate_newline_dup (p
->Value
);
386 fprintf (profile
, "%s=%s\n", p
->KeyName
, t
);
390 static void dump_sections (FILE *profile
, TSecHeader
*p
)
394 dump_sections (profile
, p
->link
);
396 fprintf (profile
, "\n[%s]\n", p
->AppName
);
397 dump_keys (profile
, p
->Keys
);
401 static void dump_profile (TProfile
*p
)
407 dump_profile (p
->link
);
408 /* .ado: p->FileName can be empty, it's better to jump over */
409 if (p
->FileName
[0] != (char) 0)
410 if ((profile
= fopen (p
->FileName
, "w")) != NULL
){
411 dump_sections (profile
, p
->Section
);
417 * Must be called at the end of wine run
420 void sync_profiles (void)
425 static void free_keys (TKeys
*p
)
435 static void free_sections (TSecHeader
*p
)
439 free_sections (p
->link
);
447 static void free_profile (TProfile
*p
)
451 free_profile (p
->link
);
452 free_sections (p
->Section
);
453 g_free (p
->FileName
);
457 void free_profile_name (const char *s
)
464 for (p
= Base
; p
; p
= p
->link
){
465 if (strcmp (s
, p
->FileName
) == 0){
466 free_sections (p
->Section
);
474 void free_profiles (void)
479 void *profile_init_iterator (const char *appname
, const char *file
)
484 Current
= find_loaded (file
, §ion
);
486 Current
= g_new (TProfile
, 1);
487 Current
->link
= Base
;
488 Current
->FileName
= g_strdup (file
);
489 Current
->Section
= load (file
);
491 section
= Current
->Section
;
493 for (; section
; section
= section
->link
){
494 if ( g_strcasecmp (section
->AppName
, appname
))
496 return section
->Keys
;
501 void *profile_iterator_next (void *s
, char **key
, char **value
)
503 TKeys
*keys
= (TKeys
*) s
;
506 *key
= keys
->KeyName
;
507 *value
= keys
->Value
;
513 void profile_clean_section (const char *appname
, const char *file
)
517 /* We assume the user has called one of the other initialization funcs */
518 if (!find_loaded (file
, §ion
)){
519 fprintf (stderr
,"Warning: profile_clean_section called before init\n");
522 /* We only disable the section, so it will still be freed, but it */
523 /* won't be find by further walks of the structure */
525 for (; section
; section
= section
->link
){
526 if ( g_strcasecmp (section
->AppName
, appname
))
528 section
->AppName
[0] = 0;
532 int profile_has_section (const char *section_name
, const char *profile
)
536 /* We assume the user has called one of the other initialization funcs */
537 if (!find_loaded (profile
, §ion
)){
540 for (; section
; section
= section
->link
){
541 if ( g_strcasecmp (section
->AppName
, section_name
))
548 void profile_forget_profile (const char *file
)
552 for (p
= Base
; p
; p
= p
->link
){
553 if ( g_strcasecmp (file
, p
->FileName
))