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