#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include "profile.h"
Go to the source code of this file.
Data Structures | |
struct | TKeys |
struct | TSecHeader |
struct | TProfile |
Defines | |
#define | xmalloc(x, y) malloc (x) |
#define | INIFILE "win.ini" |
#define | STRSIZE 255 |
#define | overflow (next == &CharBuffer [STRSIZE-1]) |
Enumerations | |
enum | { FirstBrace, OnSecHeader, IgnoreToEOL, KeyDef, KeyValue, FirstChar } |
Functions | |
static int | is_loaded (char *FileName, TSecHeader **section) |
static TSecHeader * | load (char *file) |
static void | new_key (TSecHeader *section, char *KeyName, char *Value) |
char * | GetSetProfileChar (int set, char *AppName, char *KeyName, char *Default, char *FileName) |
short | GetSetProfile (int set, char *AppName, char *KeyName, char *Default, char *ReturnedString, short Size, char *FileName) |
short | GetPrivateProfileString (char *AppName, char *KeyName, char *Default, char *ReturnedString, short Size, char *FileName) |
char * | get_profile_string (char *AppName, char *KeyName, char *Default, char *FileName) |
int | GetProfileString (char *AppName, char *KeyName, char *Default, char *ReturnedString, int Size) |
int | GetPrivateProfileInt (char *AppName, char *KeyName, short Default, char *File) |
int | GetProfileInt (char *AppName, char *KeyName, int Default) |
int | WritePrivateProfileString (char *AppName, char *KeyName, char *String, char *FileName) |
int | WriteProfileString (char *AppName, char *KeyName, char *String) |
static void | dump_keys (FILE *profile, TKeys *p) |
static void | dump_sections (FILE *profile, TSecHeader *p) |
static void | dump_profile (TProfile *p) |
void | sync_profiles (void) |
static void | free_keys (TKeys *p) |
static void | free_sections (TSecHeader *p) |
static void | free_profile (TProfile *p) |
void | free_profiles (void) |
void * | profile_init_iterator (char *appname, char *file) |
void * | profile_iterator_next (void *s, char **key, char **value) |
void | profile_clean_section (char *appname, char *file) |
Variables | |
TProfile * | Current = 0 |
TProfile * | Base = 0 |
#define INIFILE "win.ini" |
Definition at line 46 of file profile.c.
Referenced by GetProfileInt(), GetProfileString(), and WriteProfileString().
#define overflow (next == &CharBuffer [STRSIZE-1]) |
#define xmalloc | ( | x, | |||
y | ) | malloc (x) |
Definition at line 43 of file profile.c.
Referenced by GetSetProfileChar(), load(), new_key(), and profile_init_iterator().
anonymous enum |
Definition at line 50 of file profile.c.
00050 { 00051 FirstBrace, OnSecHeader, IgnoreToEOL, KeyDef, KeyValue, FirstChar 00052 };
static void dump_keys | ( | FILE * | profile, | |
TKeys * | p | |||
) | [static] |
Definition at line 340 of file profile.c.
References TKeys::KeyName, TKeys::link, and TKeys::Value.
Referenced by dump_sections().
00341 { 00342 if (!p) 00343 return; 00344 dump_keys (profile, p->link); 00345 fprintf (profile, "%s=%s\n", p->KeyName, p->Value); 00346 }
static void dump_profile | ( | TProfile * | p | ) | [static] |
Definition at line 361 of file profile.c.
References dump_sections(), TProfile::FileName, TProfile::link, and TProfile::Section.
Referenced by sync_profiles().
00362 { 00363 FILE *profile; 00364 00365 if (!p) 00366 return; 00367 dump_profile (p->link); 00368 if ((profile = fopen (p->FileName, "w")) != NULL) { 00369 dump_sections (profile, p->Section); 00370 fclose (profile); 00371 } 00372 }
static void dump_sections | ( | FILE * | profile, | |
TSecHeader * | p | |||
) | [static] |
Definition at line 349 of file profile.c.
References TSecHeader::AppName, dump_keys(), TSecHeader::Keys, and TSecHeader::link.
Referenced by dump_profile().
00350 { 00351 if (!p) 00352 return; 00353 dump_sections (profile, p->link); 00354 if (p->AppName[0]) { 00355 fprintf (profile, "\n[%s]\n", p->AppName); 00356 dump_keys (profile, p->Keys); 00357 } 00358 }
static void free_keys | ( | TKeys * | p | ) | [static] |
Definition at line 385 of file profile.c.
References TKeys::KeyName, TKeys::link, and TKeys::Value.
Referenced by free_sections().
00386 { 00387 if (!p) 00388 return; 00389 free_keys (p->link); 00390 free (p->KeyName); 00391 free (p->Value); 00392 free (p); 00393 }
static void free_profile | ( | TProfile * | p | ) | [static] |
Definition at line 409 of file profile.c.
References TProfile::FileName, free_sections(), TProfile::link, and TProfile::Section.
Referenced by free_profiles().
00410 { 00411 if (!p) 00412 return; 00413 free_profile (p->link); 00414 free_sections (p->Section); 00415 free (p->FileName); 00416 free (p); 00417 }
void free_profiles | ( | void | ) |
Definition at line 420 of file profile.c.
References free_profile().
00421 { 00422 free_profile (Base); 00423 }
static void free_sections | ( | TSecHeader * | p | ) | [static] |
Definition at line 396 of file profile.c.
References TSecHeader::AppName, free_keys(), TSecHeader::Keys, and TSecHeader::link.
Referenced by free_profile().
00397 { 00398 if (!p) 00399 return; 00400 free_sections (p->link); 00401 free_keys (p->Keys); 00402 free (p->AppName); 00403 p->link = 0; 00404 p->Keys = 0; 00405 free (p); 00406 }
char* get_profile_string | ( | char * | AppName, | |
char * | KeyName, | |||
char * | Default, | |||
char * | FileName | |||
) |
Definition at line 288 of file profile.c.
References GetSetProfileChar().
00290 { 00291 return GetSetProfileChar (0, AppName, KeyName, Default, FileName); 00292 }
int GetPrivateProfileInt | ( | char * | AppName, | |
char * | KeyName, | |||
short | Default, | |||
char * | File | |||
) |
Definition at line 303 of file profile.c.
References GetPrivateProfileString().
Referenced by GetProfileInt(), and main().
00305 { 00306 static char IntBuf[10]; 00307 static char buf[10]; 00308 00309 sprintf (buf, "%d", Default); 00310 00311 /* Check the exact semantic with the SDK */ 00312 GetPrivateProfileString (AppName, KeyName, buf, IntBuf, 5, File); 00313 if (!strcasecmp (IntBuf, "true")) 00314 return 1; 00315 if (!strcasecmp (IntBuf, "yes")) 00316 return 1; 00317 return atoi (IntBuf); 00318 }
short GetPrivateProfileString | ( | char * | AppName, | |
char * | KeyName, | |||
char * | Default, | |||
char * | ReturnedString, | |||
short | Size, | |||
char * | FileName | |||
) |
Definition at line 280 of file profile.c.
References GetSetProfile().
Referenced by GetPrivateProfileInt(), GetProfileString(), mollerClass::Loop(), and main().
00283 { 00284 return (GetSetProfile (0, AppName, KeyName, Default, ReturnedString, Size, FileName)); 00285 }
int GetProfileInt | ( | char * | AppName, | |
char * | KeyName, | |||
int | Default | |||
) |
Definition at line 321 of file profile.c.
References GetPrivateProfileInt(), and INIFILE.
00322 { 00323 return GetPrivateProfileInt (AppName, KeyName, Default, INIFILE); 00324 }
int GetProfileString | ( | char * | AppName, | |
char * | KeyName, | |||
char * | Default, | |||
char * | ReturnedString, | |||
int | Size | |||
) |
Definition at line 295 of file profile.c.
References GetPrivateProfileString(), and INIFILE.
00297 { 00298 return GetPrivateProfileString (AppName, KeyName, Default, 00299 ReturnedString, Size, INIFILE); 00300 }
short GetSetProfile | ( | int | set, | |
char * | AppName, | |||
char * | KeyName, | |||
char * | Default, | |||
char * | ReturnedString, | |||
short | Size, | |||
char * | FileName | |||
) |
Definition at line 266 of file profile.c.
References GetSetProfileChar().
Referenced by GetPrivateProfileString(), and WritePrivateProfileString().
00268 { 00269 char *s; 00270 00271 s = GetSetProfileChar (set, AppName, KeyName, Default, FileName); 00272 if (!set) { 00273 ReturnedString[Size - 1] = 0; 00274 strncpy (ReturnedString, s, Size - 1); 00275 } 00276 return 1; 00277 }
char* GetSetProfileChar | ( | int | set, | |
char * | AppName, | |||
char * | KeyName, | |||
char * | Default, | |||
char * | FileName | |||
) |
Definition at line 214 of file profile.c.
References TSecHeader::AppName, TProfile::FileName, is_loaded(), TKeys::KeyName, TSecHeader::Keys, TKeys::link, TSecHeader::link, TProfile::link, load(), new_key(), TProfile::Section, TKeys::Value, and xmalloc.
Referenced by get_profile_string(), and GetSetProfile().
00216 { 00217 00218 TProfile *New; 00219 TSecHeader *section; 00220 TKeys *key; 00221 00222 if (!is_loaded (FileName, §ion)) { 00223 New = (TProfile *) xmalloc (sizeof (TProfile), "GetSetProfile"); 00224 New->link = Base; 00225 New->FileName = strdup (FileName); 00226 New->Section = load (FileName); 00227 Base = New; 00228 section = New->Section; 00229 Current = New; 00230 } 00231 /* Start search */ 00232 for (; section; section = section->link) { 00233 if (strcasecmp (section->AppName, AppName)) 00234 continue; 00235 for (key = section->Keys; key; key = key->link) { 00236 if (strcasecmp (key->KeyName, KeyName)) 00237 continue; 00238 if (set) { 00239 free (key->Value); 00240 key->Value = strdup (Default); 00241 } 00242 return key->Value; 00243 } 00244 /* If getting the information, then don't write the information 00245 * to the INI file, need to run a couple of tests with windog */ 00246 /* No key found */ 00247 if (set) { 00248 new_key (section, KeyName, Default); 00249 return 0; 00250 } 00251 } 00252 00253 /* Non existent section */ 00254 if (set) { 00255 section = (TSecHeader *) xmalloc (sizeof (TSecHeader), "GSP3"); 00256 section->AppName = strdup (AppName); 00257 section->Keys = 0; 00258 new_key (section, KeyName, Default); 00259 section->link = Current->Section; 00260 Current->Section = section; 00261 } 00262 return Default; 00263 }
static int is_loaded | ( | char * | FileName, | |
TSecHeader ** | section | |||
) | [static] |
Definition at line 76 of file profile.c.
References TProfile::FileName, TProfile::link, and TProfile::Section.
Referenced by GetSetProfileChar(), profile_clean_section(), and profile_init_iterator().
00077 { 00078 TProfile *p = Base; 00079 00080 while (p) { 00081 if (!strcasecmp (FileName, p->FileName)) { 00082 Current = p; 00083 *section = p->Section; 00084 return 1; 00085 } 00086 p = p->link; 00087 } 00088 return 0; 00089 }
static TSecHeader* load | ( | char * | file | ) | [static] |
Definition at line 94 of file profile.c.
References TSecHeader::AppName, FirstBrace, FirstChar, IgnoreToEOL, KeyDef, TKeys::KeyName, TSecHeader::Keys, KeyValue, TKeys::link, TSecHeader::link, OnSecHeader, overflow, STRSIZE, TKeys::Value, and xmalloc.
Referenced by GetSetProfileChar(), and profile_init_iterator().
00095 { 00096 FILE *f; 00097 int state; 00098 TSecHeader *SecHeader = 0; 00099 char CharBuffer[STRSIZE]; 00100 char *next = ""; /* Not needed */ 00101 int c; 00102 00103 if ((f = fopen (file, "r")) == NULL) 00104 return NULL; 00105 00106 state = FirstBrace; 00107 while ((c = getc (f)) != EOF) { 00108 if (c == '\r') /* Ignore Carriage Return */ 00109 continue; 00110 00111 switch (state) { 00112 00113 case OnSecHeader: 00114 if (c == ']' || overflow) { 00115 *next = '\0'; 00116 next = CharBuffer; 00117 SecHeader->AppName = strdup (CharBuffer); 00118 state = IgnoreToEOL; 00119 } else 00120 *next++ = c; 00121 break; 00122 00123 case IgnoreToEOL: 00124 if (c == '\n') { 00125 state = FirstChar; 00126 next = CharBuffer; 00127 } 00128 break; 00129 00130 case FirstBrace: 00131 case FirstChar: 00132 if (c == '#' || c == ';') { 00133 state = IgnoreToEOL; 00134 break; 00135 } 00136 case KeyDef: 00137 if (c == '[') { 00138 TSecHeader *temp; 00139 00140 temp = SecHeader; 00141 SecHeader = (TSecHeader *) xmalloc (sizeof (TSecHeader), 00142 "KeyDef"); 00143 SecHeader->link = temp; 00144 SecHeader->Keys = 0; 00145 state = OnSecHeader; 00146 next = CharBuffer; 00147 break; 00148 } 00149 if (state == FirstBrace) /* On first pass, don't allow dangling keys */ 00150 break; 00151 00152 if (state == FirstChar) /* On first pass, don't allow dangling keys */ 00153 state = KeyDef; 00154 00155 if (c == ' ' || c == '\t') 00156 break; 00157 00158 if (c == '\n' || overflow) { /* Abort Definition */ 00159 #ifdef DEBUG 00160 printf ("Abort %s (%s)\n", next, CharBuffer); 00161 #endif 00162 next = CharBuffer; 00163 break; 00164 } 00165 if (c == '=' || overflow) { 00166 TKeys *temp; 00167 00168 temp = SecHeader->Keys; 00169 *next = '\0'; 00170 SecHeader->Keys = (TKeys *) xmalloc (sizeof (TKeys), "KD2"); 00171 SecHeader->Keys->link = temp; 00172 SecHeader->Keys->KeyName = strdup (CharBuffer); 00173 state = KeyValue; 00174 next = CharBuffer; 00175 } else 00176 *next++ = c; 00177 break; 00178 00179 case KeyValue: 00180 if (overflow || c == '\n') { 00181 *next = '\0'; 00182 SecHeader->Keys->Value = strdup (CharBuffer); 00183 state = c == '\n' ? FirstChar : IgnoreToEOL; 00184 next = CharBuffer; 00185 #ifdef DEBUG 00186 printf ("[%s] (%s)=%s\n", SecHeader->AppName, 00187 SecHeader->Keys->KeyName, SecHeader->Keys->Value); 00188 #endif 00189 } else 00190 *next++ = c; 00191 break; 00192 00193 } /* switch */ 00194 #ifdef DEBUG 00195 printf ("%i Step %c [%s] (%s)\n", state, c, next, CharBuffer); 00196 #endif 00197 } /* while ((c = getc (f)) != EOF) */ 00198 return SecHeader; 00199 }
static void new_key | ( | TSecHeader * | section, | |
char * | KeyName, | |||
char * | Value | |||
) | [static] |
Definition at line 202 of file profile.c.
References TKeys::KeyName, TSecHeader::Keys, TKeys::link, TKeys::Value, and xmalloc.
Referenced by GetSetProfileChar().
00203 { 00204 TKeys *key; 00205 00206 key = (TKeys *) xmalloc (sizeof (TKeys), "new_key"); 00207 key->KeyName = strdup (KeyName); 00208 key->Value = strdup (Value); 00209 key->link = section->Keys; 00210 section->Keys = key; 00211 }
void profile_clean_section | ( | char * | appname, | |
char * | file | |||
) |
Definition at line 462 of file profile.c.
References TSecHeader::AppName, is_loaded(), and TSecHeader::link.
00463 { 00464 TSecHeader *section; 00465 00466 /* We assume the user has called one of the other initialization funcs */ 00467 if (!is_loaded (file, §ion)) { 00468 fprintf (stderr, "Warning: profile_clean_section called before init\n"); 00469 return; 00470 } 00471 /* We only disable the section, so it will still be freed, but it */ 00472 /* won't be find by further walks of the structure */ 00473 00474 for (; section; section = section->link) { 00475 if (strcasecmp (section->AppName, appname)) 00476 continue; 00477 section->AppName[0] = 0; 00478 } 00479 }
void* profile_init_iterator | ( | char * | appname, | |
char * | file | |||
) |
Definition at line 426 of file profile.c.
References TSecHeader::AppName, TProfile::FileName, is_loaded(), TSecHeader::Keys, TSecHeader::link, TProfile::link, load(), TProfile::Section, and xmalloc.
00427 { 00428 TProfile *New; 00429 TSecHeader *section; 00430 00431 if (!is_loaded (file, §ion)) { 00432 New = (TProfile *) xmalloc (sizeof (TProfile), "GetSetProfile"); 00433 New->link = Base; 00434 New->FileName = strdup (file); 00435 New->Section = load (file); 00436 Base = New; 00437 section = New->Section; 00438 Current = New; 00439 } 00440 for (; section; section = section->link) { 00441 if (strcasecmp (section->AppName, appname)) 00442 continue; 00443 return section->Keys; 00444 } 00445 return 0; 00446 }
void* profile_iterator_next | ( | void * | s, | |
char ** | key, | |||
char ** | value | |||
) |
Definition at line 449 of file profile.c.
References TKeys::KeyName, TKeys::link, and TKeys::Value.
void sync_profiles | ( | void | ) |
Definition at line 379 of file profile.c.
References dump_profile().
00380 { 00381 dump_profile (Base); 00382 }
int WritePrivateProfileString | ( | char * | AppName, | |
char * | KeyName, | |||
char * | String, | |||
char * | FileName | |||
) |
Definition at line 327 of file profile.c.
References GetSetProfile().
Referenced by WriteProfileString().
00329 { 00330 return GetSetProfile (1, AppName, KeyName, String, "", 0, FileName); 00331 }
int WriteProfileString | ( | char * | AppName, | |
char * | KeyName, | |||
char * | String | |||
) |
Definition at line 334 of file profile.c.
References INIFILE, and WritePrivateProfileString().
00335 { 00336 return (WritePrivateProfileString (AppName, KeyName, String, INIFILE)); 00337 }