| 1 | #include <stdio.h> | 
 
 
 
 
 | 2 | #include <stdlib.h> | 
 
 
 
 
 | 3 | #include <stdbool.h> | 
 
 
 
 
 | 4 | #include <string.h> | 
 
 
 
 
 | 5 | #include <ctype.h> | 
 
 
 
 
 | 6 |  | 
 
 
 
 
 | 7 | #include "inifile.h" | 
 
 
 
 
 | 8 | /* | 
 
 
 
 
 | 9 | char* inifile_cleanstr(char* str) | 
 
 
 
 
 | 10 | { | 
 
 
 
 
 | 11 | while (isspace(*str)) | 
 
 
 
 
 | 12 | str++; | 
 
 
 
 
 | 13 |  | 
 
 
 
 
 | 14 | int i; | 
 
 
 
 
 | 15 | for (i = 0; str[i]; i ++) | 
 
 
 
 
 | 16 | { | 
 
 
 
 
 | 17 | if | 
 
 
 
 
 | 18 | } | 
 
 
 
 
 | 19 |  | 
 
 
 
 
 | 20 | return str; | 
 
 
 
 
 | 21 | } | 
 
 
 
 
 | 22 | */ | 
 
 
 
 
 | 23 | bool inifile_read(const char* filename, inifile_callback callback) | 
 
 
 
 
 | 24 | { | 
 
 
 
 
 | 25 | FILE* fp = fopen(filename, "r"); | 
 
 
 
 
 | 26 | char* inisection = ""; | 
 
 
 
 
 | 27 | char readbuf[4096] = ""; | 
 
 
 
 
 | 28 | char* readptr; | 
 
 
 
 
 | 29 | bool success = true; | 
 
 
 
 
 | 30 | bool newsection = false; | 
 
 
 
 
 | 31 |  | 
 
 
 
 
 | 32 | if (!fp) | 
 
 
 
 
 | 33 | return inifile_cantread; | 
 
 
 
 
 | 34 |  | 
 
 
 
 
 | 35 | while ((readptr = fgets(readbuf, sizeof(readbuf), fp))) // Loop through each line. | 
 
 
 
 
 | 36 | { | 
 
 
 
 
 | 37 | while (isspace(readptr[0])) // Skip whitespace. | 
 
 
 
 
 | 38 | readptr++; | 
 
 
 
 
 | 39 |  | 
 
 
 
 
 | 40 | if (readptr[0] == '\0' || readptr[0] == '#' || readptr[0] == '!') // Skip empty lines and comments. | 
 
 
 
 
 | 41 | continue; | 
 
 
 
 
 | 42 | else if (readptr[0] == '[' && readptr[1] != ']') // It's a section header. | 
 
 
 
 
 | 43 | { | 
 
 
 
 
 | 44 | int i; | 
 
 
 
 
 | 45 | for (i = 2; readptr[i]; i ++) // Look for the ] | 
 
 
 
 
 | 46 | if (readptr[i] == ']') | 
 
 
 
 
 | 47 | break; | 
 
 
 
 
 | 48 |  | 
 
 
 
 
 | 49 | if (readptr[i]) // Replace with a null or crash with error. | 
 
 
 
 
 | 50 | readptr[i] = '\0'; | 
 
 
 
 
 | 51 | else | 
 
 
 
 
 | 52 | { | 
 
 
 
 
 | 53 | success = false; | 
 
 
 
 
 | 54 | break; | 
 
 
 
 
 | 55 | } | 
 
 
 
 
 | 56 |  | 
 
 
 
 
 | 57 | if (inisection[0]) | 
 
 
 
 
 | 58 | free(inisection); | 
 
 
 
 
 | 59 | inisection = strdup(readptr + 1); // Skip the first [ | 
 
 
 
 
 | 60 | newsection = true; | 
 
 
 
 
 | 61 | } | 
 
 
 
 
 | 62 | else // It's a value. | 
 
 
 
 
 | 63 | { | 
 
 
 
 
 | 64 | int i; | 
 
 
 
 
 | 65 | int equals = 0; | 
 
 
 
 
 | 66 | for (i = 0; readptr[i]; i ++) // Find the = | 
 
 
 
 
 | 67 | if (readptr[i] == '=') | 
 
 
 
 
 | 68 | equals = i; | 
 
 
 
 
 | 69 |  | 
 
 
 
 
 | 70 | if (readptr[i - 1] == '\n') | 
 
 
 
 
 | 71 | readptr[i - 1] = '\0'; // Remove the trailing newline. | 
 
 
 
 
 | 72 |  | 
 
 
 
 
 | 73 | if (equals) | 
 
 
 
 
 | 74 | { | 
 
 
 
 
 | 75 | readptr[equals] = '\0'; | 
 
 
 
 
 | 76 | if (!callback(inisection, newsection, readptr, readptr + equals + 1)) // If the callback is false, exit. | 
 
 
 
 
 | 77 | break; | 
 
 
 
 
 | 78 | newsection = false; | 
 
 
 
 
 | 79 | } | 
 
 
 
 
 | 80 | else // If there's no equals, crash with error. | 
 
 
 
 
 | 81 | { | 
 
 
 
 
 | 82 | success = false; | 
 
 
 
 
 | 83 | break; | 
 
 
 
 
 | 84 | } | 
 
 
 
 
 | 85 | } | 
 
 
 
 
 | 86 | } | 
 
 
 
 
 | 87 |  | 
 
 
 
 
 | 88 | if (inisection[0]) | 
 
 
 
 
 | 89 | free(inisection); | 
 
 
 
 
 | 90 |  | 
 
 
 
 
 | 91 | fclose(fp); | 
 
 
 
 
 | 92 | return success; | 
 
 
 
 
 | 93 | } |