3 * Textual representation of multiprecision numbers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_MPTEXT_H
29 #define CATACOMB_MPTEXT_H
35 /*----- Header files ------------------------------------------------------*/
41 /*----- Data structures ---------------------------------------------------*/
43 typedef struct mptext_ops
{
44 int (*get
)(void */
*p*/
);
45 void (*unget
)(int /*ch*/, void */
*p*/
);
46 int (*put
)(const char */
*s*/
, size_t /*len*/, void */
*p*/
);
49 /*----- Functions provided ------------------------------------------------*/
51 /* --- @mp_read@ --- *
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
58 * Returns: The integer read, or zero if it didn't work.
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.
69 extern mp
*mp_read(mp */
*m*/
, int /*radix*/,
70 const mptext_ops */
*ops*/
, void */
*p*/
);
72 /* --- @mp_write@ --- *
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
79 * Returns: Zero if it worked, nonzero otherwise.
81 * Use: Writes a large integer in textual form.
84 extern int mp_write(mp */
*m*/
, int /*radix*/,
85 const mptext_ops */
*ops*/
, void */
*p*/
);
87 /* --- @mptext_len@ --- *
89 * Arguments: @mp *x@ = number to work on
90 * @int r@ = radix the number will be expressed in
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
94 * (use @MP_NEGP@ to check that, or just add one on for luck);
95 * neither will it add space for a terminating null. In general
96 * the answer will be an overestimate.
99 extern size_t mptext_len(mp */
*x*/
, int /*r*/);
101 /*----- File I/O ----------------------------------------------------------*/
105 /* --- Operations table --- *
107 * The @mptext_fileops@ expect the pointer argument to be a @FILE *@.
110 extern const mptext_ops mptext_fileops
;
112 /* --- Convenience functions --- */
114 extern mp
*mp_readfile(mp */
*m*/
, FILE */
*fp*/
, int /*radix*/);
115 extern int mp_writefile(mp */
*m*/
, FILE */
*fp*/
, int /*radix*/);
117 #define MP_DOFPRINTFR(fp, args, m, r) do { \
120 mp_writefile(m, fp, r); \
122 fputs("<null>", fp); \
126 #define MP_DOFPRINTR(fp, name, m, r) \
127 MP_DOFPRINTFR(fp, (fp, "%s = ", name), m, r)
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)
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)
137 /*----- String I/O --------------------------------------------------------*/
139 /* --- Context format --- */
141 typedef struct mptext_stringctx
{
146 /* --- Operations table --- */
148 extern const mptext_ops mptext_stringops
;
150 /* --- Convenience functions --- */
152 extern mp
*mp_readstring(mp */
*m*/
, const char */
*p*/
, char **/
*end*/
,
154 extern int mp_writestring(mp */
*m*/
, char */
*p*/
, size_t /*sz*/,
157 /*----- Dynamic string I/O ------------------------------------------------*/
159 #include <mLib/dstr.h>
161 /* --- Context format --- */
163 typedef struct mptext_dstrctx
{
168 /* --- Operations table --- */
170 extern const mptext_ops mptext_dstrops
;
172 /* --- Convenience functions --- */
174 extern mp
*mp_readdstr(mp */
*m*/
, dstr */
*d*/
, size_t */
*off*/
,
176 extern int mp_writedstr(mp */
*m*/
, dstr */
*d*/
, int /*radix*/);
178 /*----- That's all, folks -------------------------------------------------*/