2 // options.cxx -- class to handle command line options
4 // Written by Curtis Olson, started April 1998.
6 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 // (Log is kept at end of this file)
30 #include <math.h> // rint()
32 #include <stdlib.h> // atof(), atoi()
35 #include <Debug/fg_debug.h>
36 #include <Include/fg_constants.h>
37 #include <Include/fg_zlib.h>
39 #include "options.hxx"
42 // Defined the shared options class here
43 fgOPTIONS current_options;
47 fgOPTIONS::fgOPTIONS( void ) {
48 // set initial values/defaults
50 if ( getenv("FG_ROOT") != NULL ) {
51 // fg_root could be anywhere, so default to environmental
52 // variable $FG_ROOT if it is set.
54 strcpy(fg_root, getenv("FG_ROOT"));
56 // Otherwise, default to a random compiled in location if
57 // $FG_ROOT is not set. This can still be overridden from the
58 // command line or a config file.
61 strcpy(fg_root, "\\FlightGear");
63 strcpy(fg_root, "/usr/local/lib/FlightGear");
68 strcpy(airport_id, "");
90 // Parse an int out of a --foo-bar=n type option
91 static int parse_int(char *arg) {
94 // advance past the '='
95 while ( (arg[0] != '=') && (arg[0] != '\0') ) {
99 if ( arg[0] == '=' ) {
103 printf("parse_int(): arg = %s\n", arg);
107 printf("parse_int(): result = %d\n", result);
113 // Parse an int out of a --foo-bar=n type option
114 static double parse_double(char *arg) {
117 // advance past the '='
118 while ( (arg[0] != '=') && (arg[0] != '\0') ) {
122 if ( arg[0] == '=' ) {
126 printf("parse_double(): arg = %s\n", arg);
130 printf("parse_double(): result = %.4f\n", result);
136 // parse time string in the form of [+-]hh:mm:ss, returns the value in seconds
137 static double parse_time(char *time_str) {
139 double hours, minutes, seconds;
144 // printf("parse_time(): %s\n", time_str);
147 if ( strlen(time_str) ) {
148 if ( time_str[0] == '+' ) {
151 } else if ( time_str[0] == '-' ) {
156 // printf("sign = %d\n", sign);
159 if ( strlen(time_str) ) {
161 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
162 num[i] = time_str[0];
166 if ( time_str[0] == ':' ) {
171 // printf("hours = %.2lf\n", hours);
173 result += hours * 3600.0;
177 if ( strlen(time_str) ) {
179 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
180 num[i] = time_str[0];
184 if ( time_str[0] == ':' ) {
189 // printf("minutes = %.2lf\n", minutes);
191 result += minutes * 60.0;
195 if ( strlen(time_str) ) {
197 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
198 num[i] = time_str[0];
204 // printf("seconds = %.2lf\n", seconds);
209 return(sign * result);
213 // parse time offset command line option
214 static int parse_time_offset(char *time_str) {
219 // printf("time offset = %s\n", time_str);
222 result = (int)rint(parse_time(time_str));
224 result = (int)parse_time(time_str);
227 printf("parse_time_offset(): %d\n", result);
233 // Parse --tile-diameter=n type option
235 #define FG_RADIUS_MIN 1
236 #define FG_RADIUS_MAX 4
238 static int parse_tile_radius(char *arg) {
241 radius = parse_int(arg);
243 if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
244 if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
246 printf("parse_tile_radius(): radius = %d\n", radius);
252 // Parse --fov=x.xx type option
253 static double parse_fov(char *arg) {
256 fov = parse_double(arg);
258 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
259 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
261 printf("parse_fov(): result = %.4f\n", fov);
267 // Parse a single option
268 int fgOPTIONS::parse_option( char *arg ) {
270 if ( (strcmp(arg, "--help") == 0) ||
271 (strcmp(arg, "-h") == 0) ) {
272 // help/usage request
273 return(FG_OPTIONS_HELP);
274 } else if ( strcmp(arg, "--disable-hud") == 0 ) {
276 } else if ( strcmp(arg, "--enable-hud") == 0 ) {
278 } else if ( strncmp(arg, "--airport-id=", 13) == 0 ) {
280 strncpy(airport_id, arg, 4);
281 } else if ( strncmp(arg, "--fg-root=", 10) == 0 ) {
283 strcpy(fg_root, arg);
284 } else if ( strcmp(arg, "--fog-disable") == 0 ) {
286 } else if ( strcmp(arg, "--fog-fastest") == 0 ) {
288 } else if ( strcmp(arg, "--fog-nicest") == 0 ) {
290 } else if ( strncmp(arg, "--fov=", 6) == 0 ) {
291 fov = parse_fov(arg);
292 } else if ( strcmp(arg, "--disable-fullscreen") == 0 ) {
294 } else if ( strcmp(arg, "--enable-fullscreen") == 0 ) {
296 } else if ( strcmp(arg, "--shading-flat") == 0 ) {
298 } else if ( strcmp(arg, "--shading-smooth") == 0 ) {
300 } else if ( strcmp(arg, "--disable-skyblend") == 0 ) {
302 } else if ( strcmp(arg, "--enable-skyblend") == 0 ) {
304 } else if ( strcmp(arg, "--disable-textures") == 0 ) {
306 } else if ( strcmp(arg, "--enable-textures") == 0 ) {
308 } else if ( strcmp(arg, "--disable-wireframe") == 0 ) {
310 } else if ( strcmp(arg, "--enable-wireframe") == 0 ) {
312 } else if ( strncmp(arg, "--tile-radius=", 14) == 0 ) {
313 tile_radius = parse_tile_radius(arg);
314 tile_diameter = tile_radius * 2 + 1;
315 } else if ( strncmp(arg, "--time-offset=", 14) == 0 ) {
316 time_offset = parse_time_offset(arg);
318 return(FG_OPTIONS_ERROR);
321 return(FG_OPTIONS_OK);
325 // Parse the command line options
326 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
330 fgPrintf(FG_GENERAL, FG_INFO, "Processing command line arguments\n");
333 fgPrintf(FG_GENERAL, FG_INFO, "argv[%d] = %s\n", i, argv[i]);
335 result = parse_option(argv[i]);
336 if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
343 return(FG_OPTIONS_OK);
347 // Parse the command line options
348 int fgOPTIONS::parse_config_file( char *path ) {
349 char fgpath[256], line[256];
353 strcpy(fgpath, path);
354 strcat(fgpath, ".gz");
356 // first try "path.gz"
357 if ( (f = fgopen(fgpath, "rb")) == NULL ) {
359 if ( (f = fgopen(path, "rb")) == NULL ) {
360 return(FG_OPTIONS_ERROR);
364 fgPrintf(FG_GENERAL, FG_INFO, "Processing config file: %s\n", path);
366 while ( fggets(f, line, 250) != NULL ) {
367 // strip trailing newline if it exists
369 if ( line[len-1] == '\n' ) {
373 result = parse_option(line);
374 if ( result == FG_OPTIONS_ERROR ) {
375 fgPrintf( FG_GENERAL, FG_EXIT,
376 "Config file parse error: %s '%s'\n", path, line );
381 return(FG_OPTIONS_OK);
385 // Print usage message
386 void fgOPTIONS::usage ( void ) {
387 printf("Usage: fg [ options ... ]\n");
390 printf("General Options:\n");
391 printf("\t--help -h: print usage\n");
392 printf("\t--fg-root=path: specify the root path for all the data files\n");
395 printf("Features:\n");
396 printf("\t--disable-hud: disable heads up display\n");
397 printf("\t--enable-hud: enable heads up display\n");
400 printf("Initial Position:\n");
401 printf("\t--airport-id=ABCD: specify starting postion by airport id\n");
404 printf("Rendering Options:\n");
405 printf("\t--fog-disable: disable fog/haze\n");
406 printf("\t--fog-fastest: enable fastest fog/haze\n");
407 printf("\t--fog-nicest: enable nicest fog/haze\n");
408 printf("\t--fov=xx.x: specify initial field of view angle in degrees\n");
409 printf("\t--disable-fullscreen: disable fullscreen mode\n");
410 printf("\t--enable-fullscreen: enable fullscreen mode\n");
411 printf("\t--shading-flat: enable flat shading\n");
412 printf("\t--shading-smooth: enable smooth shading\n");
413 printf("\t--disable-skyblend: disable sky blending\n");
414 printf("\t--enable-skyblend: enable sky blending\n");
415 printf("\t--disable-textures: disable textures\n");
416 printf("\t--enable-textures: enable textures\n");
417 printf("\t--disable-wireframe: disable wireframe drawing mode\n");
418 printf("\t--enable-wireframe: enable wireframe drawing mode\n");
421 printf("Scenery Options:\n");
422 printf("\t--tile-radius=n: specify tile radius, must be 1 - 4\n");
425 printf("Time Options:\n");
426 printf("\t--time-offset=[+-]hh:mm:ss: offset local time by this amount\n");
431 fgOPTIONS::~fgOPTIONS( void ) {
436 // Revision 1.12 1998/06/17 21:35:13 curt
437 // Refined conditional audio support compilation.
438 // Moved texture parameter setup calls to ../Scenery/materials.cxx
439 // #include <string.h> before various STL includes.
440 // Make HUD default state be enabled.
442 // Revision 1.11 1998/06/13 00:40:33 curt
443 // Tweaked fog command line options.
445 // Revision 1.10 1998/05/16 13:08:36 curt
446 // C++ - ified views.[ch]xx
447 // Shuffled some additional view parameters into the fgVIEW class.
448 // Changed tile-radius to tile-diameter because it is a much better
450 // Added a WORLD_TO_EYE transformation to views.cxx. This allows us
451 // to transform world space to eye space for view frustum culling.
453 // Revision 1.9 1998/05/13 18:29:59 curt
454 // Added a keyboard binding to dynamically adjust field of view.
455 // Added a command line option to specify fov.
456 // Adjusted terrain color.
457 // Root path info moved to fgOPTIONS.
458 // Added ability to parse options out of a config file.
460 // Revision 1.8 1998/05/07 23:14:16 curt
461 // Added "D" key binding to set autopilot heading.
462 // Made frame rate calculation average out over last 10 frames.
463 // Borland C++ floating point exception workaround.
464 // Added a --tile-radius=n option.
466 // Revision 1.7 1998/05/06 03:16:25 curt
467 // Added an averaged global frame rate counter.
468 // Added an option to control tile radius.
470 // Revision 1.6 1998/05/03 00:47:32 curt
471 // Added an option to enable/disable full-screen mode.
473 // Revision 1.5 1998/04/30 12:34:19 curt
474 // Added command line rendering options:
475 // enable/disable fog/haze
476 // specify smooth/flat shading
477 // disable sky blending and just use a solid color
478 // enable wireframe drawing mode
480 // Revision 1.4 1998/04/28 01:20:22 curt
481 // Type-ified fgTIME and fgVIEW.
482 // Added a command line option to disable textures.
484 // Revision 1.3 1998/04/26 05:01:19 curt
485 // Added an rint() / HAVE_RINT check.
487 // Revision 1.2 1998/04/25 15:11:12 curt
488 // Added an command line option to set starting position based on airport ID.
490 // Revision 1.1 1998/04/24 00:49:21 curt
491 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
492 // Trying out some different option parsing code.
493 // Some code reorganization.