-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathParseArgs.java
More file actions
executable file
·173 lines (158 loc) · 4.32 KB
/
Copy pathParseArgs.java
File metadata and controls
executable file
·173 lines (158 loc) · 4.32 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Arguments Parser
v1.0.0 - based on dfparse.hpp/1.2.8 from DF
Usage :
arg_parse(String args[]);
then the following will be filled out:
Vector<String> arg_opts; //Options (ie: /XXX[=yyy])
Vector<String> arg_vals; //Options set value (ie: /xxx=YYY) ["" if none]
Vector<String> arg_names; //filenames/commands (ie: XXX)
you can also parse files:
arg_parse_file(InputStream);
*/
package javaforce;
import java.io.*;
import java.util.*;
/**
* Parsing command line options.
*
* @author Peter Quiring
*/
public class ParseArgs extends JF {
public ArrayList<String> arg_opts;
public ArrayList<String> arg_vals;
public ArrayList<String> arg_names;
public boolean arg_decoderefs = true; //set to false to ignore @reffiles
public boolean arg_stripenter = true; //set to false to keep ENTER codes in arguments (see \examples\winmsg)
public boolean arg_decodeopts = true; //set to false to use arg_names only
public ParseArgs() {
arg_opts = new ArrayList<String>();
arg_vals = new ArrayList<String>();
arg_names = new ArrayList<String>();
}
public void arg_parse(String[] args) {
for (int a = 0; a < args.length; a++) {
arg_parse_element(args[a].toCharArray());
}
}
public void arg_parse_file(InputStream is) {
char[] str;
while (!eof(is)) {
str = arg_readln(is); //read one line (removes leading/trailing spaces)
if ((str != null) && (str.length > 0)) {
arg_parse_string(str);
}
}
}
//private members
private void arg_parse_element(char[] str) {
FileInputStream fis;
char[] tmp;
if (str.length == 0) {
arg_names.add(createString(str));
return;
}
if ((str[0] == '-') && (arg_decodeopts)) {
int p = 0;
for (int a = 0; a < str.length; a++) {
if (str[a] == ':') {
str[a] = '=';
}
}
//option (arg_opts)
p++;
tmp = null;
while ((p < str.length) && (str[p] != '=')) {
tmp = strcat(tmp, str[p++]);
}
if (tmp == null) {
return; //bad arg
}
if ((p < str.length) && (str[p] == '=')) {
//do equals thingy
arg_opts.add(createString(tmp));
p++;
tmp = null;
while (p < str.length) {
tmp = strcat(tmp, str[p++]);
}
if (tmp == null) {
arg_vals.add("");
} else {
arg_vals.add(createString(tmp));
}
} else {
//check if /xxx[- or +] option
switch (tmp[tmp.length - 1]) {
case '-':
tmp = truncstr(tmp, tmp.length - 1);
arg_opts.add(createString(tmp));
arg_vals.add("F");
break;
case '+':
tmp = truncstr(tmp, tmp.length - 1);
arg_opts.add(createString(tmp));
arg_vals.add("T");
break;
default:
arg_opts.add(createString(tmp));
arg_vals.add("");
break;
}
}
} else if ((str[0] == '@') && (arg_decoderefs)) {
//reference file
tmp = new char[str.length - 1];
for (int a = 1; a < str.length; a++) {
tmp[a - 1] = str[a];
}
fis = fileopen(createString(tmp));
if (fis == null) {
return; //can't open file
}
arg_parse_file(fis);
} else {
arg_names.add(createString(str));
}
}
private void arg_parse_string(char[] str) {
boolean quote = false;
int p = 0;
char[] tmp;
while (p < str.length) {
tmp = null;
while (p < str.length) {
if (str[p] == '\"') {
if (quote) {
quote = false;
} else {
quote = true;
}
}
if ((!quote) && (str[p] == ' ')) {
p++;
break;
}
tmp = strcat(tmp, str[p++]);
}
arg_parse_element(tmp);
}
}
private char[] arg_readln(InputStream fis) {
char[] ret = null;
char ch;
while (!eof(fis)) {
ch = readchar(fis);
if (arg_stripenter) {
if (ch == 13) {
continue;
}
if (ch == 10) {
break; //End of line (and option) (strip \n)
}
}
ret = strcat(ret, ch);
}
return ret;
}
}