-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetLongOpt.cpp
More file actions
210 lines (185 loc) · 3.57 KB
/
Copy pathGetLongOpt.cpp
File metadata and controls
210 lines (185 loc) · 3.57 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "GetLongOpt.h"
#include <string>
#include <fstream>
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <cstring>
using namespace std;
GetLongOpt::GetLongOpt(int input_argc, char** input_argv)
{
argc = input_argc;
argv = input_argv;
}
int GetLongOpt::CheckOpt(string option)
{
int i;
string temp;
option.insert(0, "-");
for(i = 1; i < argc; i++)
{
temp = argv[i];
if(temp.compare(option) == 0)
{
return i;
}
}
return -1;
}
int GetLongOpt::ValidOptType(string option, int index)
{
int i, length;
if(index < 1)
{
return index;
}
length = static_cast<int>(strlen(argv[index+1]));
i=0;
if(((argv[index+1][0] == '-') || (argv[index+1][0] == '+')) || (i == 0))
{
i = 1;
}
else
{
i = 0;
}
for(; i < length; i++)
{
if(isdigit(argv[index+1][i]) == 0)
{
if(argv[index+1][i] != '.')
{
cout << "has invalid syntex in argument: " << option << endl;
abort();
return -1;
}
}
}
return index;
}
/*///////////////////////////////////////////////////////////
//
//
///////////////////////////////////////////////////////////*/
int GetLongOpt::GetIntOpt(string option, int default_value)
{
int i = GetLongOpt::CheckOpt(option);
i = ValidOptType(option, i);
if(i > 0)
{
return atoi(argv[i+1]);
}
else
{
return default_value;
}
}
double GetLongOpt::GetFloatOpt(string option, double default_value)
{
int i = GetLongOpt::CheckOpt(option);
i = ValidOptType(option, i);
if(i > 0)
{
return atof(argv[i+1]);
}
else
{
return default_value;
}
}
string GetLongOpt::GetStringOpt(string option, string default_string)
{
int i = GetLongOpt::CheckOpt(option);
if(i > 0)
{
return argv[i+1];
}
else
{
return default_string;
}
}
string GetLongOpt::GetSpecStringOpt(int index)
{
string temp;
if((index >= argc)||(index < 0))
temp = argv[argc-1];
else
temp = argv[index];
return temp;
}
string GetLongOpt::GetLastStringOpt()
{
string temp;
temp = argv[argc-1];
return temp;
}
int GetLongOpt::GetCommandOpt(string option)
{
int i = GetLongOpt::CheckOpt(option);
return i;
}
int GetLongOpt::GetListFormOpt(string option, vector<string>* stringlist)
{
int count = 0;
int i = GetLongOpt::CheckOpt(option);
i++;
while((i > 0) && (i < argc))
{
if(argv[i][0] != '-')
{
stringlist->push_back(argv[i]);
count++;
}
else
{
return count;
}
i++;
}
return count;
}
/*///////////////////////////////////////////////////////////
//utility
///////////////////////////////////////////////////////////*/
/*
bool GetLongOpt::CheckFileExist(string filename)
{
return CheckFileExist(filename);
}
string GetLongOpt::GetMainFilename(string filename)
{
return GetMainFilename(filename);
}
*/
/*///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////*/
bool CheckFileExist(string filename)
{
ifstream *ifs;
ifs = new ifstream;
ifs->open(filename.c_str());
if(!*ifs)//not exist
{
delete ifs;
return false;
}
else //exitst
{
delete ifs;
return true;
}
}
string GetMainFilename(string filename)
{
string::size_type temp;
temp = filename.find_last_of('.');
if(temp != string::npos)
{
return filename.substr(0, temp);
}
else
{
return filename;
}
}