ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/Daodan/src/Inifile_Reader.c
(Generate patch)

Comparing Daodan/src/Inifile_Reader.c (file contents):
Revision 992 by alloc, Thu Mar 7 17:26:13 2013 UTC vs.
Revision 993 by alloc, Sun Apr 6 17:06:02 2014 UTC

# Line 6 | Line 6
6  
7   #include "Inifile_Reader.h"
8  
9 < char* inifile_cleanstr(char* str)
9 > static char* strtrim(char* string)
10   {
11 <        int i;
12 <        for (i = strlen(str) - 1; i >= 0; i --)
13 <                if (isspace(str[i]))
14 <                        str[i] = '\0';
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 <        while (isspace(*str))
24 <                str++;
20 <        
21 <        return str;
22 >                }
23 >        }
24 >        return string;
25   }
26  
27 < int64_t inifile_parseint(const char* str, bool issigned)
27 > static char* newlines(char* string)
28   {
29 <        int64_t ret = 0;
27 <        bool neg = false;
28 <        if (str[0] == '0' && str[1] == 'x')
29 >        for (char* i = string + strlen(string) - 1; i >= string; i--)
30          {
31 <                int i;
32 <                if (str[2] == '\0')
33 <                        return 0x100000000LL;
34 <                
34 <                for (i = 0, str += 2; *str; i++, str++)
35 <                {
36 <                        if (i == 8)
37 <                                return 0x100000000LL;
38 <                        
39 <                        ret <<= 4;
40 <                        if (*str >= '0' && *str <= '9')
41 <                                ret |= *str - '0';
42 <                        else if (*str >= 'a' && *str <= 'f')
43 <                                ret |= *str - 'a' + 10;
44 <                        else if (*str >= 'A' && *str <= 'F')
45 <                                ret |= *str - 'A' + 10;
46 <                        else
47 <                                return 0x100000000LL;
31 >                if ((*i == '\\') && (*(i+1) == 'n'))
32 >                {
33 >                        *i = '\n';
34 >                        memmove(i+1, i+2, strlen(i+1));
35                  }
49                return ret;
36          }
37 <        else if ((*str >= '0' && *str <= '9') || (neg = (*str == '-')))
52 <        {
53 <                int i;
54 <                if (neg)
55 <                        str++;
56 <                for (i = 0; *str; i++, str++)
57 <                {
58 <                        if (i == 10)
59 <                                return 0x100000000LL;
60 <                        else if (i == 9 && !issigned && (ret > 429496729LL || (ret == 429496729LL && *str > '5')))
61 <                                return 0x100000000LL;
62 <                        else if (i == 9 && issigned && (ret > 214748364LL || (ret == 214748364LL && *str > (neg ? '8' : '7'))))
63 <                                return 0x100000000LL;
64 <                        
65 <                        ret *= 10;
66 <                        if (*str >= '0' && *str <= '9')
67 <                                ret += *str - '0';
68 <                        else
69 <                                return 0x100000000LL;
70 <                }
71 <                if (neg)
72 <                        ret *= -1;
73 <                return ret;
74 <        }
75 <        else
76 <                return 0x100000000LL;
37 >        return string;
38   }
39  
40 < bool inifile_read(const char* filename, inifile_callback callback)
40 > bool Inifile_Read(const char* filename, inifile_callback callback)
41   {
42          FILE* fp = fopen(filename, "r");
43 <        char* inisection = "";
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;
86        bool newsection = false;
52          
53          if (!fp)
54 <                return inifile_cantread;
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 <                
60 >
61                  if (readptr[0] == '\0' || readptr[0] == '#' || readptr[0] == '!') // Skip empty lines and comments.
62                          continue;
63 <                else if (readptr[0] == '[' && readptr[1] != ']') // It's a section header.
63 >
64 >                if (sscanf(readptr, "[%[^]]]", inisection) == 1)
65 >                {
66 >                }
67 >                else if (sscanf(readptr, "%[^=]=%[^\n]", option, value) == 2)
68                  {
69 <                        int i;
70 <                        for (i = 2; readptr[i]; i ++) // Look for the ]
71 <                                if (readptr[i] == ']')
72 <                                        break;
73 <                        
105 <                        if (readptr[i]) // Replace with a null or crash with error.
106 <                                readptr[i] = '\0';
107 <                        else
108 <                        {
109 <                                success = false;
110 <                                break;
111 <                        }
112 <                        
113 <                        if (inisection[0])
114 <                                free(inisection);
115 <                        inisection = _strdup(readptr + 1); // Skip the first [
116 <                        newsection = true;
117 <                }
118 <                else // It's a value.
119 <                {
120 <                        int i;
121 <                        int equals = 0;
122 <                        for (i = 0; readptr[i]; i ++) // Find the =
123 <                                if (readptr[i] == '=')
124 <                                        equals = i;
125 <                        
126 <                        if (readptr[i - 1] == '\n')
127 <                                readptr[i - 1] = '\0'; // Remove the trailing newline.
128 <                        
129 <                        if (equals)
130 <                        {
131 <                                readptr[equals] = '\0';
132 <                                if (!callback(inisection, newsection, readptr, readptr + equals + 1)) // If the callback is false, exit.
133 <                                        break;
134 <                                newsection = false;
135 <                        }
136 <                        else // If there's no equals, crash with error.
137 <                        {
138 <                                success = false;
139 <                                break;
140 <                        }
69 >                        callback(inisection, strtrim(option), newlines(strtrim(value)));
70 >                }
71 >                else
72 >                {
73 >                        success = false;
74                  }
75          }
76          
144        if (inisection[0])
145                free(inisection);
146        
77          fclose(fp);
78          return success;
79   }
80 +

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)