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.
28 #include <sys/types.h>
32 #define INIFILE "xxx.ini"
34 #define overflow (next == &CharBuffer [STRSIZE-1])
36 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyDefOnKey
, KeyValue
};
38 typedef struct TKeys
{
44 typedef struct TSecHeader
{
47 struct TSecHeader
*link
;
50 typedef struct TProfile
{
53 struct TProfile
*link
;
56 TProfile
*Current
= 0;
59 static int is_loaded (char *FileName
, TSecHeader
**section
)
64 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 (char *file
)
151 TSecHeader
*SecHeader
= 0;
152 char CharBuffer
[STRSIZE
];
153 char *next
= ""; /* 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
) /* Abort Definition */
206 if (c
== '=' || overflow
){
209 temp
= SecHeader
->Keys
;
211 SecHeader
->Keys
=g_new (TKeys
, 1);
212 SecHeader
->Keys
->link
= temp
;
213 SecHeader
->Keys
->KeyName
= g_strdup (CharBuffer
);
223 if (overflow
|| c
== '\n'){
225 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
226 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
229 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
230 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
238 } /* while ((c = getc (f)) != EOF) */
239 if (c
== EOF
&& state
== KeyValue
){
241 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
247 static void new_key (TSecHeader
*section
, char *KeyName
, char *Value
)
251 key
= g_new (TKeys
, 1);
252 key
->KeyName
= g_strdup (KeyName
);
253 key
->Value
= g_strdup (Value
);
254 key
->link
= section
->Keys
;
259 GetSetProfileChar (int set
, char *AppName
, char *KeyName
,
260 char *Default
, char *FileName
)
267 if (!is_loaded (FileName
, §ion
)){
268 New
= g_new (TProfile
, 1);
270 New
->FileName
= g_strdup (FileName
);
271 New
->Section
= load (FileName
);
273 section
= New
->Section
;
278 for (; section
; section
= section
->link
){
279 if (section
->AppName
== 0 || g_strcasecmp (section
->AppName
, AppName
))
281 for (key
= section
->Keys
; key
; key
= key
->link
){
282 if ( g_strcasecmp (key
->KeyName
, KeyName
))
286 key
->Value
= g_strdup (Default
);
290 /* If getting the information, then don't write the information
291 to the INI file, need to run a couple of tests with windog */
294 new_key (section
, KeyName
, Default
);
299 /* Non existent section */
301 section
= g_new (TSecHeader
, 1);
302 section
->AppName
= g_strdup (AppName
);
304 new_key (section
, KeyName
, Default
);
305 section
->link
= Current
->Section
;
306 Current
->Section
= section
;
311 static short GetSetProfile (int set
, char * AppName
, char * KeyName
,
312 char * Default
, char * ReturnedString
,
313 short Size
, char * FileName
)
318 s
= GetSetProfileChar (set
, AppName
, KeyName
, Default
, FileName
);
320 ReturnedString
[Size
-1] = 0;
321 strncpy (ReturnedString
, s
, Size
-1);
326 short GetPrivateProfileString (char * AppName
, char * KeyName
,
327 char * Default
, char * ReturnedString
,
328 short Size
, char * FileName
)
330 return (GetSetProfile (0, AppName
, KeyName
, Default
, ReturnedString
, Size
, FileName
));
333 char *get_profile_string (char *AppName
, char *KeyName
, char *Default
,
336 return GetSetProfileChar (0, AppName
, KeyName
, Default
, FileName
);
340 int GetProfileString (char * AppName
, char * KeyName
, char * Default
,
341 char * ReturnedString
, int Size
)
343 return GetPrivateProfileString (AppName
, KeyName
, Default
,
344 ReturnedString
, Size
, INIFILE
);
348 int GetPrivateProfileInt (char * AppName
, char * KeyName
, int Default
,
351 static char IntBuf
[BUF_TINY
];
352 static char buf
[BUF_TINY
];
354 g_snprintf (buf
, sizeof (buf
), "%d", Default
);
356 /* Check the exact semantic with the SDK */
357 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, BUF_TINY
, File
);
358 if (! g_strcasecmp (IntBuf
, "true"))
360 if (! g_strcasecmp (IntBuf
, "yes"))
362 return (int) atol (IntBuf
);
366 int GetProfileInt (char * AppName
, char * KeyName
, int Default
)
368 return GetPrivateProfileInt (AppName
, KeyName
, Default
, INIFILE
);
372 int WritePrivateProfileString (char * AppName
, char * KeyName
, char * String
,
375 return GetSetProfile (1, AppName
, KeyName
, String
, "", 0, FileName
);
379 int WriteProfileString (char * AppName
, char * KeyName
, char * String
)
381 return (WritePrivateProfileString (AppName
, KeyName
, String
, INIFILE
));
385 static void dump_keys (FILE * profile
, TKeys
* p
)
390 dump_keys (profile
, p
->link
);
391 t
= str_untranslate_newline_dup (p
->Value
);
392 fprintf (profile
, "%s=%s\n", p
->KeyName
, t
);
396 static void dump_sections (FILE *profile
, TSecHeader
*p
)
400 dump_sections (profile
, p
->link
);
402 fprintf (profile
, "\n[%s]\n", p
->AppName
);
403 dump_keys (profile
, p
->Keys
);
407 static void dump_profile (TProfile
*p
)
413 dump_profile (p
->link
);
414 /* .ado: p->FileName can be empty, it's better to jump over */
415 if (p
->FileName
[0] != (char) 0)
416 if ((profile
= fopen (p
->FileName
, "w")) != NULL
){
417 dump_sections (profile
, p
->Section
);
423 * Must be called at the end of wine run
426 void sync_profiles (void)
431 static void free_keys (TKeys
*p
)
441 static void free_sections (TSecHeader
*p
)
445 free_sections (p
->link
);
453 static void free_profile (TProfile
*p
)
457 free_profile (p
->link
);
458 free_sections (p
->Section
);
459 g_free (p
->FileName
);
463 void free_profile_name (char *s
)
470 for (p
= Base
; p
; p
= p
->link
){
471 if (strcmp (s
, p
->FileName
) == 0){
472 free_sections (p
->Section
);
480 void free_profiles (void)
485 void *profile_init_iterator (char *appname
, char *file
)
490 if (!is_loaded (file
, §ion
)){
491 New
= g_new (TProfile
, 1);
493 New
->FileName
= g_strdup (file
);
494 New
->Section
= load (file
);
496 section
= New
->Section
;
499 for (; section
; section
= section
->link
){
500 if ( g_strcasecmp (section
->AppName
, appname
))
502 return section
->Keys
;
507 void *profile_iterator_next (void *s
, char **key
, char **value
)
509 TKeys
*keys
= (TKeys
*) s
;
512 *key
= keys
->KeyName
;
513 *value
= keys
->Value
;
519 void profile_clean_section (char *appname
, char *file
)
523 /* We assume the user has called one of the other initialization funcs */
524 if (!is_loaded (file
, §ion
)){
525 fprintf (stderr
,"Warning: profile_clean_section called before init\n");
528 /* We only disable the section, so it will still be freed, but it */
529 /* won't be find by further walks of the structure */
531 for (; section
; section
= section
->link
){
532 if ( g_strcasecmp (section
->AppName
, appname
))
534 section
->AppName
[0] = 0;
538 int profile_has_section (char *section_name
, char *profile
)
542 /* We assume the user has called one of the other initialization funcs */
543 if (!is_loaded (profile
, §ion
)){
546 for (; section
; section
= section
->link
){
547 if ( g_strcasecmp (section
->AppName
, section_name
))
554 void profile_forget_profile (char *file
)
558 for (p
= Base
; p
; p
= p
->link
){
559 if ( g_strcasecmp (file
, p
->FileName
))