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