Advertisement
NovaYoshi

INI reader

Feb 9th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. int ParseINI(FILE *File, void (*Handler)(const char *Group, const char *Item, const char *Value)) {
  2.   if(File == NULL) return 0;
  3.   char Group[512]="", *Item, *Value, Line[512]="", c, *Poke = NULL;
  4.   int State = 0, i;
  5.   while(!feof(File)) {
  6.     for(i=0,c=1;;i++) {
  7.       c = fgetc(File);
  8.       if(c=='\r'||c=='\n') {
  9.         Line[i]=0;
  10.         break;
  11.       }
  12.       Line[i] = c;
  13.     }
  14.     while(c=='\r'||c=='\n')
  15.       c = fgetc(File);
  16.     fseek(File, -1 , SEEK_CUR);
  17.     if(!*Line)
  18.       break;
  19.     else if(*Line == ';'); // comment
  20.     else if(*Line == '[') { // group
  21.       Poke = strchr(Line, ']');
  22.       if(Poke) *Poke = 0;
  23.       strcpy(Group, Line+1);
  24.     } else { // item
  25.       Poke = strchr(Line, '=');
  26.       if(Poke) {
  27.         *Poke = 0;
  28.         Item = Line;
  29.         Value = Poke+1;
  30.         Handler(Group, Item, Value);
  31.       }
  32.     }
  33.   }
  34.   fclose(File);
  35.   return 1;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement