d3409d5e |
1 | /* -*-c-*- |
2 | * |
b9b786f5 |
3 | * $Id: mptext-string.c,v 1.2 1999/12/22 15:56:21 mdw Exp $ |
d3409d5e |
4 | * |
5 | * Reading and writing large integers on strings |
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-string.c,v $ |
b9b786f5 |
33 | * Revision 1.2 1999/12/22 15:56:21 mdw |
34 | * Make the buffer passed to `put' op constant. |
35 | * |
d3409d5e |
36 | * Revision 1.1 1999/11/17 18:02:16 mdw |
37 | * New multiprecision integer arithmetic suite. |
38 | * |
39 | */ |
40 | |
41 | /*----- Header files ------------------------------------------------------*/ |
42 | |
43 | #include <string.h> |
44 | |
45 | #include "mptext.h" |
46 | |
47 | /*----- Main code ---------------------------------------------------------*/ |
48 | |
49 | /* --- Operations table --- */ |
50 | |
51 | static int get(void *p) |
52 | { |
53 | mptext_stringctx *c = p; |
54 | if (c->buf >= c->lim) |
55 | return (EOF); |
56 | return (*c->buf++); |
57 | } |
58 | |
59 | static void unget(int ch, void *p) |
60 | { |
61 | mptext_stringctx *c = p; |
62 | if (ch != EOF) |
63 | c->buf--; |
64 | } |
65 | |
b9b786f5 |
66 | static int put(const char *s, size_t sz, void *p) |
d3409d5e |
67 | { |
68 | mptext_stringctx *c = p; |
69 | int rc = 0; |
70 | if (sz > c->lim - c->buf) { |
71 | sz = c->lim - c->buf; |
72 | rc = EOF; |
73 | } |
74 | if (sz) { |
75 | memcpy(c->buf, s, sz); |
76 | c->buf += sz; |
77 | } |
78 | return (rc); |
79 | } |
80 | |
81 | const mptext_ops mptext_stringops = { get, unget, put }; |
82 | |
83 | /* --- Convenience functions --- */ |
84 | |
85 | mp *mp_readstring(mp *m, const char *p, char **end, int radix) |
86 | { |
87 | mptext_stringctx c; |
88 | c.buf = (char *)p; |
89 | c.lim = c.buf + strlen(p); |
90 | m = mp_read(m, radix, &mptext_stringops, &c); |
91 | if (end) |
92 | *end = c.buf; |
93 | return (m); |
94 | } |
95 | |
96 | int mp_writestring(mp *m, char *p, size_t sz, int radix) |
97 | { |
98 | mptext_stringctx c; |
99 | int rc; |
100 | if (!sz) |
101 | return (0); |
102 | c.buf = p; |
103 | c.lim = p + sz - 1; |
104 | rc = mp_write(m, radix, &mptext_stringops, &c); |
105 | *c.buf = 0; |
106 | return (rc); |
107 | } |
108 | |
109 | /*----- That's all, folks -------------------------------------------------*/ |