2 * Initialization-File Functions.
4 * Copyright (c) 1993 Miguel de Icaza
8 static char Copyright
[] = "Copyright (C) 1993 Miguel de Icaza";
15 #define INIFILE GetSystemIniFilename()
17 #define xmalloc(x) malloc(x)
18 #define overflow (next == &CharBuffer [STRSIZE-1])
20 enum { FirstBrace
, OnSecHeader
, IgnoreToEOL
, KeyDef
, KeyValue
};
22 typedef struct TKeys
{
28 typedef struct TSecHeader
{
31 struct TSecHeader
*link
;
34 typedef struct TProfile
{
37 struct TProfile
*link
;
40 TProfile
*Current
= 0;
43 static TSecHeader
*is_loaded (char *FileName
)
48 if (!strcasecmp (FileName
, p
->FileName
)){
57 static TSecHeader
*load (char *file
)
61 TSecHeader
*SecHeader
= 0;
62 char CharBuffer
[STRSIZE
];
66 if ((f
= fopen (file
, "r"))==NULL
)
70 while ((c
= getc (f
)) != EOF
){
71 if (c
== '\r') /* Ignore Carriage Return */
77 if (c
== ']' || overflow
){
80 SecHeader
->AppName
= strdup (CharBuffer
);
99 SecHeader
= (TSecHeader
*) xmalloc (sizeof (TSecHeader
));
100 SecHeader
->link
= temp
;
106 if (state
== FirstBrace
) /* On first pass, don't allow dangling keys */
109 if (c
== ' ' || c
== '\t')
112 if (c
== '\n' || overflow
) /* Abort Definition */
115 if (c
== '=' || overflow
){
118 temp
= SecHeader
->Keys
;
120 SecHeader
->Keys
= (TKeys
*) xmalloc (sizeof (TKeys
));
121 SecHeader
->Keys
->link
= temp
;
122 SecHeader
->Keys
->KeyName
= strdup (CharBuffer
);
130 if (overflow
|| c
== '\n'){
132 SecHeader
->Keys
->Value
= strdup (CharBuffer
);
133 state
= c
== '\n' ? KeyDef
: IgnoreToEOL
;
136 printf ("[%s] (%s)=%s\n", SecHeader
->AppName
,
137 SecHeader
->Keys
->KeyName
, SecHeader
->Keys
->Value
);
145 } /* while ((c = getc (f)) != EOF) */
149 static new_key (TSecHeader
*section
, char *KeyName
, char *Value
)
153 key
= (TKeys
*) xmalloc (sizeof (TKeys
));
154 key
->KeyName
= strdup (KeyName
);
155 key
->Value
= strdup (Value
);
156 key
->link
= section
->Keys
;
160 static short GetSetProfile (int set
, LPSTR AppName
, LPSTR KeyName
,
161 LPSTR Default
, LPSTR ReturnedString
, short Size
,
169 if (!(section
= is_loaded (FileName
))){
170 New
= (TProfile
*) xmalloc (sizeof (TProfile
));
172 New
->FileName
= strdup (FileName
);
173 New
->Section
= load (FileName
);
175 section
= New
->Section
;
180 for (; section
; section
= section
->link
){
181 if (strcasecmp (section
->AppName
, AppName
))
183 for (key
= section
->Keys
; key
; key
= key
->link
){
184 if (strcasecmp (key
->KeyName
, KeyName
))
188 key
->Value
= strdup (Default
);
191 ReturnedString
[Size
-1] = 0;
192 strncpy (ReturnedString
, key
->Value
, Size
-1);
195 /* If Getting the information, then don't write the information
196 to the INI file, need to run a couple of tests with windog */
199 new_key (section
, KeyName
, Default
);
201 ReturnedString
[Size
-1] = 0;
202 strncpy (ReturnedString
, Default
, Size
-1);
206 /* Non existent section */
208 section
= (TSecHeader
*) xmalloc (sizeof (TSecHeader
));
209 section
->AppName
= strdup (AppName
);
211 new_key (section
, KeyName
, Default
);
212 section
->link
= Current
->Section
;
213 Current
->Section
= section
;
215 ReturnedString
[Size
-1] = 0;
216 strncpy (ReturnedString
, Default
, Size
-1);
221 short GetPrivateProfileString (LPSTR AppName
, LPSTR KeyName
,
222 LPSTR Default
, LPSTR ReturnedString
,
223 short Size
, LPSTR FileName
)
225 return (GetSetProfile (0, AppName
, KeyName
, Default
, ReturnedString
, Size
, FileName
));
228 int GetProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR Default
,
229 LPSTR ReturnedString
, int Size
)
231 return GetPrivateProfileString (AppName
, KeyName
, Default
,
232 ReturnedString
, Size
, INIFILE
);
235 WORD
GetPrivateProfileInt (LPSTR AppName
, LPSTR KeyName
, short Default
,
238 static char IntBuf
[5];
241 sprintf (buf
, "%d", Default
);
243 /* Check the exact semantic with the SDK */
244 GetPrivateProfileString (AppName
, KeyName
, buf
, IntBuf
, 5, File
);
245 if (!strcasecmp (IntBuf
, "true"))
247 if (!strcasecmp (IntBuf
, "yes"))
249 return atoi (IntBuf
);
252 WORD
GetProfileInt (LPSTR AppName
, LPSTR KeyName
, int Default
)
254 return GetPrivateProfileInt (AppName
, KeyName
, Default
, INIFILE
);
257 BOOL
WritePrivateProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR String
,
260 return GetSetProfile (1, AppName
, KeyName
, String
, "", 0, FileName
);
263 BOOL
WriteProfileString (LPSTR AppName
, LPSTR KeyName
, LPSTR String
)
265 return (WritePrivateProfileString (AppName
, KeyName
, String
, INIFILE
));
268 static void dump_keys (FILE *profile
, TKeys
*p
)
272 dump_keys (profile
, p
->link
);
273 fprintf (profile
, "%s=%s\r\n", p
->KeyName
, p
->Value
);
276 static void dump_sections (FILE *profile
, TSecHeader
*p
)
280 dump_sections (profile
, p
->link
);
281 fprintf (profile
, "\r\n[%s]\r\n", p
->AppName
);
282 dump_keys (profile
, p
->Keys
);
285 static void dump_profile (TProfile
*p
)
291 dump_profile (p
->link
);
292 if ((profile
= fopen (p
->FileName
, "w")) != NULL
){
293 dump_sections (profile
, p
->Section
);
298 void sync_profiles ()