~ubuntu-branches/ubuntu/raring/powermanga/raring

« back to all changes in this revision

Viewing changes to src/configfile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-19 10:15:56 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060819101556-llg7innyzoadukro
Tags: 0.80-3ubuntu1
* Re-merge with Debian
* Fix desktop file for validation
* Install icon in /usr/share/pixmaps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//******************************************************************************
 
2
// Copyright (C) 1998-2005 TLK Games all rights reserved.
 
3
//------------------------------------------------------------------------------
 
4
// file         : "configfile.cpp"
 
5
// created      : 2005-12-12
 
6
// updates      : 2005-01-26
 
7
// id           : $Id: configfile.cpp,v 1.7 2006/03/27 09:13:14 gurumeditation Exp $
 
8
//------------------------------------------------------------------------------
 
9
// This program is free software; you can redistribute it and/or modify it under
 
10
// the terms of the GNU General Public License as published by the Free Software
 
11
// Foundation; either version 2 of the License, or (at your option) any later
 
12
// version.
 
13
// 
 
14
// This program is distributed in the hope that it will be useful, but WITHOUT
 
15
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
16
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
17
// details.
 
18
//  
 
19
// You should have received a copy of the GNU General Public License along with
 
20
// this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
21
// Place - Suite 330, Boston, MA  02111-1307, USA.
 
22
//******************************************************************************
 
23
#include "powermanga.hpp"
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
//..............................................................................
 
28
char* configfile::lang2text[XXLANGUAGE] = {"en", "fr"};
 
29
 
 
30
//------------------------------------------------------------------------------
 
31
// create object
 
32
//------------------------------------------------------------------------------
 
33
        configfile::configfile()
 
34
{
 
35
        resetvalue();
 
36
}
 
37
 
 
38
 
 
39
//------------------------------------------------------------------------------
 
40
// destroy object
 
41
//------------------------------------------------------------------------------
 
42
        configfile::~configfile()
 
43
{
 
44
}
 
45
 
 
46
//------------------------------------------------------------------------------
 
47
// reset all values
 
48
//------------------------------------------------------------------------------
 
49
void    configfile::resetvalue()
 
50
{
 
51
        fullscreen = 1;
 
52
        nosound = 0;
 
53
        resolution = 640;
 
54
        verbose = 0;
 
55
        difficulty = 1; 
 
56
        lang = 0;
 
57
}
 
58
 
 
59
//------------------------------------------------------------------------------
 
60
// display values
 
61
//------------------------------------------------------------------------------
 
62
void configfile::configinfo()
 
63
{
 
64
        fprintf(stdout, "  <config info>\n"
 
65
                        "- fullscreen: %i\n- nosound: %i\n- resolution:%i\n"
 
66
                "- verbose: %i\n - difficulty: %i\n - lang:%s\n", 
 
67
                fullscreen, nosound, resolution, verbose, difficulty, 
 
68
                lang2text[lang]);
 
69
}
 
70
 
 
71
//------------------------------------------------------------------------------
 
72
// check if config directory exists; if not create it and set config_dir 
 
73
//------------------------------------------------------------------------------
 
74
Sint32 configfile::tocheckdir()
 
