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