118 lines · c
1/*2 Name: basecvt.c3 Purpose: Convert integers and rationals from one base to another.4 Author: M. J. Fromberger5 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 copy9 of this software and associated documentation files (the "Software"), to deal10 in the Software without restriction, including without limitation the rights11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell12 copies of the Software, and to permit persons to whom the Software is13 furnished to do so, subject to the following conditions:14 15 The above copyright notice and this permission notice shall be included in16 all copies or substantial portions of the Software.17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER22 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 THE24 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, "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,75 mp_error_string(res));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,103 mp_error_string(res));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 */118