Last change
on this file since 346 was
346,
checked in by rossy, 13 years ago
|
parses daodan.ini to add language patches
|
File size:
1.7 KB
|
Line | |
---|
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 | bool inifile_read(char* filename, inifile_callback callback) |
---|
10 | { |
---|
11 | FILE* fp = fopen(filename, "r"); |
---|
12 | char* inisection = ""; |
---|
13 | char readbuf[4096] = ""; |
---|
14 | char* readptr; |
---|
15 | bool success = true; |
---|
16 | bool newsection = false; |
---|
17 | |
---|
18 | if (!fp) |
---|
19 | return inifile_cantread; |
---|
20 | |
---|
21 | while ((readptr = fgets(readbuf, sizeof(readbuf), fp))) // Loop through each line. |
---|
22 | { |
---|
23 | while (isspace(readptr[0])) // Skip whitespace. |
---|
24 | readptr++; |
---|
25 | |
---|
26 | if (readptr[0] == '\0' || readptr[0] == '#' || readptr[0] == '!') // Skip empty lines and comments. |
---|
27 | continue; |
---|
28 | else if (readptr[0] == '[' && readptr[1] != ']') // It's a section header. |
---|
29 | { |
---|
30 | int i; |
---|
31 | for (i = 2; readptr[i]; i ++) // Look for the ] |
---|
32 | if (readptr[i] == ']') |
---|
33 | break; |
---|
34 | |
---|
35 | if (readptr[i]) // Replace with a null or crash with error. |
---|
36 | readptr[i] = '\0'; |
---|
37 | else |
---|
38 | { |
---|
39 | success = false; |
---|
40 | break; |
---|
41 | } |
---|
42 | |
---|
43 | if (inisection[0]) |
---|
44 | free(inisection); |
---|
45 | inisection = strdup(readptr + 1); |
---|
46 | newsection = true; |
---|
47 | } |
---|
48 | else // It's a value. |
---|
49 | { |
---|
50 | int i; |
---|
51 | int equals = 0; |
---|
52 | for (i = 0; readptr[i]; i ++) // Find the = |
---|
53 | if (readptr[i] == '=') |
---|
54 | equals = i; |
---|
55 | |
---|
56 | if (readptr[i - 1] == '\n') |
---|
57 | readptr[i - 1] = '\0'; // Remove the trailing newline. |
---|
58 | |
---|
59 | if (equals) // If there's no equals, crash with error. |
---|
60 | { |
---|
61 | readptr[equals] = '\0'; |
---|
62 | if (!callback(inisection, newsection, readptr, readptr + equals + 1)) // If the callback is false, exit. |
---|
63 | break; |
---|
64 | newsection = false; |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | success = false; |
---|
69 | break; |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | if (inisection[0]) |
---|
75 | free(inisection); |
---|
76 | |
---|
77 | fclose(fp); |
---|
78 | return success; |
---|
79 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.