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
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
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"
27
//..............................................................................
28
char* configfile::lang2text[XXLANGUAGE] = {"en", "fr"};
30
//------------------------------------------------------------------------------
32
//------------------------------------------------------------------------------
33
configfile::configfile()
39
//------------------------------------------------------------------------------
41
//------------------------------------------------------------------------------
42
configfile::~configfile()
46
//------------------------------------------------------------------------------
48
//------------------------------------------------------------------------------
49
void configfile::resetvalue()
59
//------------------------------------------------------------------------------
61
//------------------------------------------------------------------------------
62
void configfile::configinfo()
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,
71
//------------------------------------------------------------------------------
72
// check if config directory exists; if not create it and set config_dir
73
//------------------------------------------------------------------------------
74
Sint32 configfile::tocheckdir()
79
_snprintf(config_dir, sizeof(config_dir) - 1, "./%s", CONFIG_DIR_NAME );
81
/* create directory if not exist */
82
MKDIR(config_dir, S_IRWXU);
83
fprintf(stderr, "ok\n");
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");
99
fprintf(stderr, "ok\n");
107
//------------------------------------------------------------------------------
108
// load config file "~/.tlkgames/powermanga.conf"
109
//------------------------------------------------------------------------------
110
void configfile::loadconfig()
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);
125
if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
126
{ fprintf(stderr, "configfile::loadconfig() / conf parsing failed\n");
130
if (strcmp(lisp_symbol(lisp_car(root_obj)), "powermanga-config") != 0)
131
{ fprintf(stderr, "configfile::loadconfig() / conf parsing failed\n");
134
LispReader reader(lisp_cdr(root_obj));
136
if (!reader.read_string ("lang", &ptStr))
144
if (!reader.read_bool("fullscreen", &fullscreen))
147
if (!reader.read_bool("nosound", &nosound))
149
if (!reader.read_bool("nosync", &nosync))
151
if (!reader.read_int ("verbose", &verbose))
155
if (!reader.read_int ("resolution", &resolution))
157
if (resolution != 320 && resolution != 640)
162
//------------------------------------------------------------------------------
163
// save config file "~/.tlkgames/powermanga.conf"
164
//------------------------------------------------------------------------------
165
void configfile::saveconfig()
167
FILE * config = fopen_data(configname, "w");
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");
176
fprintf(config, "\n\t;; window size (320 or 640):\n");
177
fprintf(config, "\t(resolution %d)\n", resolution);
179
fprintf(config, "\n\t;; verbose mode 0 (disabled), 1 (enable) or 2 (more messages)\n");
180
fprintf(config, "\t(verbose %d)\n", verbose);
182
fprintf(config, "\n\t;; difficulty 0 (easy), 1 (normal) or 2 (hard)\n");
183
fprintf(config, "\t(difficulty %d)\n", difficulty);
185
fprintf(config, "\n\t;; langage en or fr\n");
186
fprintf(config, "\t(lang ");
189
fprintf(config, "\"fr\")\n");
192
fprintf(config, "\"en\")\n");
195
fprintf(config, ")\n");
202
//------------------------------------------------------------------------------
204
//------------------------------------------------------------------------------
205
FILE * configfile::fopen_data(const char *fname, const char *fmode)
208
fi = fopen(fname, fmode);
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");
221
//------------------------------------------------------------------------------
222
// analyse command line paramaters
223
//------------------------------------------------------------------------------
224
Sint32 configfile::scanZeArgs(Sint32 nbArg, char **ptArg)
226
for(Sint32 _iIndex = 1; _iIndex < nbArg; _iIndex++)
228
if(*ptArg[_iIndex] != '-')
231
//######################################################
233
//######################################################
234
if(!strcmp(ptArg[_iIndex], "-h") ||
235
!strcmp(ptArg[_iIndex], "--help"))
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");
243
"--window windowed mode (full screen by default) \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");
261
"F switch between full screen and windowed mode\n");
268
if(!strcmp(ptArg[_iIndex], "--version"))
269
{ printf(POWERMANGA_VERSION);
271
printf("copyright (c) 1998-2006 TLK Games\n");
272
printf("website: http://linux.tlk.fr/\n");
276
//######################################################
278
//######################################################
279
if(!strcmp(ptArg[_iIndex], "--window"))
280
{ fullscreen = false;
284
//######################################################
285
//force fullscreen mode
286
//######################################################
287
if(!strcmp(ptArg[_iIndex], "--fullscreen"))
292
//######################################################
293
// resolution, low-res or high-res
294
//######################################################
295
if(!strcmp(ptArg[_iIndex], "--320"))
299
if(!strcmp(ptArg[_iIndex], "--640"))
304
//######################################################
305
// enable verbose mode
306
//######################################################
307
if(!strcmp(ptArg[_iIndex], "-v"))
311
if(!strcmp(ptArg[_iIndex], "--verbose"))
316
//######################################################
318
//######################################################
319
if(!strcmp(ptArg[_iIndex], "--nosound"))
324
//######################################################
326
//######################################################
327
if(!strcmp(ptArg[_iIndex], "--nosync"))
332
//######################################################
333
// difficulty: easy or hard (normal bu default)
334
//######################################################
335
if(!strcmp(ptArg[_iIndex], "--easy"))
339
if(!strcmp(ptArg[_iIndex], "--hard"))
348
//------------------------------------------------------------------------------
349
// return current language
350
//------------------------------------------------------------------------------
351
char* configfile::get_txtlang()
353
return lang2text[lang];