Changeset - r17344:622438b306a3
[Not reviewed]
master
0 6 0
alberth - 14 years ago 2011-02-18 20:14:30
alberth@openttd.org
(svn r22096) -Codechange: Move openttd getopt implementation to its own file.
6 files changed with 29 insertions and 69 deletions:
0 comments (0 inline, 0 general)
projects/openttd_vs100.vcxproj
Show inline comments
 
@@ -1022,6 +1022,8 @@
 
    <ClCompile Include="..\src\misc\dbg_helpers.cpp" />
 
    <ClInclude Include="..\src\misc\dbg_helpers.h" />
 
    <ClInclude Include="..\src\misc\fixedsizearray.hpp" />
 
    <ClCompile Include="..\src\misc\getoptdata.cpp" />
 
    <ClInclude Include="..\src\misc\getoptdata.h" />
 
    <ClInclude Include="..\src\misc\hashtable.hpp" />
 
    <ClInclude Include="..\src\misc\str.hpp" />
 
    <ClCompile Include="..\src\network\core\address.cpp" />
projects/openttd_vs100.vcxproj.filters
Show inline comments
 
@@ -2286,6 +2286,12 @@
 
    <ClInclude Include="..\src\misc\fixedsizearray.hpp">
 
      <Filter>Misc</Filter>
 
    </ClInclude>
 
    <ClCompile Include="..\src\misc\getoptdata.cpp">
 
      <Filter>Misc</Filter>
 
    </ClCompile>
 
    <ClInclude Include="..\src\misc\getoptdata.h">
 
      <Filter>Misc</Filter>
 
    </ClInclude>
 
    <ClInclude Include="..\src\misc\hashtable.hpp">
 
      <Filter>Misc</Filter>
 
    </ClInclude>
projects/openttd_vs80.vcproj
Show inline comments
 
@@ -3439,6 +3439,14 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\misc\getoptdata.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\misc\getoptdata.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\misc\hashtable.hpp"
 
				>
 
			</File>
projects/openttd_vs90.vcproj
Show inline comments
 
@@ -3436,6 +3436,14 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\misc\getoptdata.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\misc\getoptdata.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\misc\hashtable.hpp"
 
				>
 
			</File>
source.list
Show inline comments
 
@@ -815,6 +815,8 @@ misc/countedptr.hpp
 
misc/dbg_helpers.cpp
 
misc/dbg_helpers.h
 
misc/fixedsizearray.hpp
 
misc/getoptdata.cpp
 
misc/getoptdata.h
 
misc/hashtable.hpp
 
misc/str.hpp
 

	
src/openttd.cpp
Show inline comments
 
@@ -59,6 +59,7 @@
 
#include "core/backup_type.hpp"
 
#include "hotkeys.h"
 
#include "newgrf.h"
 
#include "misc/getoptdata.h"
 

	
 

	
 
#include "town.h"
 
@@ -215,73 +216,6 @@ static void ShowHelp()
 
}
 

	
 

	
 
struct MyGetOptData {
 
	char *opt;
 
	int numleft;
 
	char **argv;
 
	const char *options;
 
	char *cont;
 

	
 
	MyGetOptData(int argc, char **argv, const char *options)
 
	{
 
		opt = NULL;
 
		numleft = argc;
 
		this->argv = argv;
 
		this->options = options;
 
		cont = NULL;
 
	}
 
};
 

	
 
static int MyGetOpt(MyGetOptData *md)
 
{
 
	char *s = md->cont;
 
	if (s != NULL) {
 
		goto md_continue_here;
 
	}
 

	
 
	for (;;) {
 
		if (--md->numleft < 0) return -1;
 

	
 
		s = *md->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(md->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 (--md->numleft < 0 || *(t = *md->argv) == '-') {
 
							/* Check if item is optional? */
 
							if (r[2] != ':') return -2;
 
							md->numleft++;
 
							t = NULL;
 
						} else {
 
							md->argv++;
 
						}
 
					}
 
					md->opt = t;
 
					md->cont = NULL;
 
					return *s;
 
				}
 
				md->opt = NULL;
 
				md->cont = s;
 
				return *s;
 
			}
 
		} else {
 
			/* This is currently not supported. */
 
			return -2;
 
		}
 
	}
 
}
 

	
 
/**
 
 * Extract the resolution from the given string and store
 
 * it in the 'res' parameter.
 
@@ -452,9 +386,9 @@ int ttd_main(int argc, char *argv[])
 
#endif
 
	;
 

	
 
	MyGetOptData mgo(argc - 1, argv + 1, optformat);
 
	GetOptData mgo(argc - 1, argv + 1, optformat);
 

	
 
	while ((i = MyGetOpt(&mgo)) != -1) {
 
	while ((i = mgo.GetOpt()) != -1) {
 
		switch (i) {
 
		case 'I': free(graphics_set); graphics_set = strdup(mgo.opt); break;
 
		case 'S': free(sounds_set); sounds_set = strdup(mgo.opt); break;
0 comments (0 inline, 0 general)