75
{
 
76
 
 
77
#ifdef _WIN32
 
78
 
 
79
        _snprintf(config_dir, sizeof(config_dir) - 1, "./%s", CONFIG_DIR_NAME );
 
80
        
 
81
        /* create directory if not exist */
 
82
        MKDIR(config_dir, S_IRWXU);
 
83
        fprintf(stderr, "ok\n");
 
84
 
 
85
#else
 
86
 
 
87
        snprintf(config_dir, sizeof(config_dir) - 1, "%s/%s",
 
88
                (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
 
89
        /* test and create .lgames */
 
90
        if(!opendir(config_dir)) {
 
91
                fprintf(stderr, "couldn't find/open config directory '%s'\n", config_dir);
 
92
                fprintf(stderr, "attempting to create it... " );
 
93
                MKDIR(config_dir, S_IRWXU );
 
94
                if(!opendir(config_dir))
 
95
                {       fprintf(stderr, "failed\n");
 
96
                        return 0;
 
97
                }
 
98
                else
 
99
                        fprintf(stderr, "ok\n");
 
100
        } 
 
101
#endif
 
102
 
 
103
        return 1;
 
104
}
 
105
 
 
106
 
 
107
//------------------------------------------------------------------------------
 
108
// load config file "~/.tlkgames/powermanga.conf"
 
109
//------------------------------------------------------------------------------
 
110
void configfile::loadconfig()
 
111
{
 
112
        FILE *pfile = NULL;
 
113
        resetvalue();
 
114
        if (!tocheckdir()) return;
 
115
        sprintf(configname, "%s/%s", config_dir, CONFIG_FILE_NAME);
 
116
        pfile = fopen_data(configname, "r");
 
117
        if (pfile == NULL) return;
 
118
        lisp_stream_t stream;   
 
119
        lisp_object_t *root_obj = NULL;
 
120
        lisp_stream_init_file (&stream, pfile);
 
121
        root_obj = lisp_read (&stream);
 
122
 
 
123
        fclose(pfile);
 
124
 
 
125
        if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
 
126
        {       fprintf(stderr, "configfile::loadconfig() / conf parsing failed\n");
 
127
                return;
 
128
        }
 
129
 
 
130
        if (strcmp(lisp_symbol(lisp_car(root_obj)), "powermanga-config") != 0)
 
131
        {       fprintf(stderr, "configfile::loadconfig() / conf parsing failed\n");
 
132
                return;
 
133
        }
 
134
        LispReader reader(lisp_cdr(root_obj));
 
135
        std::string ptStr;
 
136
        if (!reader.read_string ("lang", &ptStr))
 
137
                lang = ENLANGUAGE; 
 
138
         else 
 
139
        {       if (ptStr == "fr")
 
140
                        lang = FRLANGUAGE; 
 
141
                else
 
142
                        lang = ENLANGUAGE; 
 
143
        }
 
144
        if (!reader.read_bool("fullscreen", &fullscreen))
 
145
                fullscreen = true;
 
146
        
 
147
        if (!reader.read_bool("nosound", &nosound))
 
148
                nosound = false;
 
149
        if (!reader.read_bool("nosync", &nosync))
 
150
                nosync = false;
 
151
        if (!reader.read_int ("verbose", &verbose))
 
152
                verbose = 0;
 
153
 
 
154
 
 
155
        if (!reader.read_int ("resolution", &resolution)) 
 
156
                resolution = 640; 
 
157
        if (resolution != 320 && resolution != 640) 
 
158
                resolution = 640;
 
159
        lisp_free(root_obj);
 
160
}
 
161
 
 
162
//------------------------------------------------------------------------------
 
163
// save config file "~/.tlkgames/powermanga.conf"
 
164
//------------------------------------------------------------------------------
 
165
void configfile::saveconfig()
 
166
{
 
167
        FILE * config = fopen_data(configname, "w");
 
168
        if(config)
 
169
        {
 
170
                fprintf(config, "(powermanga-config\n");
 
171
                fprintf(config, "\t;; the following options can be set to #t or #f:\n");
 
172
                fprintf(config, "\t(fullscreen %s)\n", fullscreen ? "#t" : "#f");
 
173
                fprintf(config, "\t(nosound %s)\n", nosound ? "#t" : "#f");
 
174
                fprintf(config, "\t(nosync %s)\n", nosync ? "#t" : "#f");
 
175
                 
 
176
                fprintf(config, "\n\t;; window size (320 or 640):\n");
 
177
                fprintf(config, "\t(resolution  %d)\n", resolution);
 
178
                
 
179
                fprintf(config, "\n\t;; verbose mode 0 (disabled), 1 (enable) or 2 (more messages)\n");
 
180
                fprintf(config, "\t(verbose   %d)\n", verbose);
 
181
                
 
182
                fprintf(config, "\n\t;; difficulty 0 (easy), 1 (normal) or 2 (hard)\n");
 
183
                fprintf(config, "\t(difficulty   %d)\n", difficulty);
 
184
 
 
185
                fprintf(config, "\n\t;; langage en or fr\n");
 
186
                fprintf(config, "\t(lang      ");
 
187
                switch (lang) 
 
188
                {       case FRLANGUAGE:
 
189
                                fprintf(config, "\"fr\")\n");
 
190
                                break;
 
191
                        default:
 
192
                                fprintf(config, "\"en\")\n");
 
193
                                break;
 
194
                }               
 
195
                fprintf(config, ")\n");
 
196
 
 
197
        }
 
198
 
 
199
        fclose(config);
 
200
}
 
201
 
 
202
//------------------------------------------------------------------------------
 
203
// open a file
 
204
//------------------------------------------------------------------------------
 
205
FILE * configfile::fopen_data(const char *fname, const char *fmode)
 
206
{
 
207
        FILE * fi;
 
208
        fi = fopen(fname, fmode);
 
209
        if (fi == NULL)
 
210
        {       fprintf(stderr, "configfile::fopen_data(): Warning: Unable "
 
211
                        "to open the file \"%s\" ", fname);
 
212
                if (strcmp(fmode, "r") == 0)
 
213
                        fprintf(stderr, "for read!!!\n");
 
214
                else if (strcmp(fmode, "w") == 0)
 
215
                        fprintf(stderr, "for write!!!\n");
 
216
        }
 
217
        return(fi);
 
218
}
 
219
 
 
220
 
 
221
//------------------------------------------------------------------------------
 
222
// analyse command line paramaters
 
223
//------------------------------------------------------------------------------
 
224
Sint32 configfile::scanZeArgs(Sint32 nbArg, char **ptArg) 
 
225
{
 
226
        for(Sint32 _iIndex = 1; _iIndex < nbArg; _iIndex++)
 
227
        {       
 
228
                if(*ptArg[_iIndex] != '-')
 
229
                        continue;
 
230
 
 
231
                //######################################################
 
232
                // help
 
233
                //######################################################
 
234
                if(!strcmp(ptArg[_iIndex], "-h") ||
 
235
                        !strcmp(ptArg[_iIndex], "--help"))
 
236
                {
 
237
                        fprintf(stdout, "\noptions:\n"
 
238
                                "-h, --help     print Help (this message) and exit\n"
 
239
                                "--version      print version information and exit\n"
 
240
                                "--320          game run in a 320*200 window (slow machine)\n");
 
241
#ifdef SDL_TLK
 
242
                        fprintf(stdout,
 
243
                                "--window       windowed mode (full screen by default) \n");                    
 
244
#endif
 
245
                        fprintf(stdout,
 
246
                                "-v             verbose mode\n"
 
247
                                "--verbose      verbose mode (more messages)\n"
 
248
                                "--nosound      force no sound\n"
 
249
                                "--nosync       disable timer\n"
 
250
                                "--easy         easy bonuses\n"
 
251
                                "--hard         hard bonuses\n"
 
252
                                "--------------------------------------------------------------\n"
 
253
                                "keys recognized during the game:\n"
 
254
                                "CTRL+S         enable/disable the music\n"
 
255
                                "CTRL+Q         finish the play current\n"
 
256
                                "CTRL+A         about Powermanga\n"
 
257
                                "F10            quit Powermanga\n"
 
258
                                "P              enable/disable pause\n");
 
259
#ifdef SDL_TLK
 
260
                        fprintf(stdout,
 
261
                                "F              switch between full screen and windowed mode\n");
 
262
#endif
 
263
return 0;
 
264
                        
 
265
                }
 
266
 
 
267
        
 
268
                if(!strcmp(ptArg[_iIndex], "--version"))
 
269
                {       printf(POWERMANGA_VERSION);
 
270
                        printf("\n");
 
271
                        printf("copyright (c) 1998-2006 TLK Games\n");
 
272
                        printf("website: http://linux.tlk.fr/\n");
 
273
                        return 0;
 
274
                }       
 
275
 
 
276
                //######################################################
 
277
                //force window mode
 
278
                //######################################################
 
279
                if(!strcmp(ptArg[_iIndex], "--window"))
 
280
                {       fullscreen = false;
 
281
                        continue;
 
282
                }
 
283
 
 
284
                //######################################################
 
285
                //force fullscreen mode
 
286
                //######################################################
 
287
                if(!strcmp(ptArg[_iIndex], "--fullscreen"))
 
288
                {       fullscreen = true;
 
289
                        continue;
 
290
                }
 
291
                
 
292
                //######################################################
 
293
                // resolution, low-res or high-res
 
294
                //######################################################
 
295
                if(!strcmp(ptArg[_iIndex], "--320"))
 
296
                {       resolution = 320;
 
297
                        continue;
 
298
                }
 
299
                if(!strcmp(ptArg[_iIndex], "--640"))
 
300
                {       resolution = 640;
 
301
                        continue;
 
302
                }
 
303
                
 
304
                //######################################################
 
305
                // enable verbose mode          
 
306
                //######################################################
 
307
                if(!strcmp(ptArg[_iIndex], "-v"))
 
308
                {       verbose = 1;
 
309
                        continue;
 
310
                }
 
311
                if(!strcmp(ptArg[_iIndex], "--verbose"))
 
312
                {       verbose = 2;
 
313
                        continue;
 
314
                }
 
315
 
 
316
                //######################################################
 
317
                // disable sound
 
318
                //######################################################
 
319
                if(!strcmp(ptArg[_iIndex], "--nosound"))
 
320
                {       nosound = true;
 
321
                        continue;
 
322
                }
 
323
 
 
324
                //######################################################
 
325
                // disable timer
 
326
                //######################################################
 
327
                if(!strcmp(ptArg[_iIndex], "--nosync"))
 
328
                {       nosync = true;
 
329
                        continue;
 
330
                }
 
331
 
 
332
                //######################################################
 
333
                // difficulty: easy or hard (normal bu default)
 
334
                //######################################################
 
335
                if(!strcmp(ptArg[_iIndex], "--easy"))
 
336
                {       difficulty = 0;
 
337
                        continue;
 
338
                }
 
339
                if(!strcmp(ptArg[_iIndex], "--hard"))
 
340
                {       difficulty = 2;
 
341
                        continue;
 
342
                }
 
343
        }
 
344
        return 1;
 
345
}
 
346
 
 
347
 
 
348
//------------------------------------------------------------------------------
 
349
// return current language
 
350
//------------------------------------------------------------------------------
 
351
char* configfile::get_txtlang()
 
352
{
 
353
        return  lang2text[lang];
 
354
}