]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/profile.cpp
* improved quake2 support by added a quake2 boolean to the CGameDescription class...
[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 {
51   char line[1024], *ptr;
52   FILE *rc;
53   
54   rc = fopen (filename, "rt");
55   
56   if (rc == NULL)
57     return false;
58   
59   while (fgets (line, 1024, rc) != 0)
60   {
61     // First we find the section
62     if (line[0] != '[')
63       continue;
64     
65     ptr = strchr (line, ']');
66     *ptr = '\0';
67     
68     if (strcmp (&line[1], section) == 0)
69     {
70       while (fgets (line, 1024, rc) != 0)
71       {
72         ptr = strchr (line, '=');
73
74         if (ptr == NULL)
75         {
76           // reached the end of the section
77           fclose (rc);
78           return false;
79         }
80         *ptr = '\0';
81
82         // remove spaces
83         while (line[strlen(line)-1] == ' ')
84           line[strlen(line)-1] = '\0';
85
86         if (strcmp (line, key) == 0)
87         {
88           strcpy (value, ptr+1);
89           fclose (rc);
90           
91           if (value[strlen (value)-1] == 10 || value[strlen (value)-1] == 13 || value[strlen (value)-1] == 32)
92             value[strlen (value)-1] = 0;
93
94           return true;
95         }
96       }
97     }
98   }
99
100   fclose (rc);
101   return false;
102 }
103
104 static bool save_var (const char *filename, const char *section, const char *key, const char *value)
105 {
106   char line[1024], *ptr;
107   MemStream old_rc;
108   bool found;
109   FILE *rc;
110
111   rc = fopen (filename, "rb");
112
113   if (rc != NULL)
114   {
115     guint32 len;
116     void *buf;
117
118     fseek (rc, 0, SEEK_END);
119     len = ftell (rc);
120     rewind (rc);
121     buf = qmalloc (len);
122     fread (buf, len, 1, rc);
123     old_rc.Write (buf, len);
124     free (buf);
125     fclose (rc);
126     old_rc.Seek (0, SEEK_SET);
127   }
128
129   // TTimo: changed to binary writing. It doesn't seem to affect linux version, and win32 version was happending a lot of '\n'
130   rc = fopen (filename, "wb");
131
132   if (rc == NULL)
133     return false;
134
135   // First we need to find the section
136   found = false;
137   while (old_rc.ReadString (line, 1024) != NULL)
138   {
139     fputs (line, rc);
140
141     if (line[0] == '[')
142     {
143       ptr = strchr (line, ']');
144       *ptr = '\0';
145
146       if (strcmp (&line[1], section) == 0)
147       {
148         found = true;
149         break;
150       }
151     }
152   } 
153
154   if (!found)
155   {
156     fputs ("\n", rc);
157     fprintf (rc, "[%s]\n", section);
158   }
159
160   fprintf (rc, "%s=%s\n", key, value);
161
162   while (old_rc.ReadString (line, 1024) != NULL)
163   {
164     ptr = strchr (line, '=');
165
166     if (ptr != NULL)
167     {
168       *ptr = '\0';
169
170       if (strcmp (line, key) == 0)
171         break;
172  
173       *ptr = '=';
174       fputs (line, rc);
175     }
176     else
177     {
178       fputs (line, rc);
179       break;
180     }
181   }
182
183   while (old_rc.ReadString (line, 1024) != NULL)
184     fputs (line, rc);
185
186   fclose (rc);
187   return true;
188 }
189
190 // =============================================================================
191 // Global functions
192
193 bool WINAPI profile_save_int (const char *filename, const char *section, const char *key, int value)
194 {
195   char buf[16];
196   sprintf (buf, "%d", value);
197   return save_var (filename, section, key, buf);
198 }
199
200 bool WINAPI profile_save_float (const char *filename, const char *section, const char *key, float value)
201 {
202   char buf[16];
203   sprintf (buf, "%f", value);
204   return save_var (filename, section, key, buf);
205 }
206
207 bool WINAPI profile_save_string (const char * filename, const char *section, const char *key, const char *value)
208 {
209   return save_var (filename, section, key, value);
210 }
211
212 bool profile_save_buffer (const char * rc_path, const char *name, void *buffer, guint32 size)
213 {
214   bool ret = false;
215   char filename[PATH_MAX];
216   sprintf (filename, "%s/%s.bin", rc_path, name);
217   FILE *f;
218
219   f = fopen (filename, "wb");
220
221   if (f != NULL)
222   {
223     if (fwrite (buffer, size, 1, f) == 1)
224       ret = true;
225
226     fclose (f);
227   }
228
229   return ret;
230 }
231
232 bool profile_load_buffer (const char * rc_path, const char *name, void *buffer, guint32 *plSize)
233 {
234   char filename[PATH_MAX];
235   sprintf (filename, "%s/%s.bin", rc_path, name);
236   bool ret = false;
237   guint32 len;
238   FILE *f;
239
240   f = fopen (filename, "rb");
241
242   if (f != NULL)
243   {
244     fseek (f, 0, SEEK_END);
245     len = ftell (f);
246     rewind (f);
247
248     if (len > *plSize)
249       len = *plSize;
250     else
251       *plSize = len;
252
253     if (fread (buffer, len, 1, f) == 1)
254       ret = true;
255
256     fclose (f);
257   }
258
259   return true;
260 }
261
262 int WINAPI profile_load_int (const char *filename, const char *section, const char *key, int default_value)
263 {
264   char value[1024];
265
266   if (read_var (filename, section, key, value))
267     return atoi (value);
268   else
269     return default_value;
270 }
271
272 float WINAPI profile_load_float (const char *filename, const char *section, const char *key, float default_value)
273 {
274   char value[1024];
275
276   if (read_var (filename, section, key, value))
277     return atof (value);
278   else
279     return default_value;
280 }
281
282 char* WINAPI profile_load_string (const char *filename, const char *section, const char *key, const char *default_value)
283 {
284   static Str ret;
285   char value[1024];
286
287   if (read_var (filename, section, key, value))
288     ret = value;
289   else
290     ret = default_value;
291
292   return (char*)ret.GetBuffer ();
293 }