2 Copyright (c) 2001, Loki software, inc.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
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.
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
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.
32 // Application settings load/save
34 // Leonardo Zide (leo@lokigames.com)
46 // =============================================================================
49 bool read_var( const char *filename, const char *section, const char *key, char *value ){
50 char line[1024], *ptr;
53 rc = fopen( filename, "rt" );
59 while ( fgets( line, 1024, rc ) != 0 )
61 // First we find the section
62 if ( line[0] != '[' ) {
66 ptr = strchr( line, ']' );
69 if ( strcmp( &line[1], section ) == 0 ) {
70 while ( fgets( line, 1024, rc ) != 0 )
72 ptr = strchr( line, '=' );
75 // reached the end of the section
82 while ( line[strlen( line ) - 1] == ' ' )
83 line[strlen( line ) - 1] = '\0';
85 if ( strcmp( line, key ) == 0 ) {
86 strcpy( value, ptr + 1 );
89 if ( value[strlen( value ) - 1] == 10 || value[strlen( value ) - 1] == 13 || value[strlen( value ) - 1] == 32 ) {
90 value[strlen( value ) - 1] = 0;
103 static bool save_var( const char *filename, const char *section, const char *key, const char *value ){
104 char line[1024], *ptr;
109 rc = fopen( filename, "rb" );
115 fseek( rc, 0, SEEK_END );
118 buf = qmalloc( len );
119 fread( buf, len, 1, rc );
120 old_rc.Write( buf, len );
123 old_rc.Seek( 0, SEEK_SET );
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" );
133 // First we need to find the section
135 while ( old_rc.ReadString( line, 1024 ) != NULL )
139 if ( line[0] == '[' ) {
140 ptr = strchr( line, ']' );
143 if ( strcmp( &line[1], section ) == 0 ) {
152 fprintf( rc, "[%s]\n", section );
155 fprintf( rc, "%s=%s\n", key, value );
157 while ( old_rc.ReadString( line, 1024 ) != NULL )
159 ptr = strchr( line, '=' );
164 if ( strcmp( line, key ) == 0 ) {
178 while ( old_rc.ReadString( line, 1024 ) != NULL )
185 // =============================================================================
188 bool WINAPI profile_save_int( const char *filename, const char *section, const char *key, int value ){
190 sprintf( buf, "%d", value );
191 return save_var( filename, section, key, buf );
194 bool WINAPI profile_save_float( const char *filename, const char *section, const char *key, float value ){
196 sprintf( buf, "%f", value );
197 return save_var( filename, section, key, buf );
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 );
204 bool profile_save_buffer( const char * rc_path, const char *name, void *buffer, guint32 size ){
206 char filename[PATH_MAX];
207 sprintf( filename, "%s/%s.bin", rc_path, name );
210 f = fopen( filename, "wb" );
213 if ( fwrite( buffer, size, 1, f ) == 1 ) {
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 );
230 f = fopen( filename, "rb" );
233 fseek( f, 0, SEEK_END );
237 if ( len > *plSize ) {
244 if ( fread( buffer, len, 1, f ) == 1 ) {
254 int WINAPI profile_load_int( const char *filename, const char *section, const char *key, int default_value ){
257 if ( read_var( filename, section, key, value ) ) {
258 return atoi( value );
261 return default_value;
265 float WINAPI profile_load_float( const char *filename, const char *section, const char *key, float default_value ){
268 if ( read_var( filename, section, key, value ) ) {
269 return atof( value );
272 return default_value;
276 char* WINAPI profile_load_string( const char *filename, const char *section, const char *key, const char *default_value ){
280 if ( read_var( filename, section, key, value ) ) {
287 return (char*)ret.GetBuffer();