3 * $Id: mptext.h,v 1.4 2000/06/17 11:46:58 mdw Exp $
5 * Textual representation of multiprecision numbers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 2000/06/17 11:46:58 mdw
34 * Convenience macros for producing debugging output containing MP
37 * Revision 1.3 1999/12/22 15:56:30 mdw
38 * Make the buffer passed to `put' op constant.
40 * Revision 1.2 1999/12/10 23:29:48 mdw
41 * Change header file guard names.
43 * Revision 1.1 1999/11/17 18:02:16 mdw
44 * New multiprecision integer arithmetic suite.
48 #ifndef CATACOMB_MPTEXT_H
49 #define CATACOMB_MPTEXT_H
55 /*----- Header files ------------------------------------------------------*/
61 /*----- Data structures ---------------------------------------------------*/
63 typedef struct mptext_ops
{
64 int (*get
)(void */
*p*/
);
65 void (*unget
)(int /*ch*/, void */
*p*/
);
66 int (*put
)(const char */
*s*/
, size_t /*len*/, void */
*p*/
);
69 /*----- Functions provided ------------------------------------------------*/
71 /* --- @mp_read@ --- *
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
78 * Returns: The integer read, or zero if it didn't work.
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.
89 extern mp
*mp_read(mp */
*m*/
, int /*radix*/,
90 const mptext_ops */
*ops*/
, void */
*p*/
);
92 /* --- @mp_write@ --- *
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
99 * Returns: Zero if it worked, nonzero otherwise.
101 * Use: Writes a large integer in textual form.
104 extern int mp_write(mp */
*m*/
, int /*radix*/,
105 const mptext_ops */
*ops*/
, void */
*p*/
);
107 /*----- File I/O ----------------------------------------------------------*/
111 /* --- Operations table --- *
113 * The @mptext_fileops@ expect the pointer argument to be a @FILE *@.
116 extern const mptext_ops mptext_fileops
;
118 /* --- Convenience functions --- */
120 extern mp
*mp_readfile(mp */
*m*/
, FILE */
*fp*/
, int /*radix*/);
121 extern int mp_writefile(mp */
*m*/
, FILE */
*fp*/
, int /*radix*/);
123 #define MP_DOFPRINTFR(fp, args, m, r) do { \
125 mp_writefile(m, fp, r); \
129 #define MP_DOFPRINTR(fp, name, m, r) \
130 MP_DOFPRINTFR(fp, (fp, "%s = ", name), m, r)
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)
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)
140 /*----- String I/O --------------------------------------------------------*/
142 /* --- Context format --- */
144 typedef struct mptext_stringctx
{
149 /* --- Operations table --- */
151 extern const mptext_ops mptext_stringops
;
153 /* --- Convenience functions --- */
155 extern mp
*mp_readstring(mp */
*m*/
, const char */
*p*/
, char **/
*end*/
,
157 extern int mp_writestring(mp */
*m*/
, char */
*p*/
, size_t /*sz*/,
160 /*----- Dynamic string I/O ------------------------------------------------*/
162 #include <mLib/dstr.h>
164 /* --- Context format --- */
166 typedef struct mptext_dstrctx
{
171 /* --- Operations table --- */
173 extern const mptext_ops mptext_dstrops
;
175 /* --- Convenience functions --- */
177 extern mp
*mp_readdstr(mp */
*m*/
, dstr */
*d*/
, size_t */
*off*/
,
179 extern int mp_writedstr(mp */
*m*/
, dstr */
*d*/
, int /*radix*/);
181 /*----- That's all, folks -------------------------------------------------*/