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
13 * 20/Apr SL - I'm not sure where these definitions came from, but my SDK
14 * has a NULL KeyValue returning a list of KeyNames, and a NULL
15 * AppName undefined. I changed GetSetProfile to match. This makes
16 * PROGMAN.EXE do the right thing.
19 static char Copyright
[] = "Copyright (C) 1993 Miguel de Icaza";
27 #include "prototypes.h"
32 #define xmalloc(x) malloc(x)
33 #define overflow (next == &CharBuffer [STRSIZE-1])
35 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyValue
};
37 typedef struct TKeys
{
43 typedef struct TSecHeader
{
46 struct TSecHeader
*link
;
49 typedef struct TProfile
{
52 struct TProfile
*link
;
55 TProfile
*Current
= 0;
58 static TSecHeader
*is_loaded (char *FileName
)
63 if (!strcasecmp (FileName
, p
->FileName
)){
72 static char *GetIniFileName(char *name
)
76 if (strchr(name
, '/'))
79 if (strchr(name
, '\\'))
80 return GetUnixFileName(name
);
82 GetWindowsDirectory(temp
, sizeof(temp
) );
86 return GetUnixFileName(temp
);
89 static TSecHeader
*load (char *filename
)
93 TSecHeader
*SecHeader
= 0;
94 char CharBuffer
[STRSIZE
];
98 file
= GetIniFileName(filename
);
101 printf("Load %s\n", file
);
103 if ((f
= fopen (file
, "r"))==NULL
)
107 printf("Loading %s\n", file
);
112 while ((c
= getc (f
)) != EOF
){
113 if (c
== '\r') /* Ignore Carriage Return */
119 if (c
== ']' || overflow
){
122 SecHeader
->AppName
= strdup (CharBuffer
);
125 printf("%s: section %s\n", file
, CharBuffer
);
144 SecHeader
= (TSecHeader
*) xmalloc (sizeof (TSecHeader
));
145 SecHeader
->link
= temp
;
151 if (state
== FirstBrace
) /* On first pass, don't allow dangling keys */
154 if (c
== ' ' || c
== '\t')
157 if (c
== '\n' || c
== ';' || overflow
) /* Abort Definition */
166 if (c
== '=' || overflow
){
169 temp
= SecHeader
->Keys
;
171 SecHeader
->Keys
= (TKeys
*) xmalloc (sizeof (TKeys
));
172 SecHeader
->Keys
->link
= temp
;
173 SecHeader
->Keys
->KeyName
= strdup (CharBuffer
);
177 printf("%s: key %s\n", file
, CharBuffer
);
184 if (overflow
|| c
== '\n'){
186 SecHeader
->Keys
->Value
= strdup (CharBuffer
);
187 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
190 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
191 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
199 } /* while ((c = getc (f)) != EOF) */
203 static void new_key (TSecHeader
*section
, char *KeyName
, char *Value
)
207 key
= (TKeys
*) xmalloc (sizeof (TKeys
));
208 key
->KeyName
= strdup (KeyName
);
209 key
->Value
= strdup (Value
);
210 key
->link
= section
->Keys
;
214 static short GetSetProfile (int set
, LPSTR AppName
, LPSTR KeyName
,
215 LPSTR Default
, LPSTR ReturnedString
, short Size
,
223 /* Supposedly Default should NEVER be NULL. But sometimes it is. */
227 if (!(section
= is_loaded (FileName
))){
228 New
= (TProfile
*) xmalloc (sizeof (TProfile
));
230 New
->FileName
= strdup (FileName
);
231 New
->Section
= load (FileName
);
233 section
= New
->Section
;
237 for (; section
; section
= section
->link
){
238 if (strcasecmp (section
->AppName
, AppName
))
241 /* If no key value given, then list all the keys */
242 if ((!KeyName
) && (!set
)){
243 char *p
= ReturnedString
;
247 printf("GetSetProfile // KeyName == NULL, Enumeration !\n");
249 for (key
= section
->Keys
; key
; key
= key
->link
){
251 printf("GetSetProfile // No more storage for enum !\n");
254 slen
= min(strlen(key
->KeyName
) + 1, left
);
256 printf("GetSetProfile // strncpy(%08X, %08X, %d);\n",
257 ReturnedString
, key
->Value
, slen
);
259 strncpy (p
, key
->KeyName
, slen
);
261 printf("GetSetProfile // enum '%s' !\n", p
);
267 printf("GetSetProfile // normal end of enum !\n");
268 return (Size
- 2 - left
);
270 for (key
= section
->Keys
; key
; key
= key
->link
){
271 if (strcasecmp (key
->KeyName
, KeyName
))
275 key
->Value
= strdup (Default
? Default
: "");
278 ReturnedString
[Size
-1] = 0;
279 strncpy (ReturnedString
, key
->Value
, Size
-1);
282 /* If Getting the information, then don't write the information
283 to the INI file, need to run a couple of tests with windog */
286 new_key (section
, KeyName
, Default
);
288 ReturnedString
[Size
-1] = 0;
289 strncpy (ReturnedString
, Default
, Size
-1);
293 /* Non existent section */
295 section
= (TSecHeader
*) xmalloc (sizeof (TSecHeader
));
296 section
->AppName
= strdup (AppName
);
298 new_key (section
, KeyName
, Default
);
299 section
->link
= Current
->Section
;
300 Current
->Section
= section
;
302 ReturnedString
[Size
-1] = 0;
303 strncpy (ReturnedString
, Default
, Size
-1);
308 short GetPrivateProfileString (LPSTR AppName
, LPSTR KeyName
,
309 LPSTR Default
, LPSTR ReturnedString
,
310 short Size
, LPSTR FileName
)
315 printf("GetPrivateProfileString ('%s', '%s', '%s', %08X, %d, %s\n",
316 AppName
, KeyName
, Default
, ReturnedString
, Size
, FileName
);
318 v
= GetSetProfile (0,AppName
,KeyName
,Default
,ReturnedString
,Size
,FileName
);
320 return strlen (ReturnedString
);
325 int GetProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR Default
,
326 LPSTR ReturnedString
, int Size
)
328 return GetPrivateProfileString (AppName
, KeyName
, Default
,
329 ReturnedString
, Size
, WIN_INI
);
332 WORD
GetPrivateProfileInt (LPSTR AppName
, LPSTR KeyName
, short Default
,
335 static char IntBuf
[6];
338 sprintf (buf
, "%d", Default
);
340 /* Check the exact semantic with the SDK */
341 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, 6, File
);
342 if (!strcasecmp (IntBuf
, "true"))
344 if (!strcasecmp (IntBuf
, "yes"))
346 return atoi (IntBuf
);
349 WORD
GetProfileInt (LPSTR AppName
, LPSTR KeyName
, int Default
)
351 return GetPrivateProfileInt (AppName
, KeyName
, Default
, WIN_INI
);
354 BOOL
WritePrivateProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR String
,
357 return GetSetProfile (1, AppName
, KeyName
, String
, "", 0, FileName
);
360 BOOL
WriteProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR String
)
362 return (WritePrivateProfileString (AppName
, KeyName
, String
, WIN_INI
));
365 static void dump_keys (FILE *profile
, TKeys
*p
)
369 dump_keys (profile
, p
->link
);
370 fprintf (profile
, "%s=%s\r\n", p
->KeyName
, p
->Value
);
373 static void dump_sections (FILE *profile
, TSecHeader
*p
)
377 dump_sections (profile
, p
->link
);
378 fprintf (profile
, "\r\n[%s]\r\n", p
->AppName
);
379 dump_keys (profile
, p
->Keys
);
382 static void dump_profile (TProfile
*p
)
388 dump_profile (p
->link
);
389 if ((profile
= fopen (GetIniFileName(p
->FileName
), "w")) != NULL
){
390 dump_sections (profile
, p
->Section
);
395 void sync_profiles (void)