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