2 * udibuild - UDI Compilation Utility
3 * - By John Hodge (thePowersGang)
4 * Part of the Acess2 OS Project
14 #include "include/inifile.h"
16 typedef struct sInifile_Section tIniFile_Section;
17 typedef struct sInifile_Value tIniFile_Value;
26 struct sInifile_Section
28 tIniFile_Section *Next;
30 tIniFile_Value *FirstValue;
35 tIniFile_Section RootSection;
39 static void rtrim(char *str)
44 while( pos != str && isspace(pos[-1]) )
48 tIniFile *IniFile_Load(const char *Path)
50 FILE *fp = fopen(Path, "r");
54 tIniFile *ret = malloc( sizeof(tIniFile) );
57 ret->RootSection.Name = "";
58 ret->RootSection.FirstValue = NULL;
60 tIniFile_Section *curSect = &ret->RootSection;
62 while( fgets(buf, sizeof(buf)-1, fp) )
64 if( strchr(buf, '#') )
65 *strchr(buf, '#') = '\0';
69 if( sscanf(buf, "[%[^]]]", name) == 1 ) {
70 //printf("section %s\n", name);
72 tIniFile_Section *new_sect = malloc(sizeof(tIniFile_Section)+strlen(name)+1);
73 new_sect->Next = NULL;
74 new_sect->Name = (const char*)(new_sect+1);
75 new_sect->FirstValue = NULL;
76 strcpy( (char*)new_sect->Name, name );
77 curSect->Next = new_sect;
80 else if( sscanf(buf, "%[^=]=%n", name, &ofs) >= 1 ) {
82 while( isspace(buf[ofs]) ) ofs ++;
83 //printf("key %s equals %s\n", name, value);
84 const char *value = buf + ofs;
85 tIniFile_Value *val = malloc(sizeof(tIniFile_Value)+strlen(name)+1+strlen(value)+1);
86 val->Next = curSect->FirstValue;
87 curSect->FirstValue = val;
89 val->Key = (char*)(val+1);
90 strcpy((char*)val->Key, name);
91 val->Value = val->Key + strlen(val->Key) + 1;
92 strcpy((char*)val->Value, value);
95 //printf("ignore %s\n", buf);
105 const char *IniFile_Get(tIniFile *File, const char *Sect, const char *Key, const char *Default)
110 tIniFile_Section *sect;
111 for( sect = &File->RootSection; sect; sect = sect->Next )
113 if( strcmp(sect->Name, Sect) == 0 )
120 for( val = sect->FirstValue; val; val = val->Next )
122 if( strcmp(val->Key, Key) == 0 )
131 void IniFile_Free(tIniFile *File)