Polly 20.0.0git
basecvt.c
Go to the documentation of this file.
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;
39 int ix;
40
41 if (argc < 4) {
42 fprintf(stderr, "Usage: basecvt <ibase> <obase> <values>+\n");
43 return 1;
44 }
45
46 in_rdx = atoi(argv[1]);
47 out_rdx = atoi(argv[2]);
48
49 if (in_rdx < MP_MIN_RADIX || in_rdx > MP_MAX_RADIX) {
50 fprintf(stderr,
51 "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 "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(&value)) != MP_OK) {
63 fprintf(stderr, "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(&value, in_rdx, argv[ix], &endp);
73 if (res != MP_OK && res != MP_TRUNC) {
74 fprintf(stderr, "basecvt: error reading argument %d: %s\n", ix,
76 break;
77 } else if (*endp != '\0') {
78 fprintf(stderr, "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(&value);
84 if (is_int) {
85 len = mp_int_string_len(MP_NUMER_P(&value), out_rdx);
86 } else {
87 len = mp_rat_string_len(&value, out_rdx);
88 }
89
90 if ((buf = malloc(len)) == NULL) {
91 fprintf(stderr, "basecvt: out of memory\n");
92 break;
93 }
94
95 if (is_int) {
96 res = mp_int_to_string(MP_NUMER_P(&value), out_rdx, buf, len);
97 } else {
98 res = mp_rat_to_string(&value, out_rdx, buf, len);
99 }
100
101 if (res != MP_OK) {
102 fprintf(stderr, "basecvt: error converting argument %d: %s\n", ix,
104 free(buf);
105 break;
106 }
107
108 printf("%s\n", buf);
109 free(buf);
110 }
111
112 mp_rat_clear(&value);
113
114 return (res != MP_OK);
115}
116
117/* Here there be dragons */
unsigned int mp_size
Definition: imath/imath.h:39
#define MP_MAX_RADIX
Definition: imath/imath.h:88
int mp_result
Definition: imath/imath.h:40
#define MP_MIN_RADIX
Definition: imath/imath.h:87
static mp_int MP_NUMER_P(mp_rat Q)
Definition: imath/imrat.h:44
const char * res
Definition: isl_test.c:775
#define mp_int_to_string
Definition: wrap.h:122
#define mp_rat_is_integer
Definition: wrap.h:147
#define mp_rat_string_len
Definition: wrap.h:163
#define mp_error_string
Definition: wrap.h:66
#define mp_int_string_len
Definition: wrap.h:116
#define MP_TRUNC
Definition: wrap.h:12
#define mp_rat_read_ustring
Definition: wrap.h:157
#define mp_rat_clear
Definition: wrap.h:131
#define mp_rat_init
Definition: wrap.h:144
#define mp_rat_to_string
Definition: wrap.h:168
#define MP_OK
Definition: wrap.h:9