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