forked from matthewarcus/mmps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
109 lines (94 loc) · 2.53 KB
/
utils.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
// $Revision: 1.4 $
// utils.h
// (C) 2004 by Matthew Arcus
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#if !defined UTILS_H
#define UTILS_H
#include <stdio.h>
#include <math.h>
#include <vector>
#include <string>
static inline double roundint(double x)
{
return floor (x + 0.5);
}
static const unsigned int MESSAGESIZE = 1024;
void error(const char *s);
void syserror(const char *s);
template <class T>
void error(const char *s, T t)
{
char message[MESSAGESIZE];
snprintf(message, MESSAGESIZE, s, t);
error(message);
}
template <class T>
void syserror(const char *s, T t)
{
char message[MESSAGESIZE];
snprintf(message, MESSAGESIZE, s, t);
syserror(message);
}
class OptSpec
{
public:
OptSpec(const char *optstring);
virtual int ReadOpt(int &argc, char** &argv) = 0;
const char *optstring;
static int ReadOpts (int &argc, char** &argv, OptSpec* optspecs[]);
virtual ~OptSpec() {};
private:
OptSpec(const OptSpec&);
OptSpec &operator=(const OptSpec&);
};
class BoolSpec : public OptSpec
{
public:
BoolSpec (const char *optstring, bool &value);
int ReadOpt(int &argc, char** &argv);
private:
bool &value;
};
class StringSpec : public OptSpec
{
public:
StringSpec (const char *optstring, const char* &value);
int ReadOpt(int &argc, char** &argv);
private:
const char* &value;
};
class IntSpec : public OptSpec
{
public:
IntSpec (const char *optstring, int &value);
int ReadOpt(int &argc, char** &argv);
private:
int &value;
};
class DoubleSpec : public OptSpec
{
public:
DoubleSpec (const char *optstring, double &value, double conversion = 1.0);
int ReadOpt(int &argc, char** &argv);
private:
double &value;
double conversion;
};
class CmdSpec : public OptSpec
{
public:
CmdSpec (const char *optstring, int nargs);
int ReadOpt(int &argc, char** &argv);
static const std::vector<std::vector<std::string> > &getcmdlist() { return cmdlist; }
private:
int nargs;
static std::vector<std::vector<std::string> > cmdlist;
};
#endif