2 * udibuild - UDI Compilation Utility
3 * - By John Hodge (thePowersGang)
13 #include "include/inifile.h"
15 typedef struct sInifile_Section tIniFile_Section;
16 typedef struct sInifile_Value tIniFile_Value;
25 struct sInifile_Section
27 tIniFile_Section *Next;
29 tIniFile_Value *FirstValue;
34 tIniFile_Section RootSection;
38 static void rtrim(char *str)
43 while( pos != str && isspace(pos[-1]) )
47 tIniFile *IniFile_Load(const char *Path)
49 FILE *fp = fopen(Path, "r");
53 tIniFile *ret = malloc( sizeof(tIniFile) );
56 ret->RootSection.Name = "";
57 ret->RootSection.FirstValue = NULL;
59 tIniFile_Section *curSect = &ret->RootSection;
61 while( fgets(buf, sizeof(buf)-1, fp) )
63 if( strchr(buf, '#') )
64 *strchr(buf, '#') = '\0';
68 if( sscanf(buf, "[%[^]]]", name) == 1 ) {
69 //printf("section %s\n", name);
71 tIniFile_Section *new_sect = malloc(sizeof(tIniFile_Section)+strlen(name)+1);
72 new_sect->Next = NULL;
73 new_sect->Name = (const char*)(new_sect+1);
74 new_sect->FirstValue = NULL;
75 strcpy( (char*)new_sect->Name, name );
76 curSect->Next = new_sect;
79 else if( sscanf(buf, "%[^=]=%n", name, &ofs) >= 1 ) {
80 //printf("key %s equals %s\n", name, value);
81 const char *value = buf + ofs;
82 tIniFile_Value *val = malloc(sizeof(tIniFile_Value)+strlen(name)+1+strlen(value)+1);
83 val->Next = curSect->FirstValue;
84 curSect->FirstValue = val;
86 val->Key = (char*)(val+1);
87 strcpy((char*)val->Key, name);
88 val->Value = val->Key + strlen(val->Key) + 1;
89 strcpy((char*)val->Value, value);
92 //printf("ignore %s\n", buf);
102 const char *IniFile_Get(tIniFile *File, const char *Sect, const char *Key, const char *Default)
104 tIniFile_Section *sect;
105 for( sect = &File->RootSection; sect; sect = sect->Next )
107 if( strcmp(sect->Name, Sect) == 0 )
114 for( val = sect->FirstValue; val; val = val->Next )
116 if( strcmp(val->Key, Key) == 0 )
125 void IniFile_Free(tIniFile *File)