| 1 | 
 #include <stdio.h> | 
 
 
 
 
 
 | 2 | 
 #include <stdlib.h> | 
 
 
 
 
 
 | 3 | 
 #include "stdint.h" | 
 
 
 
 
 
 | 4 | 
 #include <string.h> | 
 
 
 
 
 
 | 5 | 
 #include <ctype.h> | 
 
 
 
 
 
 | 6 | 
  | 
 
 
 
 
 
 | 7 | 
 #include "Inifile_Reader.h" | 
 
 
 
 
 
 | 8 | 
  | 
 
 
 
 
 
 | 9 | 
 static char* strtrim(char* string) | 
 
 
 
 
 
 | 10 | 
 { | 
 
 
 
 
 
 | 11 | 
         while (isspace(*string)) | 
 
 
 
 
 
 | 12 | 
                 string++; | 
 
 
 
 
 
 | 13 | 
         for (int i = strlen(string) - 1; i >= 0; i--) | 
 
 
 
 
 
 | 14 | 
         { | 
 
 
 
 
 
 | 15 | 
                 if (isspace(string[i])) | 
 
 
 
 
 
 | 16 | 
                 { | 
 
 
 
 
 
 | 17 | 
                         string[i] = 0; | 
 
 
 
 
 
 | 18 | 
                 } | 
 
 
 
 
 
 | 19 | 
                 else | 
 
 
 
 
 
 | 20 | 
                 { | 
 
 
 
 
 
 | 21 | 
                         break; | 
 
 
 
 
 
 | 22 | 
                 } | 
 
 
 
 
 
 | 23 | 
         } | 
 
 
 
 
 
 | 24 | 
         return string; | 
 
 
 
 
 
 | 25 | 
 } | 
 
 
 
 
 
 | 26 | 
  | 
 
 
 
 
 
 | 27 | 
 static char* newlines(char* string) | 
 
 
 
 
 
 | 28 | 
 { | 
 
 
 
 
 
 | 29 | 
         for (char* i = string + strlen(string) - 1; i >= string; i--) | 
 
 
 
 
 
 | 30 | 
         { | 
 
 
 
 
 
 | 31 | 
                 if ((*i == '\\') && (*(i+1) == 'n')) | 
 
 
 
 
 
 | 32 | 
                 { | 
 
 
 
 
 
 | 33 | 
                         *i = '\n'; | 
 
 
 
 
 
 | 34 | 
                         memmove(i+1, i+2, strlen(i+1)); | 
 
 
 
 
 
 | 35 | 
                 } | 
 
 
 
 
 
 | 36 | 
         } | 
 
 
 
 
 
 | 37 | 
         return string; | 
 
 
 
 
 
 | 38 | 
 } | 
 
 
 
 
 
 | 39 | 
  | 
 
 
 
 
 
 | 40 | 
 bool Inifile_Read(const char* filename, inifile_callback callback) | 
 
 
 
 
 
 | 41 | 
 { | 
 
 
 
 
 
 | 42 | 
         FILE* fp = fopen(filename, "r"); | 
 
 
 
 
 
 | 43 | 
  | 
 
 
 
 
 
 | 44 | 
         char inisection[30] = ""; | 
 
 
 
 
 
 | 45 | 
         char option[30] = ""; | 
 
 
 
 
 
 | 46 | 
         char value[200] = ""; | 
 
 
 
 
 
 | 47 | 
  | 
 
 
 
 
 
 | 48 | 
         char readbuf[4096] = ""; | 
 
 
 
 
 
 | 49 | 
         char* readptr; | 
 
 
 
 
 
 | 50 | 
  | 
 
 
 
 
 
 | 51 | 
         bool success = true; | 
 
 
 
 
 
 | 52 | 
          | 
 
 
 
 
 
 | 53 | 
         if (!fp) | 
 
 
 
 
 
 | 54 | 
                 return false; | 
 
 
 
 
 
 | 55 | 
          | 
 
 
 
 
 
 | 56 | 
         while ((readptr = fgets(readbuf, sizeof(readbuf), fp))) // Loop through each line. | 
 
 
 
 
 
 | 57 | 
         { | 
 
 
 
 
 
 | 58 | 
                 while (isspace(readptr[0])) // Skip whitespace. | 
 
 
 
 
 
 | 59 | 
                         readptr++; | 
 
 
 
 
 
 | 60 | 
  | 
 
 
 
 
 
 | 61 | 
                 if (readptr[0] == '\0' || readptr[0] == '#' || readptr[0] == '!') // Skip empty lines and comments. | 
 
 
 
 
 
 | 62 | 
                         continue; | 
 
 
 
 
 
 | 63 | 
  | 
 
 
 
 
 
 | 64 | 
                 if (sscanf(readptr, "[%[^]]]", inisection) == 1) | 
 
 
 
 
 
 | 65 | 
                 { | 
 
 
 
 
 
 | 66 | 
                 } | 
 
 
 
 
 
 | 67 | 
                 else if (sscanf(readptr, "%[^=]=%[^\n]", option, value) == 2) | 
 
 
 
 
 
 | 68 | 
                 { | 
 
 
 
 
 
 | 69 | 
                         callback(inisection, strtrim(option), newlines(strtrim(value))); | 
 
 
 
 
 
 | 70 | 
                 } | 
 
 
 
 
 
 | 71 | 
                 else | 
 
 
 
 
 
 | 72 | 
                 { | 
 
 
 
 
 
 | 73 | 
                         success = false; | 
 
 
 
 
 
 | 74 | 
                 } | 
 
 
 
 
 
 | 75 | 
         } | 
 
 
 
 
 
 | 76 | 
          | 
 
 
 
 
 
 | 77 | 
         fclose(fp); | 
 
 
 
 
 
 | 78 | 
         return success; | 
 
 
 
 
 
 | 79 | 
 } | 
 
 
 
 
 
 | 80 | 
  |