1/*
2 Name: basecvt.c
3 Purpose: Convert integers and rationals from one base to another.
4 Author: M. J. Fromberger
5
6 Copyright (C) 2004-2008 Michael J. Fromberger, All Rights Reserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 */
26
27#include <errno.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#include "imath.h"
33#include "imrat.h"
34
35int main(int argc, char *argv[]) {
36 mp_size in_rdx, out_rdx;
37 mpq_t value;
38 mp_result res;
39 int ix;
40
41 if (argc < 4) {
42 fprintf(stderr, format: "Usage: basecvt <ibase> <obase> <values>+\n");
43 return 1;
44 }
45
46 in_rdx = atoi(nptr: argv[1]);
47 out_rdx = atoi(nptr: argv[2]);
48
49 if (in_rdx < MP_MIN_RADIX || in_rdx > MP_MAX_RADIX) {
50 fprintf(stderr,
51 format: "basecvt: input radix %u not allowed (minimum %u, maximum %u)\n",
52 in_rdx, MP_MIN_RADIX, MP_MAX_RADIX);
53 return 3;
54 }
55 if (out_rdx < MP_MIN_RADIX || out_rdx > MP_MAX_RADIX) {
56 fprintf(stderr,
57 format: "basecvt: output radix %u not allowed (minimum %u, maximum %u)\n",
58 out_rdx, MP_MIN_RADIX, MP_MAX_RADIX);
59 return 3;
60 }
61
62 if ((res = mp_rat_init(r: &value)) != MP_OK) {
63 fprintf(stderr, format: "basecvt: out of memory\n");
64 return 2;
65 }
66
67 for (ix = 3; ix < argc; ++ix) {
68 char *buf, *endp = NULL;
69 mp_result len;
70 int is_int;
71
72 res = mp_rat_read_ustring(r: &value, radix: in_rdx, str: argv[ix], end: &endp);
73 if (res != MP_OK && res != MP_TRUNC) {
74 fprintf(stderr, format: "basecvt: error reading argument %d: %s\n", ix,
75 mp_error_string(res));
76 break;
77 } else if (*endp != '\0') {
78 fprintf(stderr, format: "basecvt: argument %d contains '%s' not in base %u\n",
79 ix, endp, in_rdx);
80 continue;
81 }
82
83 is_int = mp_rat_is_integer(r: &value);
84 if (is_int) {
85 len = mp_int_string_len(z: MP_NUMER_P(Q: &value), radix: out_rdx);
86 } else {
87 len = mp_rat_string_len(r: &value, radix: out_rdx);
88 }
89
90 if ((buf = malloc(size: len)) == NULL) {
91 fprintf(stderr, format: "basecvt: out of memory\n");
92 break;
93 }
94
95 if (is_int) {
96 res = mp_int_to_string(z: MP_NUMER_P(Q: &value), radix: out_rdx, str: buf, limit: len);
97 } else {
98 res = mp_rat_to_string(r: &value, radix: out_rdx, str: buf, limit: len);
99 }
100
101 if (res != MP_OK) {
102 fprintf(stderr, format: "basecvt: error converting argument %d: %s\n", ix,
103 mp_error_string(res));
104 free(ptr: buf);
105 break;
106 }
107
108 printf(format: "%s\n", buf);
109 free(ptr: buf);
110 }
111
112 mp_rat_clear(r: &value);
113
114 return (res != MP_OK);
115}
116
117/* Here there be dragons */
118

source code of polly/lib/External/isl/imath/examples/basecvt.c