-
Notifications
You must be signed in to change notification settings - Fork 1
/
Args.h
141 lines (124 loc) · 4.03 KB
/
Args.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
/*
Calculate Mathematical Constants Using GMP (GNU Multiple Precision)
Copyright (C) 2006-2012 Hanhong Xue (macroxue at yahoo dot com)
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.
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, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _ARGS_H
#define _ARGS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
class Args
{
public:
Args(int argc, char *argv[]);
const char * GetName() const {
return name;
}
protected:
void usage();
protected:
const char * name; // name of the constant
size_t digits; // number of digits to calculate
const char * file; // file to save the result
size_t gcd_level; // GCD removal until this level
size_t verbose_level; // verbose level
static const size_t MAX_VERBOSE_LEVEL = 10;
};
Args::Args(int argc, char *argv[])
: name(NULL), digits(1000000), file(NULL), gcd_level(3), verbose_level(0)
{
int opt;
while ((opt = getopt(argc, argv, "d:o:g:v:h")) != -1) {
switch (opt) {
case 'd':
digits = atol(optarg);
switch (optarg[strlen(optarg)-1]) {
case 'K':
digits <<= 10;
break;
case 'M':
digits <<= 20;
break;
case 'G':
digits <<= 30;
break;
case 'k':
digits *= 1000;
break;
case 'm':
digits *= 1000000;
break;
case 'g':
digits *= 1000000000;
break;
}
break;
case 'o':
file = optarg;
break;
case 'g':
gcd_level = atol(optarg);
break;
case 'v':
verbose_level = atol(optarg);
break;
default :
usage();
break;
}
}
int pos, i;
for (pos = 0, i = optind; i < argc; pos++, i++) {
switch (pos) {
case 0:
name = argv[i];
break;
default:
printf("Error: Extra argument '%s'.\n", argv[i]);
usage();
}
}
if (name == NULL) {
printf("Specify the name of the constant to compute.\n");
usage();
}
if (digits <= 0) {
printf("The number of digits must be positive.\n");
usage();
}
if (verbose_level < 0) {
printf("Verbose level must be positive.\n");
usage();
}
if (verbose_level > MAX_VERBOSE_LEVEL)
verbose_level = MAX_VERBOSE_LEVEL;
}
void Args::usage()
{
const char *usage_format =
"Usage: const name [-d digits] [-o file] [-g level] [-v level]\n"
" name One of the following constants.\n"
" pi : pi using Chudnovsky formula\n"
" pi-rama : pi using Ramanujan formula\n"
" pi-agm : pi using AGM\n"
" e : e\n"
" -d digits Number of digits to calculate. Default: %lu.\n"
" 'K','M','G','k','m','g' notations can be used.\n"
" -o file File to save result. Default: result not saved.\n"
" -g level Remove GCD until this level. Default: %lu.\n"
" -v level Verbose level. Default: %lu.\n";
printf(usage_format, digits, gcd_level, verbose_level);
exit(-1);
}
#endif