d3409d5e |
1 | /* -*-c-*- |
2 | * |
7383bc11 |
3 | * $Id: mptext.h,v 1.5 2000/10/08 12:04:58 mdw Exp $ |
d3409d5e |
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 $ |
7383bc11 |
33 | * Revision 1.5 2000/10/08 12:04:58 mdw |
34 | * (MP_DOFPRINTFR): cope with null pointers. |
35 | * |
f654f421 |
36 | * Revision 1.4 2000/06/17 11:46:58 mdw |
37 | * Convenience macros for producing debugging output containing MP |
38 | * integers. |
39 | * |
b9b786f5 |
40 | * Revision 1.3 1999/12/22 15:56:30 mdw |
41 | * Make the buffer passed to `put' op constant. |
42 | * |
b3f05084 |
43 | * Revision 1.2 1999/12/10 23:29:48 mdw |
44 | * Change header file guard names. |
45 | * |
d3409d5e |
46 | * Revision 1.1 1999/11/17 18:02:16 mdw |
47 | * New multiprecision integer arithmetic suite. |
48 | * |
49 | */ |
50 | |
b3f05084 |
51 | #ifndef CATACOMB_MPTEXT_H |
52 | #define CATACOMB_MPTEXT_H |
d3409d5e |
53 | |
54 | #ifdef __cplusplus |
55 | extern "C" { |
56 | #endif |
57 | |
58 | /*----- Header files ------------------------------------------------------*/ |
59 | |
b3f05084 |
60 | #ifndef CATACOMB_MP_H |
d3409d5e |
61 | # include "mp.h" |
62 | #endif |
63 | |
64 | /*----- Data structures ---------------------------------------------------*/ |
65 | |
66 | typedef struct mptext_ops { |
67 | int (*get)(void */*p*/); |
68 | void (*unget)(int /*ch*/, void */*p*/); |
b9b786f5 |
69 | int (*put)(const char */*s*/, size_t /*len*/, void */*p*/); |
d3409d5e |
70 | } mptext_ops; |
71 | |
72 | /*----- Functions provided ------------------------------------------------*/ |
73 | |
74 | /* --- @mp_read@ --- * |
75 | * |
76 | * Arguments: @mp *m@ = destination multiprecision number |
77 | * @int radix@ = base to assume for data (or zero to guess) |
78 | * @const mptext_ops *ops@ = pointer to operations block |
79 | * @void *p@ = data for the operations block |
80 | * |
81 | * Returns: The integer read, or zero if it didn't work. |
82 | * |
83 | * Use: Reads an integer from some source. If the @radix@ is |
84 | * specified, the number is assumed to be given in that radix, |
85 | * with the letters `a' (either upper- or lower-case) upwards |
86 | * standing for digits greater than 9. Otherwise, base 10 is |
87 | * assumed unless the number starts with `0' (octal), `0x' (hex) |
88 | * or `nnn_' (base `nnn'). An arbitrary amount of whitespace |
89 | * before the number is ignored. |
90 | */ |
91 | |
92 | extern mp *mp_read(mp */*m*/, int /*radix*/, |
93 | const mptext_ops */*ops*/, void */*p*/); |
94 | |
95 | /* --- @mp_write@ --- * |
96 | * |
97 | * Arguments: @mp *m@ = pointer to a multi-precision integer |
98 | * @int radix@ = radix to use when writing the number out |
99 | * @const mptext_ops *ops@ = pointer to an operations block |
100 | * @void *p@ = data for the operations block |
101 | * |
102 | * Returns: Zero if it worked, nonzero otherwise. |
103 | * |
104 | * Use: Writes a large integer in textual form. |
105 | */ |
106 | |
107 | extern int mp_write(mp */*m*/, int /*radix*/, |
108 | const mptext_ops */*ops*/, void */*p*/); |
109 | |
110 | /*----- File I/O ----------------------------------------------------------*/ |
111 | |
112 | #include <stdio.h> |
113 | |
114 | /* --- Operations table --- * |
115 | * |
116 | * The @mptext_fileops@ expect the pointer argument to be a @FILE *@. |
117 | */ |
118 | |
119 | extern const mptext_ops mptext_fileops; |
120 | |
121 | /* --- Convenience functions --- */ |
122 | |
123 | extern mp *mp_readfile(mp */*m*/, FILE */*fp*/, int /*radix*/); |
124 | extern int mp_writefile(mp */*m*/, FILE */*fp*/, int /*radix*/); |
125 | |
f654f421 |
126 | #define MP_DOFPRINTFR(fp, args, m, r) do { \ |
127 | fprintf args; \ |
7383bc11 |
128 | if (m) \ |
129 | mp_writefile(m, fp, r); \ |
130 | else \ |
131 | fputs("<null>", fp); \ |
f654f421 |
132 | fputc('\n', fp); \ |
133 | } while (0) |
134 | |
135 | #define MP_DOFPRINTR(fp, name, m, r) \ |
136 | MP_DOFPRINTFR(fp, (fp, "%s = ", name), m, r) |
137 | |
138 | #define MP_PRINT(name, m) MP_DOFPRINTR(stdout, name, m, 10) |
139 | #define MP_EPRINT(name, m) MP_DOFPRINTR(stderr, name, m, 10) |
140 | #define MP_PRINTX(name, m) MP_DOFPRINTR(stdout, name, m, 16) |
141 | #define MP_EPRINTX(name, m) MP_DOFPRINTR(stderr, name, m, 16) |
142 | |
143 | #define MP_FPRINTF(fp, args, m) MP_DOFPRINTFR(fp, args, m, 10) |
144 | #define MP_FPRINTFX(fp, args, m) MP_DOFPRINTFR(fp, args, m, 16) |
145 | |
d3409d5e |
146 | /*----- String I/O --------------------------------------------------------*/ |
147 | |
148 | /* --- Context format --- */ |
149 | |
150 | typedef struct mptext_stringctx { |
151 | char *buf; |
152 | char *lim; |
153 | } mptext_stringctx; |
154 | |
155 | /* --- Operations table --- */ |
156 | |
157 | extern const mptext_ops mptext_stringops; |
158 | |
159 | /* --- Convenience functions --- */ |
160 | |
161 | extern mp *mp_readstring(mp */*m*/, const char */*p*/, char **/*end*/, |
162 | int /*radix*/); |
163 | extern int mp_writestring(mp */*m*/, char */*p*/, size_t /*sz*/, |
164 | int /*radix*/); |
165 | |
166 | /*----- Dynamic string I/O ------------------------------------------------*/ |
167 | |
168 | #include <mLib/dstr.h> |
169 | |
170 | /* --- Context format --- */ |
171 | |
172 | typedef struct mptext_dstrctx { |
173 | dstr *d; |
174 | size_t i; |
175 | } mptext_dstrctx; |
176 | |
177 | /* --- Operations table --- */ |
178 | |
179 | extern const mptext_ops mptext_dstrops; |
180 | |
181 | /* --- Convenience functions --- */ |
182 | |
183 | extern mp *mp_readdstr(mp */*m*/, dstr */*d*/, size_t */*off*/, |
184 | int /*radix*/); |
185 | extern int mp_writedstr(mp */*m*/, dstr */*d*/, int /*radix*/); |
186 | |
187 | /*----- That's all, folks -------------------------------------------------*/ |
188 | |
189 | #ifdef __cplusplus |
190 | } |
191 | #endif |
192 | |
193 | #endif |