]> icculus.org git repositories - taylor/freespace2.git/blob - src/osapi/osregistry.cpp
added copyright header
[taylor/freespace2.git] / src / osapi / osregistry.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include <windows.h>
10 #include <string.h>
11 #include "pstypes.h"
12 #include "osregistry.h"
13
14 // NEVER ADD ANY OTHER INCLUDES HERE - YOU'LL LIKELY BREAK THE LAUNCHER!!!!!!!!!!!!!
15
16 // ------------------------------------------------------------------------------------------------------------
17 // REGISTRY DEFINES/VARS
18 //
19
20 static char                     szCompanyName[128];
21 static char                     szAppName[128];
22 static char                     szAppVersion[128];
23
24 char *Osreg_company_name = "Volition";
25 char *Osreg_class_name = "Freespace2Class";
26 #if defined(FS2_DEMO)
27 char *Osreg_app_name = "FreeSpace2Demo";
28 char *Osreg_title = "Freespace 2 Demo";
29 #elif defined(OEM_BUILD)
30 char *Osreg_app_name = "FreeSpace2OEM";
31 char *Osreg_title = "Freespace 2 OEM";
32 #else
33 char *Osreg_app_name = "FreeSpace2";
34 char *Osreg_title = "Freespace 2";
35 #endif
36
37 int Os_reg_inited = 0;
38
39
40 // ------------------------------------------------------------------------------------------------------------
41 // REGISTRY FUNCTIONS
42 //
43
44 // os registry functions -------------------------------------------------------------
45
46 // initialize the registry. setup default keys to use
47 void os_init_registry_stuff(char *company, char *app, char *version)
48 {
49         if(company){
50                 strcpy( szCompanyName, company );       
51         } else {
52                 strcpy( szCompanyName, Osreg_company_name);
53         }
54
55         if(app){
56                 strcpy( szAppName, app );       
57         } else {
58                 strcpy( szAppName, Osreg_app_name);
59         }
60
61         if(version){
62                 strcpy( szAppVersion, version); 
63         } else {
64                 strcpy( szAppVersion, "1.0");
65         }
66
67         Os_reg_inited = 1;
68 }
69
70 // Removes a value from to the INI file.  Passing
71 // name=NULL will delete the section.
72 void os_config_remove( char *section, char *name )
73 {
74         HKEY hKey = NULL;
75         DWORD dwDisposition;
76         char keyname[1024];
77         LONG lResult;   
78
79         if(!Os_reg_inited){
80                 return;
81         }
82
83         if ( section )  {
84                 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
85         } else {
86                 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
87         }
88
89         // remove the value
90         if ( !name )    {
91                 if ( !section ) {                       
92                         goto Cleanup;
93                 }
94                 lResult = RegDeleteKey( HKEY_LOCAL_MACHINE, keyname );
95                 if ( lResult != ERROR_SUCCESS ) {                       
96                         goto Cleanup;
97                 }
98         } else  {
99                 lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE,                                           // Where to add it
100                                                                                                  keyname,                                                               // name of key
101                                                                                                  NULL,                                                                  // DWORD reserved
102                                                                                                  "",                                                                            // Object class
103                                                                                                  REG_OPTION_NON_VOLATILE,                       // Save to disk
104                                                                                                  KEY_ALL_ACCESS,                                                // Allows all changes
105                                                                                                  NULL,                                                                  // Default security attributes
106                                                                                                  &hKey,                                                 // Location to store key
107                                                                                                  &dwDisposition );                                      // Location to store status of key
108
109                 if ( lResult != ERROR_SUCCESS ) {                       
110                         goto Cleanup;
111                 }
112
113                 lResult = RegDeleteValue( hKey, name );
114                 if ( lResult != ERROR_SUCCESS ) {                       
115                         goto Cleanup;
116                 }
117         }
118
119 Cleanup:
120         if ( hKey )
121                 RegCloseKey(hKey);
122 }
123
124 // Writes a string to the INI file.  If value is NULL,
125 // removes the string. Writing a NULL value to a NULL name will delete
126 // the section.
127 void os_config_write_string( char *section, char *name, char *value )
128 {
129         HKEY hKey = NULL;
130         DWORD dwDisposition;
131         char keyname[1024];
132         LONG lResult;   
133
134         if(!Os_reg_inited){
135                 return;
136         }
137
138         if ( section )  {
139                 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
140         } else {
141                 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
142         }
143
144         lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE,                                   // Where to add it
145                                                                                          keyname,                                                       // name of key
146                                                                                          NULL,                                                          // DWORD reserved
147                                                                                          "",                                                                    // Object class
148                                                                                          REG_OPTION_NON_VOLATILE,               // Save to disk
149                                                                                          KEY_ALL_ACCESS,                                        // Allows all changes
150                                                                                          NULL,                                                          // Default security attributes
151                                                                                          &hKey,                                                         // Location to store key
152                                                                                          &dwDisposition );                              // Location to store status of key
153
154         if ( lResult != ERROR_SUCCESS ) {               
155                 goto Cleanup;
156         }
157
158         if ( !name )     {              
159                 goto Cleanup;
160         }
161                 
162         lResult = RegSetValueEx( hKey,                                                                  // Handle to key
163                                                                          name,                                                                  // The values name
164                                                                          NULL,                                                                  // DWORD reserved
165                                                                          REG_SZ,                                                                        // null terminated string
166                                                                          (CONST BYTE *)value,                           // value to set
167                                                                          strlen(value) + 1 );                           // How many bytes to set
168                                                                                                                                                         
169         if ( lResult != ERROR_SUCCESS ) {               
170                 goto Cleanup;
171         }
172
173
174 Cleanup:
175         if ( hKey )
176                 RegCloseKey(hKey);
177 }
178
179 // same as previous function except we don't use the application name to build up the keyname
180 void os_config_write_string2( char *section, char *name, char *value )
181 {
182         HKEY hKey = NULL;
183         DWORD dwDisposition;
184         char keyname[1024];
185         LONG lResult;   
186
187         if(!Os_reg_inited){
188                 return;
189         }
190
191         if ( section )  {
192                 sprintf( keyname, "Software\\%s\\%s", szCompanyName, section );
193         } else {
194                 sprintf( keyname, "Software\\%s", szCompanyName );
195         }
196
197         lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE,                                   // Where to add it
198                                                                                          keyname,                                                       // name of key
199                                                                                          NULL,                                                          // DWORD reserved
200                                                                                          "",                                                                    // Object class
201                                                                                          REG_OPTION_NON_VOLATILE,               // Save to disk
202                                                                                          KEY_ALL_ACCESS,                                        // Allows all changes
203                                                                                          NULL,                                                          // Default security attributes
204                                                                                          &hKey,                                                         // Location to store key
205                                                                                          &dwDisposition );                              // Location to store status of key
206
207         if ( lResult != ERROR_SUCCESS ) {               
208                 goto Cleanup;
209         }
210
211         if ( !name )     {              
212                 goto Cleanup;
213         }
214                 
215         lResult = RegSetValueEx( hKey,                                                                  // Handle to key
216                                                                          name,                                                                  // The values name
217                                                                          NULL,                                                                  // DWORD reserved
218                                                                          REG_SZ,                                                                        // null terminated string
219                                                                          (CONST BYTE *)value,                           // value to set
220                                                                          strlen(value) + 1 );                           // How many bytes to set
221                                                                                                                                                         
222         if ( lResult != ERROR_SUCCESS ) {               
223                 goto Cleanup;
224         }
225
226
227 Cleanup:
228         if ( hKey )
229                 RegCloseKey(hKey);
230 }
231
232 // Writes an unsigned int to the INI file.  
233 void os_config_write_uint( char *section, char *name, uint value )
234 {
235         HKEY hKey = NULL;
236         DWORD dwDisposition;
237         char keyname[1024];
238         LONG lResult;   
239
240         if(!Os_reg_inited){
241                 return;
242         }
243
244         if ( section )  {
245                 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
246         } else {
247                 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
248         }
249
250         lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE,                                           // Where to add it
251                                                                                          keyname,                                                               // name of key
252                                                                                          NULL,                                                                  // DWORD reserved
253                                                                                          "",                                                                            // Object class
254                                                                                          REG_OPTION_NON_VOLATILE,                       // Save to disk
255                                                                                          KEY_ALL_ACCESS,                                                // Allows all changes
256                                                                                          NULL,                                                                  // Default security attributes
257                                                                                          &hKey,                                                 // Location to store key
258                                                                                          &dwDisposition );                                      // Location to store status of key
259
260         if ( lResult != ERROR_SUCCESS ) {               
261                 goto Cleanup;
262         }
263
264         if ( !name )     {              
265                 goto Cleanup;
266         }
267                 
268         lResult = RegSetValueEx( hKey,                                                                  // Handle to key
269                                                                          name,                                                                                  // The values name
270                                                                          NULL,                                                                                  // DWORD reserved
271                                                                          REG_DWORD,                                                                             // null terminated string
272                                                                          (CONST BYTE *)&value,                                          // value to set
273                                                                          4 );                                                           // How many bytes to set
274                                                                                                                                                                 
275         if ( lResult != ERROR_SUCCESS ) {               
276                 goto Cleanup;
277         }
278
279 Cleanup:
280         if ( hKey )
281                 RegCloseKey(hKey);
282
283 }
284
285
286 // Reads a string from the INI file.  If default is passed,
287 // and the string isn't found, returns ptr to default otherwise
288 // returns NULL;    Copy the return value somewhere before
289 // calling os_read_string again, because it might reuse the
290 // same buffer.
291 static char tmp_string_data[1024];
292 char * os_config_read_string( char *section, char *name, char *default_value )
293 {
294         HKEY hKey = NULL;
295         DWORD dwType, dwLen;
296         char keyname[1024];
297         LONG lResult;   
298
299         if(!Os_reg_inited){
300                 return NULL;
301         }
302
303         if ( section )  {
304                 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
305         } else {
306                 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
307         }
308
309         lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,                                                     // Where it is
310                                                                                          keyname,                                                               // name of key
311                                                                                          NULL,                                                                  // DWORD reserved
312                                                                                          KEY_QUERY_VALUE,                                               // Allows all changes
313                                                                                          &hKey );                                                               // Location to store key
314
315         if ( lResult != ERROR_SUCCESS ) {               
316                 goto Cleanup;
317         }
318
319         if ( !name )     {              
320                 goto Cleanup;
321         }
322
323         dwLen = 1024;
324         lResult = RegQueryValueEx( hKey,                                                                        // Handle to key
325                                                                          name,                                                                                  // The values name
326                                                                          NULL,                                                                                  // DWORD reserved
327                                  &dwType,                                                                               // What kind it is
328                                                                          (ubyte *)&tmp_string_data,                                             // value to set
329                                                                          &dwLen );                                                              // How many bytes to set
330                                                                                                                                                                 
331         if ( lResult != ERROR_SUCCESS ) {               
332                 goto Cleanup;
333         }
334
335         default_value = tmp_string_data;
336
337 Cleanup:
338         if ( hKey )
339                 RegCloseKey(hKey);
340
341         return default_value;
342 }
343
344 // same as previous function except we don't use the application name to build up the keyname
345 char * os_config_read_string2( char *section, char *name, char *default_value )
346 {
347         HKEY hKey = NULL;
348         DWORD dwType, dwLen;
349         char keyname[1024];
350         LONG lResult;   
351
352         if(!Os_reg_inited){
353                 return NULL;
354         }
355
356         if ( section )  {
357                 sprintf( keyname, "Software\\%s\\%s", szCompanyName, section );
358         } else {
359                 sprintf( keyname, "Software\\%s", szCompanyName );
360         }
361
362         lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,                                                     // Where it is
363                                                                                          keyname,                                                               // name of key
364                                                                                          NULL,                                                                  // DWORD reserved
365                                                                                          KEY_QUERY_VALUE,                                               // Allows all changes
366                                                                                          &hKey );                                                               // Location to store key
367
368         if ( lResult != ERROR_SUCCESS ) {               
369                 goto Cleanup;
370         }
371
372         if ( !name )     {              
373                 goto Cleanup;
374         }
375
376         dwLen = 1024;
377         lResult = RegQueryValueEx( hKey,                                                                        // Handle to key
378                                                                          name,                                                                                  // The values name
379                                                                          NULL,                                                                                  // DWORD reserved
380                                  &dwType,                                                                               // What kind it is
381                                                                          (ubyte *)&tmp_string_data,                                             // value to set
382                                                                          &dwLen );                                                              // How many bytes to set
383                                                                                                                                                                 
384         if ( lResult != ERROR_SUCCESS ) {               
385                 goto Cleanup;
386         }
387
388         default_value = tmp_string_data;
389
390 Cleanup:
391         if ( hKey )
392                 RegCloseKey(hKey);
393
394         return default_value;
395 }
396
397 // Reads a string from the INI file.  Default_value must 
398 // be passed, and if 'name' isn't found, then returns default_value
399 uint  os_config_read_uint( char *section, char *name, uint default_value )
400 {
401         HKEY hKey = NULL;
402         DWORD dwType, dwLen;
403         char keyname[1024];
404         LONG lResult;
405         uint tmp_val;   
406
407         if(!Os_reg_inited){
408                 return NULL;
409         }
410
411         if ( section )  {
412                 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
413         } else {
414                 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
415         }
416
417         lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,                                                     // Where it is
418                                                                                          keyname,                                                               // name of key
419                                                                                          NULL,                                                                  // DWORD reserved
420                                                                                          KEY_QUERY_VALUE,                                               // Allows all changes
421                                                                                          &hKey );                                                               // Location to store key
422
423         if ( lResult != ERROR_SUCCESS ) {               
424                 goto Cleanup;
425         }
426
427         if ( !name )     {              
428                 goto Cleanup;
429         }
430
431         dwLen = 4;
432         lResult = RegQueryValueEx( hKey,                                                                        // Handle to key
433                                                                          name,                                                                                  // The values name
434                                                                          NULL,                                                                                  // DWORD reserved
435                                  &dwType,                                                                               // What kind it is
436                                                                          (ubyte *)&tmp_val,                                             // value to set
437                                                                          &dwLen );                                                              // How many bytes to set
438                                                                                                                                                                 
439         if ( lResult != ERROR_SUCCESS ) {               
440                 goto Cleanup;
441         }
442
443         default_value = tmp_val;
444
445 Cleanup:
446         if ( hKey )
447                 RegCloseKey(hKey);
448
449         return default_value;
450 }
451
452 // uses Ex versions of Windows registry functions
453 static char tmp_string_data_ex[1024];
454 char * os_config_read_string_ex( char *keyname, char *name, char *default_value )
455 {
456         HKEY hKey = NULL;
457         DWORD dwType, dwLen;
458         LONG lResult;
459
460         lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,                                                     // Where it is
461                                                                                          keyname,                                                               // name of key
462                                                                                          NULL,                                                                  // DWORD reserved
463                                                                                          KEY_QUERY_VALUE,                                               // Allows all changes
464                                                                                          &hKey );                                                               // Location to store key
465
466         if ( lResult != ERROR_SUCCESS ) {
467                 //mprintf(( "Error opening registry key '%s'\n", keyname ));
468                 goto Cleanup;
469         }
470
471         if ( !name )     {
472                 //mprintf(( "No variable name passed\n" ));
473                 goto Cleanup;
474         }
475
476         dwLen = 1024;
477         lResult = RegQueryValueEx( hKey,                                                                        // Handle to key
478                                                                          name,                                                                                  // The values name
479                                                                          NULL,                                                                                  // DWORD reserved
480                                  &dwType,                                                                               // What kind it is
481                                                                          (ubyte *)&tmp_string_data_ex,                                          // value to set
482                                                                          &dwLen );                                                              // How many bytes to set
483                                                                                                                                                                 
484         if ( lResult != ERROR_SUCCESS ) {
485                 //mprintf(( "Error reading registry key '%s'\n", name ));
486                 goto Cleanup;
487         }
488
489         default_value = tmp_string_data_ex;
490
491 Cleanup:
492         if ( hKey )
493                 RegCloseKey(hKey);
494
495         return default_value;
496 }
497