-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.h
172 lines (135 loc) · 4.55 KB
/
commands.h
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
/*
* Copyright (C) 2010 Regents of the University of Michigan
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __COMMANDS_H__
#define __COMMANDS_H__
#include <ctype.h>
#include <stddef.h>
#include <vector>
#include <map>
#include <string>
typedef std::map<std::string,void*> StringMap;
typedef std::map<std::string,void*>::iterator StringMapIterator;
class commandList; // list of commands
// contents for long command list
struct longCommandList
{
const char * desc;
int (*func) (int, char**);
const char * help;
};
class command // represent each command - an abstract class
{
protected:
std::string description; // full name of the option
int (*func)(int, char**); // pointer to store actual value
std::string helpstring; // detailed description of the option
static int nameCol; // length of name column
static int statusCol; // length of status column
static int helpCol; // length of the help column
// get option by full string
//virtual int TranslateExtras(const char * value, const char * extras);
virtual longCommandList* Translate(const char* value);
//static bool CheckInteger(const char * value);
//static bool CheckDouble(const char * value);
std::string * errors;
std::string * messages;
public:
// constructor
command(const char * desc, int (*f)(int, char**), const char * help = NULL);
// destructor
virtual ~command() {}
// Read argn-th argument and assing values
//virtual int Read(int argc, char ** argv, int argn) = 0; // {}
// virtual function which prints the name and values
virtual void Status() = 0;
virtual void HelpMessage() = 0;
// modify nameCol
static void SetNameLen(int len)
{
nameCol = len;
}
// modify statusCol
static void SetStatusLen(int len)
{
statusCol = len;
}
// set error buffer
void SetErrorBuffer(std::string & buffer)
{
errors = &buffer;
}
// set error buffer
void SetMessageBuffer(std::string & buffer)
{
messages = &buffer;
}
// function printing warning
//void error(const char * format, ...);
//void message(const char * format, ...);
// give full access to paramList class
friend class commandList;
};
#define BEGIN_LONG_COMMANDS(array) longCommandList array[] = { \
{ NULL, NULL, NULL},
#define LONG_COMMAND_GROUP(label, help) { label, NULL, help },
#define LONG_COMMAND(label, funcptr, help) { label, funcptr, help},
#define END_LONG_COMMANDS() { NULL, NULL, NULL } };
// long command class
class longCommands : public command
{
public:
longCommands(const char * desc, longCommandList * list); // constructor
virtual void Status(); // Print the status
virtual void HelpMessage(); // Print the status
//longCommands * SetPrecision(int precision) // Set precision for output
//{
// this->precision = precision;
//
// return this;
//}
protected:
std::map<std::string, longCommandList*> index;
//std::map<std::string, longCommandList*> legacyIndex;
longCommandList * list;
int group_len;
int name_len;
//int precision;
virtual longCommandList* Translate(const char* value);
//virtual void Translate(const char * value);
//virtual int TranslateExtras(const char * value, const char * extras);
void Status(longCommandList * ptr, int & line_len, bool & need_a_comma);
void HelpMessage(longCommandList * ptr);
};
// List of commands
class commandList {
protected:
bool help;
std::vector<command*> pl; // vector of pointers;
public:
commandList() : help(false) {}
virtual ~commandList();
void Add(command * p);
// Tries to process all command line arguments
virtual int Read(int argc, char ** argv, int start = 1);
// Outputs summary of command switches and settings
virtual void Status();
virtual void HelpMessage();
// Keeps track of warnings generated during command processing
std::string errors;
std::string messages;
};
#endif // commands.h