| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mptext.h,v 1.6 2002/10/15 22:58:29 mdw Exp $ |
| 4 | * |
| 5 | * Textual representation of multiprecision numbers |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: mptext.h,v $ |
| 33 | * Revision 1.6 2002/10/15 22:58:29 mdw |
| 34 | * Fast estimation of number representation lengths. |
| 35 | * |
| 36 | * Revision 1.5 2000/10/08 12:04:58 mdw |
| 37 | * (MP_DOFPRINTFR): cope with null pointers. |
| 38 | * |
| 39 | * Revision 1.4 2000/06/17 11:46:58 mdw |
| 40 | * Convenience macros for producing debugging output containing MP |
| 41 | * integers. |
| 42 | * |
| 43 | * Revision 1.3 1999/12/22 15:56:30 mdw |
| 44 | * Make the buffer passed to `put' op constant. |
| 45 | * |
| 46 | * Revision 1.2 1999/12/10 23:29:48 mdw |
| 47 | * Change header file guard names. |
| 48 | * |
| 49 | * Revision 1.1 1999/11/17 18:02:16 mdw |
| 50 | * New multiprecision integer arithmetic suite. |
| 51 | * |
| 52 | */ |
| 53 | |
| 54 | #ifndef CATACOMB_MPTEXT_H |
| 55 | #define CATACOMB_MPTEXT_H |
| 56 | |
| 57 | #ifdef __cplusplus |
| 58 | extern "C" { |
| 59 | #endif |
| 60 | |
| 61 | /*----- Header files ------------------------------------------------------*/ |
| 62 | |
| 63 | #ifndef CATACOMB_MP_H |
| 64 | # include "mp.h" |
| 65 | #endif |
| 66 | |
| 67 | /*----- Data structures ---------------------------------------------------*/ |
| 68 | |
| 69 | typedef struct mptext_ops { |
| 70 | int (*get)(void */*p*/); |
| 71 | void (*unget)(int /*ch*/, void */*p*/); |
| 72 | int (*put)(const char */*s*/, size_t /*len*/, void */*p*/); |
| 73 | } mptext_ops; |
| 74 | |
| 75 | /*----- Functions provided ------------------------------------------------*/ |
| 76 | |
| 77 | /* --- @mp_read@ --- * |
| 78 | * |
| 79 | * Arguments: @mp *m@ = destination multiprecision number |
| 80 | * @int radix@ = base to assume for data (or zero to guess) |
| 81 | * @const mptext_ops *ops@ = pointer to operations block |
| 82 | * @void *p@ = data for the operations block |
| 83 | * |
| 84 | * Returns: The integer read, or zero if it didn't work. |
| 85 | * |
| 86 | * Use: Reads an integer from some source. If the @radix@ is |
| 87 | * specified, the number is assumed to be given in that radix, |
| 88 | * with the letters `a' (either upper- or lower-case) upwards |
| 89 | * standing for digits greater than 9. Otherwise, base 10 is |
| 90 | * assumed unless the number starts with `0' (octal), `0x' (hex) |
| 91 | * or `nnn_' (base `nnn'). An arbitrary amount of whitespace |
| 92 | * before the number is ignored. |
| 93 | */ |
| 94 | |
| 95 | extern mp *mp_read(mp */*m*/, int /*radix*/, |
| 96 | const mptext_ops */*ops*/, void */*p*/); |
| 97 | |
| 98 | /* --- @mp_write@ --- * |
| 99 | * |
| 100 | * Arguments: @mp *m@ = pointer to a multi-precision integer |
| 101 | * @int radix@ = radix to use when writing the number out |
| 102 | * @const mptext_ops *ops@ = pointer to an operations block |
| 103 | * @void *p@ = data for the operations block |
| 104 | * |
| 105 | * Returns: Zero if it worked, nonzero otherwise. |
| 106 | * |
| 107 | * Use: Writes a large integer in textual form. |
| 108 | */ |
| 109 | |
| 110 | extern int mp_write(mp */*m*/, int /*radix*/, |
| 111 | const mptext_ops */*ops*/, void */*p*/); |
| 112 | |
| 113 | /* --- @mptext_len@ --- * |
| 114 | * |
| 115 | * Arguments: @mp *x@ = number to work on |
| 116 | * @int r@ = radix the number will be expressed in |
| 117 | * |
| 118 | * Returns: The number of digits needed to represent the number in the |
| 119 | * given base. This will not include space for a leading sign |
| 120 | * (use @MP_ISNEG@ to check that, or just add one on for luck); |
| 121 | * neither will it add space for a terminating null. In general |
| 122 | * the answer will be an overestimate. |
| 123 | */ |
| 124 | |
| 125 | extern size_t mptext_len(mp */*x*/, int /*r*/); |
| 126 | |
| 127 | /*----- File I/O ----------------------------------------------------------*/ |
| 128 | |
| 129 | #include <stdio.h> |
| 130 | |
| 131 | /* --- Operations table --- * |
| 132 | * |
| 133 | * The @mptext_fileops@ expect the pointer argument to be a @FILE *@. |
| 134 | */ |
| 135 | |
| 136 | extern const mptext_ops mptext_fileops; |
| 137 | |
| 138 | /* --- Convenience functions --- */ |
| 139 | |
| 140 | extern mp *mp_readfile(mp */*m*/, FILE */*fp*/, int /*radix*/); |
| 141 | extern int mp_writefile(mp */*m*/, FILE */*fp*/, int /*radix*/); |
| 142 | |
| 143 | #define MP_DOFPRINTFR(fp, args, m, r) do { \ |
| 144 | fprintf args; \ |
| 145 | if (m) \ |
| 146 | mp_writefile(m, fp, r); \ |
| 147 | else \ |
| 148 | fputs("<null>", fp); \ |
| 149 | fputc('\n', fp); \ |
| 150 | } while (0) |
| 151 | |
| 152 | #define MP_DOFPRINTR(fp, name, m, r) \ |
| 153 | MP_DOFPRINTFR(fp, (fp, "%s = ", name), m, r) |
| 154 | |
| 155 | #define MP_PRINT(name, m) MP_DOFPRINTR(stdout, name, m, 10) |
| 156 | #define MP_EPRINT(name, m) MP_DOFPRINTR(stderr, name, m, 10) |
| 157 | #define MP_PRINTX(name, m) MP_DOFPRINTR(stdout, name, m, 16) |
| 158 | #define MP_EPRINTX(name, m) MP_DOFPRINTR(stderr, name, m, 16) |
| 159 | |
| 160 | #define MP_FPRINTF(fp, args, m) MP_DOFPRINTFR(fp, args, m, 10) |
| 161 | #define MP_FPRINTFX(fp, args, m) MP_DOFPRINTFR(fp, args, m, 16) |
| 162 | |
| 163 | /*----- String I/O --------------------------------------------------------*/ |
| 164 | |
| 165 | /* --- Context format --- */ |
| 166 | |
| 167 | typedef struct mptext_stringctx { |
| 168 | char *buf; |
| 169 | char *lim; |
| 170 | } mptext_stringctx; |
| 171 | |
| 172 | /* --- Operations table --- */ |
| 173 | |
| 174 | extern const mptext_ops mptext_stringops; |
| 175 | |
| 176 | /* --- Convenience functions --- */ |
| 177 | |
| 178 | extern mp *mp_readstring(mp */*m*/, const char */*p*/, char **/*end*/, |
| 179 | int /*radix*/); |
| 180 | extern int mp_writestring(mp */*m*/, char */*p*/, size_t /*sz*/, |
| 181 | int /*radix*/); |
| 182 | |
| 183 | /*----- Dynamic string I/O ------------------------------------------------*/ |
| 184 | |
| 185 | #include <mLib/dstr.h> |
| 186 | |
| 187 | /* --- Context format --- */ |
| 188 | |
| 189 | typedef struct mptext_dstrctx { |
| 190 | dstr *d; |
| 191 | size_t i; |
| 192 | } mptext_dstrctx; |
| 193 | |
| 194 | /* --- Operations table --- */ |
| 195 | |
| 196 | extern const mptext_ops mptext_dstrops; |
| 197 | |
| 198 | /* --- Convenience functions --- */ |
| 199 | |
| 200 | extern mp *mp_readdstr(mp */*m*/, dstr */*d*/, size_t */*off*/, |
| 201 | int /*radix*/); |
| 202 | extern int mp_writedstr(mp */*m*/, dstr */*d*/, int /*radix*/); |
| 203 | |
| 204 | /*----- That's all, folks -------------------------------------------------*/ |
| 205 | |
| 206 | #ifdef __cplusplus |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | #endif |