1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <unistd.h> 5 6#include "iniparser.h" 7 8void create_example_ini_file(void); 9int parse_ini_file(char * ini_name); 10 11int main(int argc, char * argv[]) 12{ 13 int status ; 14 15 if (argc<2) { 16 create_example_ini_file(); 17 status = parse_ini_file("example.ini"); 18 } else { 19 status = parse_ini_file(argv[1]); 20 } 21 return status ; 22} 23 24void create_example_ini_file(void) 25{ 26 FILE * ini ; 27 28 ini = fopen("example.ini", "w"); 29 fprintf(ini, "\n\ 30#\n\ 31# This is an example of ini file\n\ 32#\n\ 33\n\ 34[Pizza]\n\ 35\n\ 36Ham = yes ;\n\ 37Mushrooms = TRUE ;\n\ 38Capres = 0 ;\n\ 39Cheese = NO ;\n\ 40\n\ 41\n\ 42[Wine]\n\ 43\n\ 44Grape = Cabernet Sauvignon ;\n\ 45Year = 1989 ;\n\ 46Country = Spain ;\n\ 47Alcohol = 12.5 ;\n\ 48\n\ 49#\n\ 50# end of file\n\ 51#\n"); 52 53 fclose(ini); 54} 55 56 57int parse_ini_file(char * ini_name) 58{ 59 dictionary * ini ; 60 61 /* Some temporary variables to hold query results */ 62 int b ; 63 int i ; 64 double d ; 65 char * s ; 66 67 ini = iniparser_load(ini_name); 68 if (ini==NULL) { 69 fprintf(stderr, "cannot parse file [%s]", ini_name); 70 return -1 ; 71 } 72 iniparser_dump(ini, stderr); 73 74 /* Get pizza attributes */ 75 printf("Pizza:\n"); 76 77 b = iniparser_getboolean(ini, "pizza:ham", -1); 78 printf("Ham: [%d]\n", b); 79 b = iniparser_getboolean(ini, "pizza:mushrooms", -1); 80 printf("Mushrooms: [%d]\n", b); 81 b = iniparser_getboolean(ini, "pizza:capres", -1); 82 printf("Capres: [%d]\n", b); 83 b = iniparser_getboolean(ini, "pizza:cheese", -1); 84 printf("Cheese: [%d]\n", b); 85 86 /* Get wine attributes */ 87 printf("Wine:\n"); 88 s = iniparser_getstr(ini, "wine:grape"); 89 if (s) { 90 printf("grape: [%s]\n", s); 91 } else { 92 printf("grape: not found\n"); 93 } 94 i = iniparser_getint(ini, "wine:year", -1); 95 if (i>0) { 96 printf("year: [%d]\n", i); 97 } else { 98 printf("year: not found\n"); 99 } 100 s = iniparser_getstr(ini, "wine:country"); 101 if (s) { 102 printf("country: [%s]\n", s); 103 } else { 104 printf("country: not found\n"); 105 } 106 d = iniparser_getdouble(ini, "wine:alcohol", -1.0); 107 if (d>0.0) { 108 printf("alcohol: [%g]\n", d); 109 } else { 110 printf("alcohol: not found\n"); 111 } 112 113 iniparser_freedict(ini); 114 return 0 ; 115} 116 117 118