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 346 by rossy, Tue Jun 9 12:00:41 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 < bool inifile_read(char* filename, inifile_callback callback)
9 > static char* strtrim(char* string)
10 > {
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 >        }
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)
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;
16        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
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 <                        }
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          
74        if (inisection[0])
75                free(inisection);
76        
77          fclose(fp);
78          return success;
79   }
80 +

Diff Legend

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