]> git.mxchange.org Git - flightgear.git/blob - Main/options.cxx
Added an command line option to set starting position based on airport ID.
[flightgear.git] / Main / options.cxx
1 //
2 // options.cxx -- class to handle command line options
3 //
4 // Written by Curtis Olson, started April 1998.
5 //
6 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23 // (Log is kept at end of this file)
24
25
26 #include <math.h>            // rint()
27 #include <stdio.h>
28 #include <stdlib.h>          // atof()
29 #include <string.h>
30
31 #include <Debug/fg_debug.h>
32
33 #include "options.hxx"
34
35
36 // Defined the shared options class here
37 fgOPTIONS current_options;
38
39
40 // Constructor
41 fgOPTIONS::fgOPTIONS( void ) {
42     // set initial values/defaults
43
44     strcpy(airport_id, "");
45     hud_status = 0;
46     time_offset = 0;
47 }
48
49
50 // parse time string in the form of [+-]hh:mm:ss, returns the value in seconds
51 static double parse_time(char *time_str) {
52     char num[256];
53     double hours, minutes, seconds;
54     double result = 0.0;
55     int sign = 1;
56     int i;
57
58     // printf("parse_time(): %s\n", time_str);
59
60     // check for sign
61     if ( strlen(time_str) ) {
62         if ( time_str[0] == '+' ) {
63             sign = 1;
64             time_str++;
65         } else if ( time_str[0] == '-' ) {
66             sign = -1;
67             time_str++;
68         }
69     }
70     // printf("sign = %d\n", sign);
71
72     // get hours
73     if ( strlen(time_str) ) {
74         i = 0;
75         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
76             num[i] = time_str[0];
77             time_str++;
78             i++;
79         }
80         if ( time_str[0] == ':' ) {
81             time_str++;
82         }
83         num[i] = '\0';
84         hours = atof(num);
85         // printf("hours = %.2lf\n", hours);
86
87         result += hours * 3600.0;
88     }
89
90     // get minutes
91     if ( strlen(time_str) ) {
92         i = 0;
93         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
94             num[i] = time_str[0];
95             time_str++;
96             i++;
97         }
98         if ( time_str[0] == ':' ) {
99             time_str++;
100         }
101         num[i] = '\0';
102         minutes = atof(num);
103         // printf("minutes = %.2lf\n", minutes);
104
105         result += minutes * 60.0;
106     }
107
108     // get seconds
109     if ( strlen(time_str) ) {
110         i = 0;
111         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
112             num[i] = time_str[0];
113             time_str++;
114             i++;
115         }
116         num[i] = '\0';
117         seconds = atof(num);
118         // printf("seconds = %.2lf\n", seconds);
119
120         result += seconds;
121     }
122
123     return(sign * result);
124 }
125
126
127 // parse time offset command line option
128 static int parse_time_offset(char *time_str) {
129     int result;
130
131     time_str += 14;
132
133     // printf("time offset = %s\n", time_str);
134
135     result = (int)rint(parse_time(time_str));
136
137     printf("parse_time_offset(): %d\n", result);
138
139     return( result );
140
141 }
142
143
144 // Parse the command line options
145 int fgOPTIONS::parse( int argc, char **argv ) {
146     int i = 1;
147
148     fgPrintf(FG_GENERAL, FG_INFO, "Processing arguments\n");
149
150     while ( i < argc ) {
151         fgPrintf(FG_GENERAL, FG_INFO, "argv[%d] = %s\n", i, argv[i]);
152
153         if ( strncmp(argv[i], "--airport-id=", 13) == 0 ) {
154             argv[i] += 13;
155             strncpy(airport_id, argv[i], 4);
156         } else if ( strcmp(argv[i], "--disable-hud") == 0 ) {
157             hud_status = 0;     
158         } else if ( strcmp(argv[i], "--enable-hud") == 0 ) {
159             hud_status = 1;     
160         } else if ( (strcmp(argv[i], "--help") == 0) ||
161              (strcmp(argv[i], "-h") == 0) ) {
162             // help/usage request
163             return(FG_OPTIONS_HELP);
164         } else if ( strncmp(argv[i], "--time-offset=", 14) == 0 ) {
165             time_offset = parse_time_offset(argv[i]);
166         } else {
167             return(FG_OPTIONS_ERROR);
168         }
169
170         i++;
171     }
172     
173     return(FG_OPTIONS_OK);
174 }
175
176
177 // Print usage message
178 void fgOPTIONS::usage ( void ) {
179     printf("Usage: fg [ options ... ]\n");
180     printf("\t--airport-id=ABCD:  specify starting postion by airport id\n");
181     printf("\t--disable-hud:  disable heads up display\n");
182     printf("\t--enable-hud:  enable heads up display\n");
183     printf("\t--help -h:  print usage\n");
184     printf("\t--time-offset=[+-]hh:mm:ss:  offset local time by this amount\n");
185 }
186
187
188 // Destructor
189 fgOPTIONS::~fgOPTIONS( void ) {
190 }
191
192
193 // $Log$
194 // Revision 1.2  1998/04/25 15:11:12  curt
195 // Added an command line option to set starting position based on airport ID.
196 //
197 // Revision 1.1  1998/04/24 00:49:21  curt
198 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
199 // Trying out some different option parsing code.
200 // Some code reorganization.
201 //
202 //