summaryrefslogtreecommitdiff
path: root/g_src/command_line.cpp
blob: 422f88fc6566cf57d1b8120d533bef7a46c82cca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "platform.h"
#include <string.h>
#include <math.h>
#include <iosfwd>
#include <iostream>
#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <zlib.h>

#include "svector.h"
using std::string;

#include "endian.h"

#include "files.h"

#include "enabler.h"

#include "textlines.h"

#include "basics.h"

#include "command_line.h"

void command_linest::init(const string &str)
{
	original=str;

	//BUILD THE TOKEN LIST
	long pos=0;
	while(grab_arg(original,pos));

	//HANDLE EACH TOKEN
	long l;
	for(l=0;l<arg_vect.str.size();l++)
		{
		handle_arg(arg_vect.str[l]->dat);
		}
}

char command_linest::grab_arg(string &source,long &pos)
{
	string dest;

	while(pos<source.length())
		{
		//HIT A NEW ARGUMENT?  RETURN, OTHERWISE SKIP AND START UP
		if(source[pos]=='-')
			{
			if(dest.empty()){pos++;continue;}
			else
				{
				pos++;
				arg_vect.add_string(dest);
				return 1;
				}
			}

		dest+=source[pos];

		pos++;
		}

	if(!dest.empty())arg_vect.add_string(dest);
	return 0;
}

void command_linest::handle_arg(string &arg)
{
	long pos=0;
	string dest;

	grab_token_string_pos(dest,arg,pos,' ');
	pos+=dest.length();

	short arg_pos=0;
	if(dest=="gen")
		{
		//KEEP GOING FOR A NUMBER
		while(pos+1<arg.length())
			{
			dest.erase();
			pos++;
			auto s=arg.begin(),e=arg.end();
			s+=pos;
			bool quote=false;
			for(;s<e;++s)
				{
				if((*s)=='"')
					{
					if(quote)break;
					else quote=true;
					++pos;
					continue;
					}
				else if((*s)==' '&&!quote)break;
				dest+=(*s);
				}
			pos+=dest.length();


			if(!dest.empty())
				{
				if(arg_pos==0)gen_id=convert_string_to_long(dest);
				if(arg_pos==1)
					{
					if(dest!="RANDOM")
						{
						world_seed=convert_string_to_ulong(dest);
						use_seed=1;
						}
					}
				if(arg_pos==2)
					{
					if(dest!="NONE")
						{
						world_param=dest;
						use_param=1;
						}
					}

				arg_pos++;
				}
			}
		}
}