2 * Initialization-File Functions.
4 * Copyright (c) 1993 Miguel de Icaza
6 * 1/Dec o Corrected return values for Get*ProfileString
8 * o Now, if AppName == NULL in Get*ProfileString it returns a list
9 * of the KeyNames (as documented in the MS-SDK).
11 * o if KeyValue == NULL now clears the value in Get*ProfileString
14 static char Copyright
[] = "Copyright (C) 1993 Miguel de Icaza";
22 #include "prototypes.h"
27 #define xmalloc(x) malloc(x)
28 #define overflow (next == &CharBuffer [STRSIZE-1])
30 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyValue
};
32 typedef struct TKeys
{
38 typedef struct TSecHeader
{
41 struct TSecHeader
*link
;
44 typedef struct TProfile
{
47 struct TProfile
*link
;
50 TProfile
*Current
= 0;
53 static TSecHeader
*is_loaded (char *FileName
)
58 if (!strcasecmp (FileName
, p
->FileName
)){
67 static TSecHeader
*load (char *file
)
71 TSecHeader
*SecHeader
= 0;
72 char CharBuffer
[STRSIZE
];
77 printf("Load %s\n", file
);
79 if ((f
= fopen (file
, "r"))==NULL
)
83 printf("Loading %s\n", file
);
86 while ((c
= getc (f
)) != EOF
){
87 if (c
== '\r') /* Ignore Carriage Return */
93 if (c
== ']' || overflow
){
96 SecHeader
->AppName
= strdup (CharBuffer
);
99 printf("%s: section %s\n", file
, CharBuffer
);
118 SecHeader
= (TSecHeader
*) xmalloc (sizeof (TSecHeader
));
119 SecHeader
->link
= temp
;
125 if (state
== FirstBrace
) /* On first pass, don't allow dangling keys */
128 if (c
== ' ' || c
== '\t')
131 if (c
== '\n' || c
== ';' || overflow
) /* Abort Definition */
140 if (c
== '=' || overflow
){
143 temp
= SecHeader
->Keys
;
145 SecHeader
->Keys
= (TKeys
*) xmalloc (sizeof (TKeys
));
146 SecHeader
->Keys
->link
= temp
;
147 SecHeader
->Keys
->KeyName
= strdup (CharBuffer
);
151 printf("%s: key %s\n", file
, CharBuffer
);
158 if (overflow
|| c
== '\n'){
160 SecHeader
->Keys
->Value
= strdup (CharBuffer
);
161 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
164 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
165 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
173 } /* while ((c = getc (f)) != EOF) */
177 static void new_key (TSecHeader
*section
, char *KeyName
, char *Value
)
181 key
= (TKeys
*) xmalloc (sizeof (TKeys
));
182 key
->KeyName
= strdup (KeyName
);
183 key
->Value
= strdup (Value
);
184 key
->link
= section
->Keys
;
188 static short GetSetProfile (int set
, LPSTR AppName
, LPSTR KeyName
,
189 LPSTR Default
, LPSTR ReturnedString
, short Size
,
197 if (!(section
= is_loaded (FileName
))){
198 New
= (TProfile
*) xmalloc (sizeof (TProfile
));
200 New
->FileName
= strdup (FileName
);
201 New
->Section
= load (FileName
);
203 section
= New
->Section
;
207 for (; section
; section
= section
->link
){
208 if (strcasecmp (section
->AppName
, AppName
))
211 /* If no key value given, then list all the keys */
212 if ((!AppName
) && (!set
)){
213 char *p
= ReturnedString
;
217 for (key
= section
->Keys
; key
; key
= key
->link
){
218 strncpy (p
, key
->KeyName
, left
);
219 slen
= strlen (key
->KeyName
) + 1;
225 for (key
= section
->Keys
; key
; key
= key
->link
){
226 if (strcasecmp (key
->KeyName
, KeyName
))
230 key
->Value
= strdup (Default
? Default
: "");
233 ReturnedString
[Size
-1] = 0;
234 strncpy (ReturnedString
, key
->Value
, Size
-1);
237 /* If Getting the information, then don't write the information
238 to the INI file, need to run a couple of tests with windog */
241 new_key (section
, KeyName
, Default
);
243 ReturnedString
[Size
-1] = 0;
244 strncpy (ReturnedString
, Default
, Size
-1);
248 /* Non existent section */
250 section
= (TSecHeader
*) xmalloc (sizeof (TSecHeader
));
251 section
->AppName
= strdup (AppName
);
253 new_key (section
, KeyName
, Default
);
254 section
->link
= Current
->Section
;
255 Current
->Section
= section
;
257 ReturnedString
[Size
-1] = 0;
258 strncpy (ReturnedString
, Default
, Size
-1);
263 short GetPrivateProfileString (LPSTR AppName
, LPSTR KeyName
,
264 LPSTR Default
, LPSTR ReturnedString
,
265 short Size
, LPSTR FileName
)
269 v
= GetSetProfile (0,AppName
,KeyName
,Default
,ReturnedString
,Size
,FileName
);
271 return strlen (ReturnedString
);
276 int GetProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR Default
,
277 LPSTR ReturnedString
, int Size
)
279 return GetPrivateProfileString (AppName
, KeyName
, Default
,
280 ReturnedString
, Size
, WIN_INI
);
283 WORD
GetPrivateProfileInt (LPSTR AppName
, LPSTR KeyName
, short Default
,
286 static char IntBuf
[5];
289 sprintf (buf
, "%d", Default
);
291 /* Check the exact semantic with the SDK */
292 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, 5, File
);
293 if (!strcasecmp (IntBuf
, "true"))
295 if (!strcasecmp (IntBuf
, "yes"))
297 return atoi (IntBuf
);
300 WORD
GetProfileInt (LPSTR AppName
, LPSTR KeyName
, int Default
)
302 return GetPrivateProfileInt (AppName
, KeyName
, Default
, WIN_INI
);
305 BOOL
WritePrivateProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR String
,
308 return GetSetProfile (1, AppName
, KeyName
, String
, "", 0, FileName
);
311 BOOL
WriteProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR String
)
313 return (WritePrivateProfileString (AppName
, KeyName
, String
, WIN_INI
));
316 static void dump_keys (FILE *profile
, TKeys
*p
)
320 dump_keys (profile
, p
->link
);
321 fprintf (profile
, "%s=%s\r\n", p
->KeyName
, p
->Value
);
324 static void dump_sections (FILE *profile
, TSecHeader
*p
)
328 dump_sections (profile
, p
->link
);
329 fprintf (profile
, "\r\n[%s]\r\n", p
->AppName
);
330 dump_keys (profile
, p
->Keys
);
333 static void dump_profile (TProfile
*p
)
339 dump_profile (p
->link
);
340 if ((profile
= fopen (p
->FileName
, "w")) != NULL
){
341 dump_sections (profile
, p
->Section
);
346 void sync_profiles ()