1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christian Meyer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | #include "ini_parser.h" |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <strings.h> |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | IniParser::IniParser (char* filename) |
---|
25 | { |
---|
26 | stream = NULL; |
---|
27 | bInSection = false; |
---|
28 | openFile (filename); |
---|
29 | } |
---|
30 | |
---|
31 | IniParser::~IniParser () |
---|
32 | { |
---|
33 | if( stream != NULL) fclose (stream); |
---|
34 | } |
---|
35 | |
---|
36 | int IniParser::openFile( char* filename) |
---|
37 | { |
---|
38 | if( filename == NULL) return -1; |
---|
39 | if( stream != NULL) fclose (stream); |
---|
40 | if( (stream = fopen (filename, "r")) == NULL) |
---|
41 | { |
---|
42 | printf("IniParser could not open %s\n", filename); |
---|
43 | return -1; |
---|
44 | } |
---|
45 | bInSection = false; |
---|
46 | return 0; |
---|
47 | } |
---|
48 | |
---|
49 | int IniParser::getSection( char* section) |
---|
50 | { |
---|
51 | bInSection = false; |
---|
52 | if( stream == NULL) return -1; |
---|
53 | |
---|
54 | char linebuffer[PARSELINELENGHT]; |
---|
55 | char secbuffer[PARSELINELENGHT]; |
---|
56 | char* ptr; |
---|
57 | |
---|
58 | rewind (stream); |
---|
59 | while( !feof( stream)) |
---|
60 | { |
---|
61 | // get next line |
---|
62 | fgets (linebuffer, PARSELINELENGHT, stream); |
---|
63 | // remove newline char |
---|
64 | if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0; |
---|
65 | // check for section identifyer |
---|
66 | if( sscanf (linebuffer, "[%s", secbuffer) == 1) |
---|
67 | { |
---|
68 | if( (ptr = strchr( secbuffer, ']')) != NULL) |
---|
69 | { |
---|
70 | *ptr = 0; |
---|
71 | if( !strcmp( secbuffer, section)) |
---|
72 | { |
---|
73 | bInSection = true; |
---|
74 | return 0; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | return -1; |
---|
80 | } |
---|
81 | |
---|
82 | int IniParser::nextVar( char* name, char* value) |
---|
83 | { |
---|
84 | if( stream == NULL) |
---|
85 | { |
---|
86 | bInSection = false; |
---|
87 | return -1; |
---|
88 | } |
---|
89 | if( !bInSection) return -1; |
---|
90 | |
---|
91 | char linebuffer[PARSELINELENGHT]; |
---|
92 | char* ptr; |
---|
93 | |
---|
94 | while( !feof( stream)) |
---|
95 | { |
---|
96 | // get next line |
---|
97 | fgets (linebuffer, PARSELINELENGHT, stream); |
---|
98 | // remove newline char |
---|
99 | if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0; |
---|
100 | if( linebuffer[0] == '[') |
---|
101 | { |
---|
102 | bInSection = false; |
---|
103 | return -1; |
---|
104 | } |
---|
105 | if( (ptr = strchr( linebuffer, '=')) != NULL) |
---|
106 | { |
---|
107 | if( ptr == linebuffer) continue; |
---|
108 | strcpy (value, &ptr[1]); |
---|
109 | strncpy (name, linebuffer, strlen (linebuffer) - strlen (value) - 1); |
---|
110 | return 0; |
---|
111 | } |
---|
112 | } |
---|
113 | return -1; |
---|
114 | } |
---|
115 | |
---|
116 | char* IniParser::getVar( char* name, char* section, char* defvalue = "") |
---|
117 | { |
---|
118 | strcpy (internbuf, defvalue); |
---|
119 | if( getSection (section) == -1) return internbuf; |
---|
120 | |
---|
121 | char namebuf[PARSELINELENGHT]; |
---|
122 | char valuebuf[PARSELINELENGHT]; |
---|
123 | |
---|
124 | while( nextVar (namebuf, valuebuf) != -1) |
---|
125 | { |
---|
126 | if( !strcmp (name, namebuf)) |
---|
127 | { |
---|
128 | strcpy (internbuf, valuebuf); |
---|
129 | return internbuf; |
---|
130 | } |
---|
131 | } |
---|
132 | return internbuf; |
---|
133 | } |
---|