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