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