xref: /llvm-project/polly/lib/External/isl/imath/examples/input.c (revision 658eb9e14264d48888ade0e3daf0b648f76c3f0e)
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 
main(int argc,char * argv[])36 int 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, "Usage: input <value> [output-base]\n");
44     return 1;
45   }
46   if (argc > 2) {
47     if ((radix = atoi(argv[2])) < MP_MIN_RADIX || (radix > MP_MAX_RADIX)) {
48       fprintf(stderr, "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(&value)) != MP_OK) {
56     fprintf(stderr, "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(&value, 0, argv[1], &endp)) != MP_OK) {
62     fprintf(stderr, "Error in mp_rat_read_ustring(): %s\n",
63             mp_error_string(res));
64 
65     if (res == MP_TRUNC) fprintf(stderr, " -- remaining input is: %s\n", endp);
66 
67     mp_rat_clear(&value);
68     return 1;
69   }
70 
71   printf("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(&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(MP_NUMER_P(&value), radix);
80       obuf = malloc(buf_size);
81 
82       /* Convert the value to a string in the desired radix. */
83       res = mp_int_to_string(MP_NUMER_P(&value), radix, obuf, buf_size);
84       if (res != MP_OK) {
85         fprintf(stderr, "Converstion to base %d failed: %s\n", radix,
86                 mp_error_string(res));
87         mp_rat_clear(&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(&value, radix);
94       obuf = malloc(buf_size);
95 
96       /* Convert the value to a string in the desired radix. */
97       res = mp_rat_to_string(&value, radix, obuf, buf_size);
98       if (res != MP_OK) {
99         fprintf(stderr, "Conversion to base %d failed: %s\n", radix,
100                 mp_error_string(res));
101         mp_rat_clear(&value);
102         return 1;
103       }
104     }
105     fputs(obuf, stdout);
106     fputc('\n', stdout);
107     free(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(&value);
113   return 0;
114 }
115 
116 /* Here there be dragons */
117