-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.c
300 lines (247 loc) · 7.95 KB
/
base.c
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* File: base.c
Copyright (C) 2011 David Hauweele <david@hauweele.net>
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/>. */
/* TODO:
cannot do this otherwise than reading string by string and convert them
at first we just we just use large integers in the future */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <getopt.h>
#include <ctype.h>
#include <errno.h>
#include <err.h>
#include "safe-call.h"
/* Since we are still using large integer for now
some mechanism will override this to avoid
integer overflow. */
#define MAX_LINE 4096
/* Default symbols will ensure the usual syntax in different bases. */
#define DEFAULT_MINUS '-'
#define DEFAULT_EXPONENT 'E'
#define DEFAULT_SEPARATOR '.'
#define DEFAULT_SYMBOLS "0123456789" \
"abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
static char *input_symbols = "0123456789";
static char *output_symbols = DEFAULT_SYMBOLS;
static char separator = DEFAULT_SEPARATOR;
static char exponent = DEFAULT_EXPONENT;
static char minus = DEFAULT_MINUS;
static int input_base;
static int output_base;
static bool leflag;
static const char *prog_name;
struct opts_name {
char name_short;
const char *name_long;
const char *help;
};
/* Auto-Indentation of the standard help message
according to the options specified in names. */
static void help(const struct opts_name *names)
{
const struct opts_name *opt;
int size;
int max = 0;
/* basic usage */
fprintf(stderr, "Usage: %s [OPTIONS] [SOCKET-PATH]\n", prog_name);
/* maximum option name size for padding */
for(opt = names ; opt->name_long ; opt++) {
size = strlen(opt->name_long);
if(size > max)
max = size;
}
/* print options and help messages */
for(opt = names ; opt->name_long ; opt++) {
if(opt->name_short != 0)
fprintf(stderr, " -%c, --%s", opt->name_short, opt->name_long);
else
fprintf(stderr, " --%s", opt->name_long);
/* padding */
size = strlen(opt->name_long);
for(; size <= max ; size++)
fputc(' ', stderr);
fprintf(stderr, "%s\n", opt->help);
}
}
/* This ensure that the string in argument
could be represented using the currently
defined symbols. And return an integer
to represent the base.
Mainly used while parsing the command line. */
static int check_base(const char *arg, bool in)
{
register int base = atoi(arg);
if(base < 2 || base >= sizeof(DEFAULT_SYMBOLS))
errx(EXIT_FAILURE, "%s base out of range",
in ? "Input" : "Output");
return base;
}
/* Check that the specified string is constitued of only
one charater and then return this character.
Mainly used while parsing the command line. */
static char check_character(const char *arg, const char *type)
{
if(strlen(arg) != 1)
errx(EXIT_FAILURE, "Invalid %s character", type);
return arg[0];
}
/* Return the value associated to the specified symbol
according to the input symbols or fail if out of range. */
static unsigned int input_symbols_value(char c)
{
unsigned int i = 0;
for(; input_symbols[i] != c && input_symbols[i] != '\0' ; i++);
if(input_symbols[i] == '\0')
errx(1, "Symbol invalid according to the specified input base");
return i;
}
/* This function convert the line from the input-base parameters
to the output base parameters. */
static void convert(const char *line)
{
char output[MAX_LINE];
int i;
uint64_t overflow = 0;
uint64_t integer_part = 0;
uint64_t fractional_part = 0;
bool minus_mode = false;
bool floating_mode = false;
bool exponent_mode = false;
if(line[0] == minus) {
minus_mode = true;
line++;
}
/* convert from input base */
for(const char *s = line ; *s != '\0' ; s++) {
/* ignore */
if(isspace(*s))
continue;
integer_part *= input_base;
integer_part += input_symbols_value(*s);
/* check overflow */
if(integer_part < overflow)
errx(1, "Overflow");
overflow = integer_part;
}
for(i = 0 ; integer_part ; i++) {
output[i] = output_symbols[integer_part % output_base];
integer_part /= output_base;
}
if(minus_mode)
putchar(minus);
for(i--; i >= 0 ; i--)
putchar(output[i]);
if(output[0] == '\0')
putchar(output_symbols[0]);
putchar('\n');
}
int main(int argc, char *argv[])
{
int exit_status = EXIT_FAILURE;
/* This structure is mainly used for the help(...) function and
map options to their respective help message. */
struct opts_name names[] = {
{ 'i', "input-base", "Base of the input numbers" },
{ 'o', "output-base", "Base of the output numbers" },
{ 'I', "input-sym", "Symbols of the input numbers" },
{ 'O', "output-sym", "Symbols of the output numbers" },
{ 's', "separator", "Floating point number separator" },
{ 'e', "exponent", "Floating point number exponent symbol" },
{ 'm', "minus", "Minus symbol" },
{ 'b', "big-endian", "Big endian notation" },
{ 'l', "little-endian", "Little endian notation" },
{ 'h', "help", "Show this help message" },
{ 0, NULL, NULL }
};
/* getopt long options declaration */
struct option opts[] = {
{ "input-base", required_argument, NULL, 'i' },
{ "output-base", required_argument, NULL, 'o' },
{ "input-sym", required_argument, NULL, 'I' },
{ "output-sym", required_argument, NULL, 'O' },
{ "separator", required_argument, NULL, 's' },
{ "exponent", required_argument, NULL, 'e' },
{ "minus", required_argument, NULL, 'm' },
{ "big-endian", no_argument, NULL, 'b' },
{ "little-endian", no_argument, NULL, 'l' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
/* parse program name */
prog_name = (const char *)strrchr(argv[0], '/');
prog_name = prog_name ? (prog_name + 1) : argv[0];
while(1) {
int c = getopt_long(argc, argv, "i:o:I:O:s:e:m:blh", opts, NULL);
if(c == -1)
break;
switch(c) {
int base;
case('i'):
base = check_base(optarg, true);
input_symbols = xmalloc(base + 1);
strncpy(input_symbols, DEFAULT_SYMBOLS, base);
break;
case('o'):
base = check_base(optarg, false);
output_symbols = xmalloc(base + 1);
strncpy(output_symbols, DEFAULT_SYMBOLS, base);
break;
case('I'):
input_symbols = optarg;
break;
case('O'):
output_symbols = optarg;
break;
case('s'):
separator = check_character(optarg, "separator");
break;
case('e'):
exponent = check_character(optarg, "exponent");
break;
case('m'):
minus = check_character(optarg, "minus");
break;
case 'b':
break;
case 'l':
leflag = true;
break;
case('h'):
exit_status = EXIT_SUCCESS;
default:
help(names);
exit(exit_status);
}
}
input_base = strlen(input_symbols);
output_base = strlen(output_symbols);
argc -= optind;
argv += optind;
if(!argv || *argv == '\0') {
while(!feof(stdin)) {
char buf[MAX_LINE];
if(!fgets(buf, MAX_LINE, stdin)) {
if(errno == 0)
return 0;
err(1, "Cannot read from stdin");
}
convert(buf);
}
}
for(; *argv ; argv++)
convert(*argv);
return 0;
}