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>
33 #define overflow (next == &CharBuffer [STRSIZE-1])
35 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyDefOnKey
, KeyValue
};
37 typedef struct TKeys
{
43 typedef struct TSecHeader
{
46 struct TSecHeader
*link
;
49 typedef struct TProfile
{
52 struct TProfile
*link
;
55 static TProfile
*Base
= 0;
58 find_loaded (char *FileName
, TSecHeader
** section
)
63 if (!g_strcasecmp (FileName
, p
->FileName
)) {
64 *section
= p
->Section
;
72 #define TRANSLATION_CHAR '\200'
75 str_untranslate_newline_dup (char *s
)
79 g_return_val_if_fail(s
, NULL
);
82 l
+= (*p
== '\n' || *p
== TRANSLATION_CHAR
);
85 q
= p
= g_malloc (l
+ 1);
91 *p
++ = TRANSLATION_CHAR
;
94 case TRANSLATION_CHAR
:
95 if (s
[1] == 'n' || s
[1] == TRANSLATION_CHAR
)
96 *p
++ = TRANSLATION_CHAR
;
97 *p
++ = TRANSLATION_CHAR
;
108 return 0; /* not reached */
112 str_translate_newline_dup (char *s
)
115 g_return_val_if_fail(s
,NULL
);
116 q
= p
= g_malloc (strlen (s
) + 1);
120 if (*s
== TRANSLATION_CHAR
) {
125 case TRANSLATION_CHAR
:
126 *p
++ = TRANSLATION_CHAR
;
129 *p
++ = TRANSLATION_CHAR
;
133 *p
++ = TRANSLATION_CHAR
;
142 return q
; /* not reached */
145 static TSecHeader
*load (char *file
)
149 TSecHeader
*SecHeader
= 0;
150 char CharBuffer
[STRSIZE
];
151 char *next
= ""; /* Not needed */
154 if ((f
= fopen (file
, "r"))==NULL
)
158 while ((c
= getc (f
)) != EOF
){
159 if (c
== '\r') /* Ignore Carriage Return */
165 if (c
== ']' || overflow
){
168 SecHeader
->AppName
= g_strdup (CharBuffer
);
188 SecHeader
= g_new (TSecHeader
, 1);
189 SecHeader
->link
= temp
;
195 if (state
== FirstBrace
) /* On first pass, don't allow dangling keys */
198 if ((c
== ' ' && state
!= KeyDefOnKey
) || c
== '\t')
201 if (c
== '\n' || overflow
) /* Abort Definition */
204 if (c
== '=' || overflow
){
207 temp
= SecHeader
->Keys
;
209 SecHeader
->Keys
=g_new (TKeys
, 1);
210 SecHeader
->Keys
->link
= temp
;
211 SecHeader
->Keys
->KeyName
= g_strdup (CharBuffer
);
221 if (overflow
|| c
== '\n'){
223 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
224 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
227 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
228 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
236 } /* while ((c = getc (f)) != EOF) */
237 if (c
== EOF
&& state
== KeyValue
){
239 SecHeader
->Keys
->Value
= str_translate_newline_dup (CharBuffer
);
245 static void new_key (TSecHeader
*section
, char *KeyName
, char *Value
)
249 key
= g_new (TKeys
, 1);
250 key
->KeyName
= g_strdup (KeyName
);
251 key
->Value
= g_strdup (Value
);
252 key
->link
= section
->Keys
;
257 GetSetProfileChar (int set
, const char *AppName
, char *KeyName
,
258 char *Default
, char *FileName
)
265 Current
= find_loaded (FileName
, §ion
);
267 Current
= g_new (TProfile
, 1);
268 Current
->link
= Base
;
269 Current
->FileName
= g_strdup (FileName
);
270 Current
->Section
= load (FileName
);
272 section
= Current
->Section
;
276 for (; section
; section
= section
->link
){
277 if (section
->AppName
== 0 || g_strcasecmp (section
->AppName
, AppName
))
279 for (key
= section
->Keys
; key
; key
= key
->link
){
280 if ( g_strcasecmp (key
->KeyName
, KeyName
))
284 key
->Value
= g_strdup (Default
);
288 /* If getting the information, then don't write the information
289 to the INI file, need to run a couple of tests with windog */
292 new_key (section
, KeyName
, Default
);
297 /* Non existent section */
299 section
= g_new (TSecHeader
, 1);
300 section
->AppName
= g_strdup (AppName
);
302 new_key (section
, KeyName
, Default
);
303 section
->link
= Current
->Section
;
304 Current
->Section
= section
;
309 static short GetSetProfile (int set
, const char * AppName
, char * KeyName
,
310 char * Default
, char * ReturnedString
,
311 short Size
, char * FileName
)
316 s
= GetSetProfileChar (set
, AppName
, KeyName
, Default
, FileName
);
318 ReturnedString
[Size
-1] = 0;
319 strncpy (ReturnedString
, s
, Size
-1);
324 short GetPrivateProfileString (const char * AppName
, char * KeyName
,
325 char * Default
, char * ReturnedString
,
326 short Size
, char * FileName
)
328 return (GetSetProfile (0, AppName
, KeyName
, Default
, ReturnedString
, Size
, FileName
));
331 char *get_profile_string (const char *AppName
, char *KeyName
, char *Default
,
334 return GetSetProfileChar (0, AppName
, KeyName
, Default
, FileName
);
337 int GetPrivateProfileInt (const char * AppName
, char * KeyName
, int Default
,
340 static char IntBuf
[BUF_TINY
];
341 static char buf
[BUF_TINY
];
343 g_snprintf (buf
, sizeof (buf
), "%d", Default
);
345 /* Check the exact semantic with the SDK */
346 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, BUF_TINY
, File
);
347 if (! g_strcasecmp (IntBuf
, "true"))
349 if (! g_strcasecmp (IntBuf
, "yes"))
351 return (int) atol (IntBuf
);
354 int WritePrivateProfileString (const char * AppName
, char * KeyName
, char * String
,
357 return GetSetProfile (1, AppName
, KeyName
, String
, "", 0, FileName
);
360 static void dump_keys (FILE * profile
, TKeys
* p
)
365 dump_keys (profile
, p
->link
);
366 t
= str_untranslate_newline_dup (p
->Value
);
367 fprintf (profile
, "%s=%s\n", p
->KeyName
, t
);
371 static void dump_sections (FILE *profile
, TSecHeader
*p
)
375 dump_sections (profile
, p
->link
);
377 fprintf (profile
, "\n[%s]\n", p
->AppName
);
378 dump_keys (profile
, p
->Keys
);
382 static void dump_profile (TProfile
*p
)
388 dump_profile (p
->link
);
389 /* .ado: p->FileName can be empty, it's better to jump over */
390 if (p
->FileName
[0] != (char) 0)
391 if ((profile
= fopen (p
->FileName
, "w")) != NULL
){
392 dump_sections (profile
, p
->Section
);
398 * Must be called at the end of wine run
401 void sync_profiles (void)
406 static void free_keys (TKeys
*p
)
416 static void free_sections (TSecHeader
*p
)
420 free_sections (p
->link
);
428 static void free_profile (TProfile
*p
)
432 free_profile (p
->link
);
433 free_sections (p
->Section
);
434 g_free (p
->FileName
);
438 void free_profile_name (char *s
)
445 for (p
= Base
; p
; p
= p
->link
){
446 if (strcmp (s
, p
->FileName
) == 0){
447 free_sections (p
->Section
);
455 void free_profiles (void)
460 void *profile_init_iterator (char *appname
, char *file
)
465 Current
= find_loaded (file
, §ion
);
467 Current
= g_new (TProfile
, 1);
468 Current
->link
= Base
;
469 Current
->FileName
= g_strdup (file
);
470 Current
->Section
= load (file
);
472 section
= Current
->Section
;
474 for (; section
; section
= section
->link
){
475 if ( g_strcasecmp (section
->AppName
, appname
))
477 return section
->Keys
;
482 void *profile_iterator_next (void *s
, char **key
, char **value
)
484 TKeys
*keys
= (TKeys
*) s
;
487 *key
= keys
->KeyName
;
488 *value
= keys
->Value
;
494 void profile_clean_section (char *appname
, char *file
)
498 /* We assume the user has called one of the other initialization funcs */
499 if (!find_loaded (file
, §ion
)){
500 fprintf (stderr
,"Warning: profile_clean_section called before init\n");
503 /* We only disable the section, so it will still be freed, but it */
504 /* won't be find by further walks of the structure */
506 for (; section
; section
= section
->link
){
507 if ( g_strcasecmp (section
->AppName
, appname
))
509 section
->AppName
[0] = 0;
513 int profile_has_section (char *section_name
, char *profile
)
517 /* We assume the user has called one of the other initialization funcs */
518 if (!find_loaded (profile
, §ion
)){
521 for (; section
; section
= section
->link
){
522 if ( g_strcasecmp (section
->AppName
, section_name
))
529 void profile_forget_profile (char *file
)
533 for (p
= Base
; p
; p
= p
->link
){
534 if ( g_strcasecmp (file
, p
->FileName
))