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 352 by rossy, Sat Jun 13 01:44:53 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"
8 < /*
9 < char* inifile_cleanstr(char* str)
7 > #include "Inifile_Reader.h"
8 >
9 > static char* strtrim(char* string)
10   {
11 <        while (isspace(*str))
12 <                str++;
13 <        
14 <        int i;
15 <        for (i = 0; str[i]; i ++)
11 >        while (isspace(*string))
12 >                string++;
13 >        for (int i = strlen(string) - 1; i >= 0; i--)
14          {
15 <                if
15 >                if (isspace(string[i]))
16 >                {
17 >                        string[i] = 0;
18 >                }
19 >                else
20 >                {
21 >                        break;
22 >                }
23          }
24 <        
25 <        return str;
24 >        return string;
25 > }
26 >
27 > static char* newlines(char* string)
28 > {
29 >        for (char* i = string + strlen(string) - 1; i >= string; i--)
30 >        {
31 >                if ((*i == '\\') && (*(i+1) == 'n'))
32 >                {
33 >                        *i = '\n';
34 >                        memmove(i+1, i+2, strlen(i+1));
35 >                }
36 >        }
37 >        return string;
38   }
39 < */
40 < bool inifile_read(const char* filename, inifile_callback callback)
39 >
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;
30        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 <                        int i;
67 <                        for (i = 2; readptr[i]; i ++) // Look for the ]
68 <                                if (readptr[i] == ']')
69 <                                        break;
70 <                        
71 <                        if (readptr[i]) // Replace with a null or crash with error.
72 <                                readptr[i] = '\0';
73 <                        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 <                        }
66 >                }
67 >                else if (sscanf(readptr, "%[^=]=%[^\n]", option, value) == 2)
68 >                {
69 >                        callback(inisection, strtrim(option), newlines(strtrim(value)));
70 >                }
71 >                else
72 >                {
73 >                        success = false;
74                  }
75          }
76          
88        if (inisection[0])
89                free(inisection);
90        
77          fclose(fp);
78          return success;
79   }
80 +

Diff Legend

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