7 List
*ini_section_add(List
*ini
, char *name
)
14 i
= (IniSection
*)malloc(sizeof(IniSection
));
18 ini
= list_add_data(ini
,i
);
22 List
*ini_get_section_node(List
*ini
, char *name
)
30 if(strcmp(name
, i
->name
) == 0) return item
;
36 IniSection
*ini_get_section(List
*ini
, char *name
)
40 item
= ini_get_section_node(ini
,name
);
41 if(!item
) return NULL
;
42 return (IniSection
*)(item
->data
);
45 List
*ini_get_variable_node(List
*ini
, char *section
, char *variable
)
51 sec
= ini_get_section(ini
, section
);
53 item
= sec
->variables
;
56 if(strcmp(variable
,ini_i
->name
) == 0) return item
;
62 IniVariable
*ini_get_variable_ini_variable(List
*ini
, char *section
, char *variable
)
66 item
= ini_get_variable_node(ini
,section
,variable
);
67 if(!item
) return NULL
;
68 return (IniVariable
*)(item
->data
);
71 char *ini_get_variable(List
*ini
, char *section
, char *variable
, char *dflt
)
75 i
= ini_get_variable_ini_variable(ini
,section
,variable
);
80 int ini_get_variable_as_int(List
*ini
, char *section
, char *variable
, int dflt
)
84 i
= ini_get_variable_ini_variable(ini
,section
,variable
);
86 if(i
->value_int
== INI_INT_UNDEFINED
) i
->value_int
= atoi(i
->value
);
90 void ini_variable_add_empty(List
*ini
, char *section
, char *variable
)
96 item
= ini_get_section_node(ini
,section
);
98 ini
= ini_section_add(ini
,section
);
99 item
= ini_get_section_node(ini
,section
);
105 var
= (IniVariable
*)malloc(sizeof(IniVariable
));
107 var
->name
= strdup(variable
);
109 var
->value_int
= INI_INT_UNDEFINED
;
111 sec
->variables
= list_add_data(sec
->variables
,var
);
114 List
*ini_set_variable(List
*ini
, char *section
, char *variable
, char *value
)
119 item
= ini_get_section_node(ini
,section
);
121 ini
= ini_section_add(ini
,section
);
122 item
= ini_get_section_node(ini
,section
);
124 if(!item
) return ini
;
126 item
= ini_get_variable_node(ini
,section
,variable
);
128 ini_variable_add_empty(ini
,section
,variable
);
129 item
= ini_get_variable_node(ini
,section
,variable
);
131 if(!item
) return ini
;
136 var
->value
= strdup(value
);
140 List
*ini_set_variable_as_int(List
*ini
, char *section
, char *variable
, int value
)
145 snprintf(value_str
,sizeof(value_str
),"%i",value
);
146 ini
= ini_set_variable(ini
,section
,variable
,value_str
);
147 i
= ini_get_variable_ini_variable(ini
,section
,variable
);
148 if(i
) i
->value_int
= value
;
152 void ini_print(FILE *f
, List
*ini
) {
153 List
*section_item
,*var_item
;
154 IniSection
*section_data
;
155 IniVariable
*ini_data
;
157 for(section_item
= ini
; section_item
; section_item
= section_item
->next
) {
158 section_data
= section_item
->data
;
159 fprintf(f
,"[%s]\n",section_data
->name
);
160 for(var_item
= section_data
->variables
; var_item
; var_item
= var_item
->next
) {
161 ini_data
= var_item
->data
;
162 fprintf(f
,"%s=%s\n",ini_data
->name
,ini_data
->value
);
167 void ini_save_file(List
*ini
, char *filename
)
171 f
= fopen(filename
,"w");
176 void ini_readline(FILE *f
,char *buffer
,int size
)
180 fgets(buffer
,size
-1,f
);
181 p
= strstr(buffer
,"\n");
183 p
= strstr(buffer
,";");
185 p
= strstr(buffer
,"#");
189 char *ini_buffer_to_section(char *buffer
)
193 b
= strstr(buffer
,"[");
203 void ini_buffer_to_variable(char *buffer
, char **var
, char **val
)
207 p
= strstr(buffer
,"=");
219 List
*ini_load_file(List
*ini
, char *default_section
, char *name
)
222 char buffer
[1024],current_section
[100];
223 char *section
, *var
, *val
;
225 memset(current_section
,0,sizeof(current_section
));
226 if(default_section
) strncpy(current_section
,default_section
,sizeof(current_section
)-1);
230 ini_readline(f
,buffer
,sizeof(buffer
));
231 section
= ini_buffer_to_section(buffer
);
233 memset(current_section
,0,sizeof(current_section
));
234 strncpy(current_section
,section
,sizeof(current_section
)-1);
236 if(current_section
[0]) {
237 ini_buffer_to_variable(buffer
,&var
,&val
);
239 ini
= ini_set_variable(ini
,current_section
,var
,val
);
248 List
*ini_section_node_with_variable_node(List
*ini
, List
*node
)
250 IniSection
*section_data
;
253 for(section
= ini
; section
; section
= section
->next
) {
254 section_data
= section
->data
;
255 if( list_has_node(section_data
->variables
,node
) ) return section
;
260 void ini_delete_variable_ini_variable(IniVariable
*var
)
262 if(var
->name
) free(var
->name
);
263 if(var
->value
) free(var
->value
);
267 void ini_delete_variable_node(List
*ini
, List
*node
)
269 IniVariable
*ini_var
;
271 IniSection
*section_data
;
273 section_node
= ini_section_node_with_variable_node(ini
,node
);
274 if(!section_node
) return;
275 section_data
= section_node
->data
;
277 ini_var
= node
->data
;
278 ini_delete_variable_ini_variable(ini_var
);
279 section_data
->variables
= list_delete_node(section_data
->variables
,node
);
282 void ini_delete_variable(List
*ini
, char *section
, char *variable
)
286 node
= ini_get_variable_node(ini
,section
,variable
);
288 ini_delete_variable_node(ini
,node
);
291 List
*ini_delete_section(List
*ini
, char *section
)
293 List
*section_node
, *var_node
;
294 IniSection
*section_data
;
296 section_node
= ini_get_section_node(ini
,section
);
297 if(!section_node
) return ini
;
298 section_data
= section_node
->data
;
299 for(var_node
= section_data
->variables
; var_node
; var_node
=var_node
->next
) {
300 ini_delete_variable_ini_variable(var_node
->data
);
302 list_free(section_data
->variables
);
303 if(section_data
->name
) free(section_data
->name
);
305 ini
= list_delete_node(ini
,section_node
);
309 List
*ini_delete_section_node(List
*ini
, List
*section
)
311 IniSection
*section_data
= section
->data
;
312 return ini_delete_section(ini
,section_data
->name
);
315 void ini_free(List
*ini
)
318 ini
= ini_delete_section_node(ini
,ini
);