OpenTTD Source  1.11.0-beta2
getoptdata.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "getoptdata.h"
12 
13 #include "../safeguards.h"
14 
23 {
24  const OptionData *odata;
25 
26  char *s = this->cont;
27  if (s == nullptr) {
28  if (this->numleft == 0) return -1; // No arguments left -> finished.
29 
30  s = this->argv[0];
31  if (*s != '-') return -1; // No leading '-' -> not an option -> finished.
32 
33  this->argv++;
34  this->numleft--;
35 
36  /* Is it a long option? */
37  for (odata = this->options; odata->flags != ODF_END; odata++) {
38  if (odata->longname != nullptr && !strcmp(odata->longname, s)) { // Long options always use the entire argument.
39  this->cont = nullptr;
40  goto set_optval;
41  }
42  }
43 
44  s++; // Skip leading '-'.
45  }
46 
47  /* Is it a short option? */
48  for (odata = this->options; odata->flags != ODF_END; odata++) {
49  if (odata->shortname != '\0' && *s == odata->shortname) {
50  this->cont = (s[1] != '\0') ? s + 1 : nullptr;
51 
52 set_optval: // Handle option value of *odata .
53  this->opt = nullptr;
54  switch (odata->flags) {
55  case ODF_NO_VALUE:
56  return odata->id;
57 
58  case ODF_HAS_VALUE:
59  case ODF_OPTIONAL_VALUE:
60  if (this->cont != nullptr) { // Remainder of the argument is the option value.
61  this->opt = this->cont;
62  this->cont = nullptr;
63  return odata->id;
64  }
65  /* No more arguments, either return an error or a value-less option. */
66  if (this->numleft == 0) return (odata->flags == ODF_HAS_VALUE) ? -2 : odata->id;
67 
68  /* Next argument looks like another option, let's not return it as option value. */
69  if (odata->flags == ODF_OPTIONAL_VALUE && this->argv[0][0] == '-') return odata->id;
70 
71  this->opt = this->argv[0]; // Next argument is the option value.
72  this->argv++;
73  this->numleft--;
74  return odata->id;
75 
76  default: NOT_REACHED();
77  }
78  }
79  }
80 
81  return -2; // No other ways to interpret the text -> error.
82 }
83 
ODF_END
@ ODF_END
Terminator (data is not parsed further).
Definition: getoptdata.h:18
ODF_OPTIONAL_VALUE
@ ODF_OPTIONAL_VALUE
An option with an optional value.
Definition: getoptdata.h:17
OptionData::longname
const char * longname
Long option name including '-'/'–' prefix, use nullptr if not available.
Definition: getoptdata.h:26
GetOptData::options
const OptionData * options
Command line option descriptions.
Definition: getoptdata.h:34
GetOptData::argv
char ** argv
Remaining command line arguments.
Definition: getoptdata.h:33
OptionData::flags
uint16 flags
Option data flags.
Definition: getoptdata.h:25
GetOptData::opt
char * opt
Option value, if available (else nullptr).
Definition: getoptdata.h:31
OptionData
Data of an option.
Definition: getoptdata.h:22
OptionData::id
byte id
Unique identification of this option data, often the same as shortname.
Definition: getoptdata.h:23
OptionData::shortname
char shortname
Short option letter if available, else use '\0'.
Definition: getoptdata.h:24
GetOptData::cont
char * cont
Next call to GetOpt should start here (in the middle of an argument).
Definition: getoptdata.h:35
GetOptData::GetOpt
int GetOpt()
Find the next option.
Definition: getoptdata.cpp:22
ODF_HAS_VALUE
@ ODF_HAS_VALUE
An option with a value.
Definition: getoptdata.h:16
getoptdata.h
ODF_NO_VALUE
@ ODF_NO_VALUE
A plain option (no value attached to it).
Definition: getoptdata.h:15
GetOptData::numleft
int numleft
Number of arguments left in argv.
Definition: getoptdata.h:32