source: Daodan/src/inifile_reader.c@ 452

Last change on this file since 452 was 451, checked in by rossy, 15 years ago

bsl thingy

File size: 3.2 KB
RevLine 
[346]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"
[451]8
[349]9char* inifile_cleanstr(char* str)
10{
[451]11 int i;
12 for (i = strlen(str) - 1; i >= 0; i --)
13 if (!isspace(str[i]))
14 {
15 str[i + 1] = '\0';
16 break;
17 }
18
[349]19 while (isspace(*str))
20 str++;
21
[451]22 return str;
23}
24
25int64_t inifile_parseint(const char* str, bool issigned)
26{
27 int64_t ret = 0;
28 bool neg = false;
29 if (str[0] == '0' && str[1] == 'x')
[349]30 {
[451]31 int i;
32 if (str[2] == '\0')
33 return 0x100000000LL;
34
35 for (i = 0, str += 2; *str; i++, str++)
36 {
37 if (i == 8)
38 return 0x100000000LL;
39
40 ret <<= 4;
41 if (*str >= '0' && *str <= '9')
42 ret |= *str - '0';
43 else if (*str >= 'a' && *str <= 'f')
44 ret |= *str - 'a' + 10;
45 else if (*str >= 'A' && *str <= 'F')
46 ret |= *str - 'A' + 10;
47 else
48 return 0x100000000LL;
49 }
50 return ret;
[349]51 }
[451]52 else if ((*str >= '0' && *str <= '9') || (neg = (*str == '-')))
53 {
54 int i;
55 if (neg)
56 str++;
57 for (i = 0; *str; i++, str++)
58 {
59 if (i == 10)
60 return 0x100000000LL;
61 else if (i == 9 && !issigned && (ret > 429496729LL || (ret == 429496729LL && *str > '5')))
62 return 0x100000000LL;
63 else if (i == 9 && issigned && (ret > 214748364LL || (ret == 214748364LL && *str > (neg ? '8' : '7'))))
64 return 0x100000000LL;
65
66 ret *= 10;
67 if (*str >= '0' && *str <= '9')
68 ret += *str - '0';
69 else
70 return 0x100000000LL;
71 }
72 if (neg)
73 ret *= -1;
74 return ret;
75 }
76 else
77 return 0x100000000LL;
[349]78}
[451]79
[352]80bool inifile_read(const char* filename, inifile_callback callback)
[346]81{
82 FILE* fp = fopen(filename, "r");
83 char* inisection = "";
84 char readbuf[4096] = "";
85 char* readptr;
86 bool success = true;
87 bool newsection = false;
88
89 if (!fp)
90 return inifile_cantread;
91
92 while ((readptr = fgets(readbuf, sizeof(readbuf), fp))) // Loop through each line.
93 {
94 while (isspace(readptr[0])) // Skip whitespace.
95 readptr++;
96
97 if (readptr[0] == '\0' || readptr[0] == '#' || readptr[0] == '!') // Skip empty lines and comments.
98 continue;
99 else if (readptr[0] == '[' && readptr[1] != ']') // It's a section header.
100 {
101 int i;
102 for (i = 2; readptr[i]; i ++) // Look for the ]
103 if (readptr[i] == ']')
104 break;
105
106 if (readptr[i]) // Replace with a null or crash with error.
107 readptr[i] = '\0';
108 else
109 {
110 success = false;
111 break;
112 }
113
114 if (inisection[0])
115 free(inisection);
[349]116 inisection = strdup(readptr + 1); // Skip the first [
[346]117 newsection = true;
118 }
119 else // It's a value.
120 {
121 int i;
122 int equals = 0;
123 for (i = 0; readptr[i]; i ++) // Find the =
124 if (readptr[i] == '=')
125 equals = i;
126
127 if (readptr[i - 1] == '\n')
128 readptr[i - 1] = '\0'; // Remove the trailing newline.
129
[349]130 if (equals)
[346]131 {
132 readptr[equals] = '\0';
133 if (!callback(inisection, newsection, readptr, readptr + equals + 1)) // If the callback is false, exit.
134 break;
135 newsection = false;
136 }
[349]137 else // If there's no equals, crash with error.
[346]138 {
139 success = false;
140 break;
141 }
142 }
143 }
144
145 if (inisection[0])
146 free(inisection);
147
148 fclose(fp);
149 return success;
150}
Note: See TracBrowser for help on using the repository browser.