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.
26 #include <sys/types.h>
31 #define overflow (next == &CharBuffer [STRSIZE-1])
33 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyDefOnKey
, KeyValue
};
35 typedef struct TKeys
{
41 typedef struct TSecHeader
{
44 struct TSecHeader
*link
;
47 typedef struct TProfile
{
50 struct TProfile
*link
;
53 static TProfile
*Base
= 0;
56 find_loaded (const char *FileName
, TSecHeader
** section
)
61 if (!g_strcasecmp (FileName
, p
->FileName
)) {
62 *section
= p
->Section
;
70 #define TRANSLATION_CHAR '\200'
73 str_untranslate_newline_dup (char *s
)
77 g_return_val_if_fail(s
, NULL
);
80 l
+= (*p
== '\n' || *p
== TRANSLATION_CHAR
);
83 q
= p
= g_malloc (l
+ 1);
89 *p
++ = TRANSLATION_CHAR
;
92 case TRANSLATION_CHAR
:
93 if (s
[1] == 'n' || s
[1] == TRANSLATION_CHAR
)
94 *p
++ = TRANSLATION_CHAR
;
95 *p
++ = TRANSLATION_CHAR
;
106 return 0; /* not reached */
110 str_translate_newline_dup (char *s
)
113 g_return_val_if_fail(s
,NULL
);
114 q
= p
= g_malloc (strlen (s
) + 1);
118 if (*s
== TRANSLATION_CHAR
) {
123 case TRANSLATION_CHAR
:
124 *p
++ = TRANSLATION_CHAR
;
127 *p
++ = TRANSLATION_CHAR
;
131 *p
++ = TRANSLATION_CHAR
;
140 return q
; /* not reached */
143 static TSecHeader
*load (const char *file
)
147 TSecHeader
*SecHeader
= 0;
148 char CharBuffer
[STRSIZE
];
149 char *next
= ""; /* Not needed */
152 if ((f
= fopen (file
, "r"))==NULL
)
156 while ((c
= getc (f
)) != EOF
){
157 if (c
== '\r') /* Ignore Carriage Return */
163 if (c
== ']' || overflow
){
166 SecHeader
->AppName
= g_strdup (CharBuffer
);
186 SecHeader
= g_new (TSecHeader
, 1);
187 SecHeader
->link
= temp
;
193 if (state
== FirstBrace
) /* On first pass, don't allow dangling keys */
196 if ((c
== ' ' && state
!= KeyDefOnKey
) || c
== '\t')
199 if (c
== '\n' || overflow
) {
200 /* Abort Definition */
205 if (c
== '=' || overflow
){
208 temp
= SecHeader
->Keys
;
210 SecHeader
->Keys
=g_new (TKeys
, 1);
211 SecHeader
->Keys
->link
= temp
;
212 SecHeader
->Keys
->KeyName
= g_strdup (CharBuffer
);
222 if (overflow
|| c
== '\n'){
224 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
225 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
228 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
229 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
237 } /* while ((c = getc (f)) != EOF) */
242 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
244 case OnSecHeader
: { /* Broken initialization file */
245 TSecHeader
*link
= SecHeader
->link
;
248 fprintf (stderr
, "Warning: Corrupted initialization file `%s'\n",
258 static void new_key (TSecHeader
*section
, const char *KeyName
, const char *Value
)
262 key
= g_new (TKeys
, 1);
263 key
->KeyName
= g_strdup (KeyName
);
264 key
->Value
= g_strdup (Value
);
265 key
->link
= section
->Keys
;
270 GetSetProfileChar (int set
, const char *AppName
, char *KeyName
,
271 char *Default
, char *FileName
)
278 Current
= find_loaded (FileName
, §ion
);
280 Current
= g_new (TProfile
, 1);
281 Current
->link
= Base
;
282 Current
->FileName
= g_strdup (FileName
);
283 Current
->Section
= load (FileName
);
285 section
= Current
->Section
;
289 for (; section
; section
= section
->link
){
290 if (section
->AppName
== 0 || g_strcasecmp (section
->AppName
, AppName
))
292 for (key
= section
->Keys
; key
; key
= key
->link
){
293 if ( g_strcasecmp (key
->KeyName
, KeyName
))
297 key
->Value
= g_strdup (Default
);
301 /* If getting the information, then don't write the information
302 to the INI file, need to run a couple of tests with windog */
305 new_key (section
, KeyName
, Default
);
310 /* Non existent section */
312 section
= g_new (TSecHeader
, 1);
313 section
->AppName
= g_strdup (AppName
);
315 new_key (section
, KeyName
, Default
);
316 section
->link
= Current
->Section
;
317 Current
->Section
= section
;
322 static short GetSetProfile (int set
, const char * AppName
, char * KeyName
,
323 char * Default
, char * ReturnedString
,
324 short Size
, char * FileName
)
329 s
= GetSetProfileChar (set
, AppName
, KeyName
, Default
, FileName
);
331 ReturnedString
[Size
-1] = 0;
332 g_strlcpy (ReturnedString
, s
, Size
-1);
337 short GetPrivateProfileString (const char * AppName
, char * KeyName
,
338 char * Default
, char * ReturnedString
,
339 short Size
, char * FileName
)
341 return (GetSetProfile (0, AppName
, KeyName
, Default
, ReturnedString
, Size
, FileName
));
344 char *get_profile_string (const char *AppName
, char *KeyName
, char *Default
,
347 return GetSetProfileChar (0, AppName
, KeyName
, Default
, FileName
);
350 int GetPrivateProfileInt (const char * AppName
, char * KeyName
, int Default
,
353 char IntBuf
[BUF_TINY
];
356 g_snprintf (buf
, sizeof (buf
), "%d", Default
);
358 /* Check the exact semantic with the SDK */
359 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, BUF_TINY
, File
);
360 if (! g_strcasecmp (IntBuf
, "true"))
362 if (! g_strcasecmp (IntBuf
, "yes"))
364 return (int) atol (IntBuf
);
367 int WritePrivateProfileString (const char * AppName
, char * KeyName
, char * String
,
370 return GetSetProfile (1, AppName
, KeyName
, String
, "", 0, FileName
);
373 static void dump_keys (FILE * profile
, TKeys
* p
)
378 dump_keys (profile
, p
->link
);
379 t
= str_untranslate_newline_dup (p
->Value
);
380 fprintf (profile
, "%s=%s\n", p
->KeyName
, t
);
384 static void dump_sections (FILE *profile
, TSecHeader
*p
)
388 dump_sections (profile
, p
->link
);
390 fprintf (profile
, "\n[%s]\n", p
->AppName
);
391 dump_keys (profile
, p
->Keys
);
395 static void dump_profile (TProfile
*p
)
401 dump_profile (p
->link
);
402 /* .ado: p->FileName can be empty, it's better to jump over */
403 if (p
->FileName
[0] != (char) 0)
404 if ((profile
= fopen (p
->FileName
, "w")) != NULL
){
405 dump_sections (profile
, p
->Section
);
411 * Must be called at the end of wine run
414 void sync_profiles (void)
419 static void free_keys (TKeys
*p
)
429 static void free_sections (TSecHeader
*p
)
433 free_sections (p
->link
);
441 static void free_profile (TProfile
*p
)
445 free_profile (p
->link
);
446 free_sections (p
->Section
);
447 g_free (p
->FileName
);
451 void free_profile_name (char *s
)
458 for (p
= Base
; p
; p
= p
->link
){
459 if (strcmp (s
, p
->FileName
) == 0){
460 free_sections (p
->Section
);
468 void free_profiles (void)
473 void *profile_init_iterator (char *appname
, char *file
)
478 Current
= find_loaded (file
, §ion
);
480 Current
= g_new (TProfile
, 1);
481 Current
->link
= Base
;
482 Current
->FileName
= g_strdup (file
);
483 Current
->Section
= load (file
);
485 section
= Current
->Section
;
487 for (; section
; section
= section
->link
){
488 if ( g_strcasecmp (section
->AppName
, appname
))
490 return section
->Keys
;
495 void *profile_iterator_next (void *s
, char **key
, char **value
)
497 TKeys
*keys
= (TKeys
*) s
;
500 *key
= keys
->KeyName
;
501 *value
= keys
->Value
;
507 void profile_clean_section (const char *appname
, char *file
)
511 /* We assume the user has called one of the other initialization funcs */
512 if (!find_loaded (file
, §ion
)){
513 fprintf (stderr
,"Warning: profile_clean_section called before init\n");
516 /* We only disable the section, so it will still be freed, but it */
517 /* won't be find by further walks of the structure */
519 for (; section
; section
= section
->link
){
520 if ( g_strcasecmp (section
->AppName
, appname
))
522 section
->AppName
[0] = 0;
526 int profile_has_section (char *section_name
, char *profile
)
530 /* We assume the user has called one of the other initialization funcs */
531 if (!find_loaded (profile
, §ion
)){
534 for (; section
; section
= section
->link
){
535 if ( g_strcasecmp (section
->AppName
, section_name
))
542 void profile_forget_profile (char *file
)
546 for (p
= Base
; p
; p
= p
->link
){
547 if ( g_strcasecmp (file
, p
->FileName
))