(svn r22097) -Fix (r22096): Add files before commit.

This commit is contained in:
alberth 2011-02-18 20:23:11 +00:00
parent 9d0ff9d692
commit b0b1c0dea9
2 changed files with 112 additions and 0 deletions

71
src/misc/getoptdata.cpp Normal file
View File

@ -0,0 +1,71 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file getoptdata.cpp Library for parsing command line options. */
#include "../stdafx.h"
#include "getoptdata.h"
/**
* Find the next option.
* @return Function returns one
* - An option letter if it found another option.
* - -1 if option processing is finished.
* - -2 if an error was encountered.
*/
int GetOptData::GetOpt()
{
char *s = this->cont;
if (s != NULL) {
goto md_continue_here;
}
for (;;) {
if (--this->numleft < 0) return -1;
s = *this->argv++;
if (*s == '-') {
md_continue_here:;
s++;
if (*s != 0) {
const char *r;
/* Found argument, try to locate it in options. */
if (*s == ':' || (r = strchr(this->options, *s)) == NULL) {
/* ERROR! */
return -2;
}
if (r[1] == ':') {
char *t;
/* Item wants an argument. Check if the argument follows, or if it comes as a separate arg. */
if (!*(t = s + 1)) {
/* It comes as a separate arg. Check if out of args? */
if (--this->numleft < 0 || *(t = *this->argv) == '-') {
/* Check if item is optional? */
if (r[2] != ':') return -2;
this->numleft++;
t = NULL;
} else {
this->argv++;
}
}
this->opt = t;
this->cont = NULL;
return *s;
}
this->opt = NULL;
this->cont = s;
return *s;
}
} else {
/* This is currently not supported. */
return -2;
}
}
}

41
src/misc/getoptdata.h Normal file
View File

@ -0,0 +1,41 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file getoptdata.h Library for parsing command-line options. */
#ifndef GETOPTDATA_H
#define GETOPTDATA_H
/** Data storage for parsing command line options. */
struct GetOptData {
char *opt; ///< Option value, if available (else \c NULL).
int numleft; ///< Number of arguments left in #argv.
char **argv; ///< Remaining command line arguments.
const char *options; ///< Command line option descriptions.
char *cont; ///< Next call to #MyGetOpt should start here (in the middle of an argument).
/**
* Constructor of the data store.
* @param argc Number of command line arguments, excluding the program name.
* @param argv Command line arguments, excluding the program name.
* @param options Command line option descriptions.
*/
GetOptData(int argc, char **argv, const char *options) :
opt(NULL),
numleft(argc),
argv(argv),
options(options),
cont(NULL)
{
}
int GetOpt();
};
#endif /* GETOPTDATA_H */