1/*
2 Name: input.c
3 Purpose: Basic I/O demo for IMath.
4 Author: Michael J. Fromberger
5
6 This program demonstrates how to read and write arbitrary precision integers
7 using IMath.
8
9 Copyright (C) 2003-2008 Michael J. Fromberger, All Rights Reserved.
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include "imrat.h"
35
36int main(int argc, char *argv[]) {
37 mp_size radix = 10; /* Default output radix */
38 mpq_t value;
39 mp_result res;
40 char *endp;
41
42 if (argc < 2) {
43 fprintf(stderr, format: "Usage: input <value> [output-base]\n");
44 return 1;
45 }
46 if (argc > 2) {
47 if ((radix = atoi(nptr: argv[2])) < MP_MIN_RADIX || (radix > MP_MAX_RADIX)) {
48 fprintf(stderr, format: "Error: Specified radix is out of range (%d)\n", radix);
49 return 1;
50 }
51 }
52
53 /* Initialize a new value, initially zero; illustrates how to check
54 for errors (e.g., out of memory) and display a message. */
55 if ((res = mp_rat_init(r: &value)) != MP_OK) {
56 fprintf(stderr, format: "Error in mp_rat_init(): %s\n", mp_error_string(res));
57 return 1;
58 }
59
60 /* Read value in base 10 */
61 if ((res = mp_rat_read_ustring(r: &value, radix: 0, str: argv[1], end: &endp)) != MP_OK) {
62 fprintf(stderr, format: "Error in mp_rat_read_ustring(): %s\n",
63 mp_error_string(res));
64
65 if (res == MP_TRUNC) fprintf(stderr, format: " -- remaining input is: %s\n", endp);
66
67 mp_rat_clear(r: &value);
68 return 1;
69 }
70
71 printf(format: "Here is your value in base %d\n", radix);
72 {
73 mp_result buf_size, res;
74 char *obuf;
75
76 if (mp_rat_is_integer(r: &value)) {
77 /* Allocate a buffer big enough to hold the given value, including
78 sign and zero terminator. */
79 buf_size = mp_int_string_len(z: MP_NUMER_P(Q: &value), radix);
80 obuf = malloc(size: buf_size);
81
82 /* Convert the value to a string in the desired radix. */
83 res = mp_int_to_string(z: MP_NUMER_P(Q: &value), radix, str: obuf, limit: buf_size);
84 if (res != MP_OK) {
85 fprintf(stderr, format: "Converstion to base %d failed: %s\n", radix,
86 mp_error_string(res));
87 mp_rat_clear(r: &value);
88 return 1;
89 }
90 } else {
91 /* Allocate a buffer big enough to hold the given value, including
92 sign and zero terminator. */
93 buf_size = mp_rat_string_len(r: &value, radix);
94 obuf = malloc(size: buf_size);
95
96 /* Convert the value to a string in the desired radix. */
97 res = mp_rat_to_string(r: &value, radix, str: obuf, limit: buf_size);
98 if (res != MP_OK) {
99 fprintf(stderr, format: "Conversion to base %d failed: %s\n", radix,
100 mp_error_string(res));
101 mp_rat_clear(r: &value);
102 return 1;
103 }
104 }
105 fputs(s: obuf, stdout);
106 fputc(c: '\n', stdout);
107 free(ptr: obuf);
108 }
109
110 /* When you are done with a value, it must be "cleared" to release
111 the memory it occupies */
112 mp_rat_clear(r: &value);
113 return 0;
114}
115
116/* Here there be dragons */
117

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