~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/options.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-2004 by the Widelands Development Team
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
// 2002-02-10   sft+    added config-existing check in handle_options
 
21
 
 
22
#include "constants.h"
 
23
#include "error.h"
 
24
#include "options.h"
 
25
#include "system.h"
 
26
#include "network.h"
 
27
 
 
28
Profile g_options(Profile::err_log);
 
29
 
 
30
 
 
31
/** options_shutdown()
 
32
 *
 
33
 * This overrites the old config file with a new one containing
 
34
 * the current options
 
35
 */
 
36
void options_shutdown()
 
37
{
 
38
        // write only used configuration entries to the file to sieve out typos
 
39
        // on the commands line etc..
 
40
        g_options.write("config", true);
 
41
}
 
42
 
 
43
 
 
44
/** show_usage()
 
45
 *
 
46
 * This functions finally prints the usage and ends the programm
 
47
 */
 
48
static void show_usage(void)
 
49
{
 
50
        log(
 
51
"Usage: widelands <option0>=<value0> ... <optionN>=<valueN>\n"
 
52
"Options:\n"
 
53
"\n"
 
54
" --<config-entry-name>=value overwrites a config file setting\n"
 
55
"\n"
 
56
" --record         Record all events to the given filename for later playback\n"
 
57
" --playback       Playback given filename (see --record)\n"
 
58
"\n"
 
59
" --coredump       Generates a core dump on segfaults instead of using the SDL\n"
 
60
"\n"
 
61
" --ggz            Starts game as GGZ Gaming Zone client (don't use!)\n"
 
62
"\n"
 
63
" --help           Show this help\n"
 
64
" --version        Show version\n"
 
65
"\n"
 
66
"Bug reports? Suggestions? Check out the project website:\n"
 
67
"  http://www.sourceforge.net/projects/widelands\n"
 
68
"Hope you enjoy this game!\n");
 
69
}
 
70
 
 
71
/** show_version()
 
72
 *
 
73
 * Print version information
 
74
 */
 
75
static void show_version(void)
 
76
{
 
77
        log(VERSION);
 
78
}
 
79
 
 
80
/** parse_command_line(int argc, char** argv)
 
81
 *
 
82
 * Parses the standard cmd line of the program
 
83
 *
 
84
 * Returns: true if execution may continue
 
85
 */
 
86
static bool parse_command_line(int argc, char** argv)
 
87
{
 
88
        for(int i = 1; i < argc; i++) {
 
89
                char *opt = argv[i];
 
90
                char *value;
 
91
 
 
92
                if (strncmp(opt, "--", 2)) {
 
93
                        show_usage();
 
94
                        return false;
 
95
                }
 
96
                opt += 2;
 
97
 
 
98
                if (!strcmp(opt, "help")) {
 
99
                        show_usage();
 
100
                        return false;
 
101
                }
 
102
                if (!strcmp(opt, "version")) {
 
103
                        show_version();
 
104
                        return false;
 
105
                }
 
106
                if (!strcmp(opt, "ggz")) {
 
107
                        NetGGZ::ref()->init();
 
108
                        continue;
 
109
                }
 
110
 
 
111
                value = strchr(opt, '=');
 
112
                if (!value) {
 
113
                        show_usage();
 
114
                        return false;
 
115
                }
 
116
                *value++ = 0;
 
117
 
 
118
                if (!strcmp(opt, "record")) {
 
119
                        Sys_SetRecordFile(value);
 
120
                        continue;
 
121
                }
 
122
                if (!strcmp(opt, "playback")) {
 
123
                        Sys_SetPlaybackFile(value);
 
124
                        continue;
 
125
                }
 
126
 
 
127
                g_options.pull_section("global")->create_val(opt, value);
 
128
        }
 
129
 
 
130
        return true;
 
131
}
 
132
 
 
133
/** options_init(int argc, char** argv)
 
134
 *
 
135
 * This function parses the config file and the cmdline.
 
136
 */
 
137
void options_init(int argc, char **argv)
 
138
{
 
139
        g_options.read("config", "global");
 
140
 
 
141
        if (!parse_command_line(argc, argv))
 
142
                exit(0);
 
143
}