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
) /* Abort Definition */
202 if (c
== '=' || overflow
){
205 temp
= SecHeader
->Keys
;
207 SecHeader
->Keys
=g_new (TKeys
, 1);
208 SecHeader
->Keys
->link
= temp
;
209 SecHeader
->Keys
->KeyName
= g_strdup (CharBuffer
);
219 if (overflow
|| c
== '\n'){
221 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
222 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
225 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
226 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
234 } /* while ((c = getc (f)) != EOF) */
239 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
241 case OnSecHeader
: { /* Broken initialization file */
242 TSecHeader
*link
= SecHeader
->link
;
245 fprintf (stderr
, "Warning: Corrupted initialization file `%s'\n",
255 static void new_key (TSecHeader
*section
, const char *KeyName
, const char *Value
)
259 key
= g_new (TKeys
, 1);
260 key
->KeyName
= g_strdup (KeyName
);
261 key
->Value
= g_strdup (Value
);
262 key
->link
= section
->Keys
;
267 GetSetProfileChar (int set
, const char *AppName
, char *KeyName
,
268 char *Default
, char *FileName
)
275 Current
= find_loaded (FileName
, §ion
);
277 Current
= g_new (TProfile
, 1);
278 Current
->link
= Base
;
279 Current
->FileName
= g_strdup (FileName
);
280 Current
->Section
= load (FileName
);
282 section
= Current
->Section
;
286 for (; section
; section
= section
->link
){
287 if (section
->AppName
== 0 || g_strcasecmp (section
->AppName
, AppName
))
289 for (key
= section
->Keys
; key
; key
= key
->link
){
290 if ( g_strcasecmp (key
->KeyName
, KeyName
))
294 key
->Value
= g_strdup (Default
);
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 */
302 new_key (section
, KeyName
, Default
);
307 /* Non existent section */
309 section
= g_new (TSecHeader
, 1);
310 section
->AppName
= g_strdup (AppName
);
312 new_key (section
, KeyName
, Default
);
313 section
->link
= Current
->Section
;
314 Current
->Section
= section
;
319 static short GetSetProfile (int set
, const char * AppName
, char * KeyName
,
320 char * Default
, char * ReturnedString
,
321 short Size
, char * FileName
)
326 s
= GetSetProfileChar (set
, AppName
, KeyName
, Default
, FileName
);
328 ReturnedString
[Size
-1] = 0;
329 strncpy (ReturnedString
, s
, Size
-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
,
344 return GetSetProfileChar (0, AppName
, KeyName
, Default
, FileName
);
347 int GetPrivateProfileInt (const char * AppName
, char * KeyName
, int Default
,
350 char IntBuf
[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"))
359 if (! g_strcasecmp (IntBuf
, "yes"))
361 return (int) atol (IntBuf
);
364 int WritePrivateProfileString (const char * AppName
, char * KeyName
, char * String
,
367 return GetSetProfile (1, AppName
, KeyName
, String
, "", 0, FileName
);
370 static void dump_keys (FILE * profile
, TKeys
* p
)
375 dump_keys (profile
, p
->link
);
376 t
= str_untranslate_newline_dup (p
->Value
);
377 fprintf (profile
, "%s=%s\n", p
->KeyName
, t
);
381 static void dump_sections (FILE *profile
, TSecHeader
*p
)
385 dump_sections (profile
, p
->link
);
387 fprintf (profile
, "\n[%s]\n", p
->AppName
);
388 dump_keys (profile
, p
->Keys
);
392 static void dump_profile (TProfile
*p
)
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
);
408 * Must be called at the end of wine run
411 void sync_profiles (void)
416 static void free_keys (TKeys
*p
)
426 static void free_sections (TSecHeader
*p
)
430 free_sections (p
->link
);
438 static void free_profile (TProfile
*p
)
442 free_profile (p
->link
);
443 free_sections (p
->Section
);
444 g_free (p
->FileName
);
448 void free_profile_name (char *s
)
455 for (p
= Base
; p
; p
= p
->link
){
456 if (strcmp (s
, p
->FileName
) == 0){
457 free_sections (p
->Section
);
465 void free_profiles (void)
470 void *profile_init_iterator (char *appname
, char *file
)
475 Current
= find_loaded (file
, §ion
);
477 Current
= g_new (TProfile
, 1);
478 Current
->link
= Base
;
479 Current
->FileName
= g_strdup (file
);
480 Current
->Section
= load (file
);
482 section
= Current
->Section
;
484 for (; section
; section
= section
->link
){
485 if ( g_strcasecmp (section
->AppName
, appname
))
487 return section
->Keys
;
492 void *profile_iterator_next (void *s
, char **key
, char **value
)
494 TKeys
*keys
= (TKeys
*) s
;
497 *key
= keys
->KeyName
;
498 *value
= keys
->Value
;
504 void profile_clean_section (char *appname
, char *file
)
508 /* We assume the user has called one of the other initialization funcs */
509 if (!find_loaded (file
, §ion
)){
510 fprintf (stderr
,"Warning: profile_clean_section called before init\n");
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
))
519 section
->AppName
[0] = 0;
523 int profile_has_section (char *section_name
, char *profile
)
527 /* We assume the user has called one of the other initialization funcs */
528 if (!find_loaded (profile
, §ion
)){
531 for (; section
; section
= section
->link
){
532 if ( g_strcasecmp (section
->AppName
, section_name
))
539 void profile_forget_profile (char *file
)
543 for (p
= Base
; p
; p
= p
->link
){
544 if ( g_strcasecmp (file
, p
->FileName
))