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 451 by rossy, Sun Jul 26 12:04:44 2009 UTC vs.
Revision 993 by alloc, Sun Apr 6 17:06:02 2014 UTC

# Line 1 | Line 1
1   #include <stdio.h>
2   #include <stdlib.h>
3 < #include <stdbool.h>
3 > #include "stdint.h"
4   #include <string.h>
5   #include <ctype.h>
6  
7 < #include "inifile.h"
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]))
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                  {
15                        str[i + 1] = '\0';
21                          break;
22                  }
23 <        
24 <        while (isspace(*str))
20 <                str++;
21 <        
22 <        return str;
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;
28 <        bool neg = false;
29 <        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 <                
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;
31 >                if ((*i == '\\') && (*(i+1) == 'n'))
32 >                {
33 >                        *i = '\n';
34 >                        memmove(i+1, i+2, strlen(i+1));
35                  }
50                return ret;
36          }
37 <        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;
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;
87        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 >                        callback(inisection, strtrim(option), newlines(strtrim(value)));
70 >                }
71 >                else
72                  {
73 <                        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);
116 <                        inisection = strdup(readptr + 1); // Skip the first [
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 <                        
130 <                        if (equals)
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 <                        }
137 <                        else // If there's no equals, crash with error.
138 <                        {
139 <                                success = false;
140 <                                break;
141 <                        }
73 >                        success = false;
74                  }
75          }
76          
145        if (inisection[0])
146                free(inisection);
147        
77          fclose(fp);
78          return success;
79   }
80 +

Diff Legend

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