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 | using namespace std; |
---|
20 | |
---|
21 | IniParser::IniParser (char* filename) |
---|
22 | { |
---|
23 | stream = NULL; |
---|
24 | bInSection = false; |
---|
25 | open_file (filename); |
---|
26 | } |
---|
27 | |
---|
28 | IniParser::~IniParser () |
---|
29 | { |
---|
30 | if( stream != NULL) fclose (stream); |
---|
31 | } |
---|
32 | |
---|
33 | int IniParser::open_file( char* filename) |
---|
34 | { |
---|
35 | if( filename == NULL) return -1; |
---|
36 | if( stream != NULL) fclose (stream); |
---|
37 | if( (stream = fopen (filename, "r")) == NULL) |
---|
38 | { |
---|
39 | printf("IniParser could not open %s\n", filename); |
---|
40 | return -1; |
---|
41 | } |
---|
42 | bInSection = false; |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | int IniParser::get_section( char* section) |
---|
47 | { |
---|
48 | bInSection = false; |
---|
49 | if( stream == NULL) return -1; |
---|
50 | |
---|
51 | char linebuffer[PARSELINELENGHT]; |
---|
52 | char secbuffer[PARSELINELENGHT]; |
---|
53 | char* ptr; |
---|
54 | |
---|
55 | rewind (stream); |
---|
56 | while( !feof( stream)) |
---|
57 | { |
---|
58 | // get next line |
---|
59 | fgets (linebuffer, PARSELINELENGHT, stream); |
---|
60 | // remove newline char |
---|
61 | if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0; |
---|
62 | // check for section identifyer |
---|
63 | if( sscanf (linebuffer, "[%s", secbuffer) == 1) |
---|
64 | { |
---|
65 | if( (ptr = strchr( secbuffer, ']')) != NULL) |
---|
66 | { |
---|
67 | *ptr = 0; |
---|
68 | if( !strcmp( secbuffer, section)) |
---|
69 | { |
---|
70 | bInSection = true; |
---|
71 | return 0; |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | return -1; |
---|
77 | } |
---|
78 | |
---|
79 | int IniParser::next_var( char* name, char* value) |
---|
80 | { |
---|
81 | if( stream == NULL) |
---|
82 | { |
---|
83 | bInSection = false; |
---|
84 | return -1; |
---|
85 | } |
---|
86 | if( !bInSection) return -1; |
---|
87 | |
---|
88 | char linebuffer[PARSELINELENGHT]; |
---|
89 | char* ptr; |
---|
90 | |
---|
91 | while( !feof( stream)) |
---|
92 | { |
---|
93 | // get next line |
---|
94 | fgets (linebuffer, PARSELINELENGHT, stream); |
---|
95 | // remove newline char |
---|
96 | if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0; |
---|
97 | if( linebuffer[0] == '[') |
---|
98 | { |
---|
99 | bInSection = false; |
---|
100 | return -1; |
---|
101 | } |
---|
102 | if( (ptr = strchr( linebuffer, '=')) != NULL) |
---|
103 | { |
---|
104 | if( ptr == linebuffer) continue; |
---|
105 | strcpy (value, &ptr[1]); |
---|
106 | strncpy (name, linebuffer, strlen (linebuffer) - strlen (value) - 1); |
---|
107 | return 0; |
---|
108 | } |
---|
109 | } |
---|
110 | return -1; |
---|
111 | } |
---|
112 | |
---|
113 | char* IniParser::get_var( char* name, char* section, char* defvalue = "") |
---|
114 | { |
---|
115 | strcpy (internbuf, defvalue); |
---|
116 | if( get_section (section) == -1) return internbuf; |
---|
117 | |
---|
118 | char namebuf[PARSELINELENGHT]; |
---|
119 | char valuebuf[PARSELINELENGHT]; |
---|
120 | |
---|
121 | while( next_var (namebuf, valuebuf) != -1) |
---|
122 | { |
---|
123 | if( !strcmp (name, namebuf)) |
---|
124 | { |
---|
125 | strcpy (internbuf, valuebuf); |
---|
126 | return internbuf; |
---|
127 | } |
---|
128 | } |
---|
129 | return internbuf; |
---|
130 | } |
---|