]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/profile.cpp
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / radiant / profile.cpp
1 /*
2    Copyright (c) 2001, Loki software, inc.
3    All rights reserved.
4
5    Redistribution and use in source and binary forms, with or without modification,
6    are permitted provided that the following conditions are met:
7
8    Redistributions of source code must retain the above copyright notice, this list
9    of conditions and the following disclaimer.
10
11    Redistributions in binary form must reproduce the above copyright notice, this
12    list of conditions and the following disclaimer in the documentation and/or
13    other materials provided with the distribution.
14
15    Neither the name of Loki software nor the names of its contributors may be used
16    to endorse or promote products derived from this software without specific prior
17    written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22    DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23    DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 //
32 // Application settings load/save
33 //
34 // Leonardo Zide (leo@lokigames.com)
35 //
36
37 #include "stdafx.h"
38 #include <glib.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 #include "str.h"
44 #include "file.h"
45
46 // =============================================================================
47 // Static functions
48
49 bool read_var( const char *filename, const char *section, const char *key, char *value ){
50         char line[1024], *ptr;
51         FILE *rc;
52
53         rc = fopen( filename, "rt" );
54
55         if ( rc == NULL ) {
56                 return false;
57         }
58
59         while ( fgets( line, 1024, rc ) != 0 )
60         {
61                 // First we find the section
62                 if ( line[0] != '[' ) {
63                         continue;
64                 }
65
66                 ptr = strchr( line, ']' );
67                 *ptr = '\0';
68
69                 if ( strcmp( &line[1], section ) == 0 ) {
70                         while ( fgets( line, 1024, rc ) != 0 )
71                         {
72                                 ptr = strchr( line, '=' );
73
74                                 if ( ptr == NULL ) {
75                                         // reached the end of the section
76                                         fclose( rc );
77                                         return false;
78                                 }
79                                 *ptr = '\0';
80
81                                 // remove spaces
82                                 while ( line[strlen( line ) - 1] == ' ' )
83                                         line[strlen( line ) - 1] = '\0';
84
85                                 if ( strcmp( line, key ) == 0 ) {
86                                         strcpy( value, ptr + 1 );
87                                         fclose( rc );
88
89                                         if ( value[strlen( value ) - 1] == 10 || value[strlen( value ) - 1] == 13 || value[strlen( value ) - 1] == 32 ) {
90                                                 value[strlen( value ) - 1] = 0;
91                                         }
92
93                                         return true;
94                                 }
95                         }
96                 }
97         }
98
99         fclose( rc );
100         return false;
101 }
102
103 static bool save_var( const char *filename, const char *section, const char *key, const char *value ){
104         char line[1024], *ptr;
105         MemStream old_rc;
106         bool found;
107         FILE *rc;
108
109         rc = fopen( filename, "rb" );
110
111         if ( rc != NULL ) {
112                 guint32 len;
113                 void *buf;
114
115                 fseek( rc, 0, SEEK_END );
116                 len = ftell( rc );
117                 rewind( rc );
118                 buf = qmalloc( len );
119                 fread( buf, len, 1, rc );
120                 old_rc.Write( buf, len );
121                 free( buf );
122                 fclose( rc );
123                 old_rc.Seek( 0, SEEK_SET );
124         }
125
126         // TTimo: changed to binary writing. It doesn't seem to affect linux version, and win32 version was happending a lot of '\n'
127         rc = fopen( filename, "wb" );
128
129         if ( rc == NULL ) {
130                 return false;
131         }
132
133         // First we need to find the section
134         found = false;
135         while ( old_rc.ReadString( line, 1024 ) != NULL )
136         {
137                 fputs( line, rc );
138
139                 if ( line[0] == '[' ) {
140                         ptr = strchr( line, ']' );
141                         *ptr = '\0';
142
143                         if ( strcmp( &line[1], section ) == 0 ) {
144                                 found = true;
145                                 break;
146                         }
147                 }
148         }
149
150         if ( !found ) {
151                 fputs( "\n", rc );
152                 fprintf( rc, "[%s]\n", section );
153         }
154
155         fprintf( rc, "%s=%s\n", key, value );
156
157         while ( old_rc.ReadString( line, 1024 ) != NULL )
158         {
159                 ptr = strchr( line, '=' );
160
161                 if ( ptr != NULL ) {
162                         *ptr = '\0';
163
164                         if ( strcmp( line, key ) == 0 ) {
165                                 break;
166                         }
167
168                         *ptr = '=';
169                         fputs( line, rc );
170                 }
171                 else
172                 {
173                         fputs( line, rc );
174                         break;
175                 }
176         }
177
178         while ( old_rc.ReadString( line, 1024 ) != NULL )
179                 fputs( line, rc );
180
181         fclose( rc );
182         return true;
183 }
184
185 // =============================================================================
186 // Global functions
187
188 bool WINAPI profile_save_int( const char *filename, const char *section, const char *key, int value ){
189         char buf[16];
190         sprintf( buf, "%d", value );
191         return save_var( filename, section, key, buf );
192 }
193
194 bool WINAPI profile_save_float( const char *filename, const char *section, const char *key, float value ){
195         char buf[16];
196         sprintf( buf, "%f", value );
197         return save_var( filename, section, key, buf );
198 }
199
200 bool WINAPI profile_save_string( const char * filename, const char *section, const char *key, const char *value ){
201         return save_var( filename, section, key, value );
202 }
203
204 bool profile_save_buffer( const char * rc_path, const char *name, void *buffer, guint32 size ){
205         bool ret = false;
206         char filename[PATH_MAX];
207         sprintf( filename, "%s/%s.bin", rc_path, name );
208         FILE *f;
209
210         f = fopen( filename, "wb" );
211
212         if ( f != NULL ) {
213                 if ( fwrite( buffer, size, 1, f ) == 1 ) {
214                         ret = true;
215                 }
216
217                 fclose( f );
218         }
219
220         return ret;
221 }
222
223 bool profile_load_buffer( const char * rc_path, const char *name, void *buffer, guint32 *plSize ){
224         char filename[PATH_MAX];
225         sprintf( filename, "%s/%s.bin", rc_path, name );
226         bool ret = false;
227         guint32 len;
228         FILE *f;
229
230         f = fopen( filename, "rb" );
231
232         if ( f != NULL ) {
233                 fseek( f, 0, SEEK_END );
234                 len = ftell( f );
235                 rewind( f );
236
237                 if ( len > *plSize ) {
238                         len = *plSize;
239                 }
240                 else{
241                         *plSize = len;
242                 }
243
244                 if ( fread( buffer, len, 1, f ) == 1 ) {
245                         ret = true;
246                 }
247
248                 fclose( f );
249         }
250
251         return true;
252 }
253
254 int WINAPI profile_load_int( const char *filename, const char *section, const char *key, int default_value ){
255         char value[1024];
256
257         if ( read_var( filename, section, key, value ) ) {
258                 return atoi( value );
259         }
260         else{
261                 return default_value;
262         }
263 }
264
265 float WINAPI profile_load_float( const char *filename, const char *section, const char *key, float default_value ){
266         char value[1024];
267
268         if ( read_var( filename, section, key, value ) ) {
269                 return atof( value );
270         }
271         else{
272                 return default_value;
273         }
274 }
275
276 char* WINAPI profile_load_string( const char *filename, const char *section, const char *key, const char *default_value ){
277         static Str ret;
278         char value[1024];
279
280         if ( read_var( filename, section, key, value ) ) {
281                 ret = value;
282         }
283         else{
284                 ret = default_value;
285         }
286
287         return (char*)ret.GetBuffer();
288 }