-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.h
118 lines (93 loc) · 3.16 KB
/
config.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
// -*- c++ -*-
//
// Copyright 2005-2008 Matt T. Yourst <[email protected]>
//
// This program is free software; it is licensed under the
// GNU General Public License, Version 2.
//
#ifndef _CONFIG_H_
#define _CONFIG_H_
#include <globals.h>
#include <superstl.h>
#include <stdarg.h>
static const W64 infinity = limits<W64s>::max;
struct ConfigurationOption {
const char* name;
const char* description;
int type;
int fieldsize;
Waddr offset;
ConfigurationOption* next;
ConfigurationOption() { }
ConfigurationOption(const char* name, const char* description, int type, Waddr offset, int fieldsize = 0) {
this->name = name;
this->description = description;
this->type = type;
this->offset = offset;
this->fieldsize = fieldsize;
this->next = null;
}
};
enum {
OPTION_TYPE_NONE = 0,
OPTION_TYPE_W64 = 1,
OPTION_TYPE_FLOAT = 2,
OPTION_TYPE_STRING = 3,
OPTION_TYPE_TRAILER = 4,
OPTION_TYPE_BOOL = 5,
OPTION_TYPE_SECTION = -1
};
struct ConfigurationParserBase {
ConfigurationOption* options;
ConfigurationOption* lastoption;
void addentry(void* baseptr, void* field, int type, const char* name, const char* description) {
Waddr offset = ((Waddr)field) - ((Waddr)baseptr);
ConfigurationOption* option = new ConfigurationOption(name, description, type, offset);
if (lastoption) lastoption->next = option;
if (!options) options = option;
lastoption = option;
}
ConfigurationParserBase() { options = null; lastoption = null; }
int parse(void* baseptr, int argc, char* argv[]);
int parse(void* baseptr, char* argstr);
ostream& printusage(const void* baseptr, ostream& os) const;
ostream& print(const void* baseptr, ostream& os) const;
};
template <typename T>
struct ConfigurationParser: public T {
ConfigurationParserBase options;
ConfigurationParser() { }
void add(W64& field, const char* name, const char* description) {
options.addentry(this, &field, OPTION_TYPE_W64, name, description);
}
void add(double& field, const char* name, const char* description) {
options.addentry(this, &field, OPTION_TYPE_FLOAT, name, description);
}
void add(bool& field, const char* name, const char* description) {
options.addentry(this, &field, OPTION_TYPE_BOOL, name, description);
}
void add(stringbuf& field, const char* name, const char* description) {
options.addentry(this, field, OPTION_TYPE_STRING, name, description);
}
void section(const char* name) {
options.addentry(this, null, OPTION_TYPE_SECTION, name, name);
}
int parse(T& config, int argc, char* argv[]) {
return options.parse(&config, argc, argv);
}
int parse(T& config, char* argstr) {
return options.parse(&config, argstr);
}
ostream& print(ostream& os, const T& config) {
return options.print(&config, os);
}
ostream& printusage(ostream& os, const T& config) {
return options.printusage(&config, os);
}
// Provided by user:
void setup();
};
void expand_command_list(dynarray<char*>& list, int argc, char** argv, int depth = 0);
void expand_command_list(dynarray<char*>& list, char* args, int depth = 0);
void free_command_list(dynarray<char*>& list);
#endif // _CONFIG_H_