blob: 6781f2582861ad7de7e588edcbb443a3a16cd868 [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/* __gmp_sprintf_funs -- support for gmp_sprintf and gmp_vsprintf.
2
3 THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
4 CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5 FUTURE GNU MP RELEASES.
6
7Copyright 2001 Free Software Foundation, Inc.
8
9This file is part of the GNU MP Library.
10
11The GNU MP Library is free software; you can redistribute it and/or modify
12it under the terms of either:
13
14 * the GNU Lesser General Public License as published by the Free
15 Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
17
18or
19
20 * the GNU General Public License as published by the Free Software
21 Foundation; either version 2 of the License, or (at your option) any
22 later version.
23
24or both in parallel, as here.
25
26The GNU MP Library is distributed in the hope that it will be useful, but
27WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29for more details.
30
31You should have received copies of the GNU General Public License and the
32GNU Lesser General Public License along with the GNU MP Library. If not,
33see https://www.gnu.org/licenses/. */
34
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include "gmp-impl.h"
41
42
43/* The data parameter "bufp" points to a "char *buf" which is the next
44 character to be written, having started as the destination from the
45 application. This is then increased each time output is produced. */
46
47
48/* If vsprintf returns -1 then pass it upwards. It doesn't matter that
49 "*bufp" is ruined in this case, since gmp_doprint will bail out
50 immediately anyway. */
51static int
52gmp_sprintf_format (char **bufp, const char *fmt, va_list ap)
53{
54 char *buf = *bufp;
55 int ret;
56 vsprintf (buf, fmt, ap);
57 ret = strlen (buf);
58 *bufp = buf + ret;
59 return ret;
60}
61
62static int
63gmp_sprintf_memory (char **bufp, const char *str, size_t len)
64{
65 char *buf = *bufp;
66 *bufp = buf + len;
67 memcpy (buf, str, len);
68 return len;
69}
70
71static int
72gmp_sprintf_reps (char **bufp, int c, int reps)
73{
74 char *buf = *bufp;
75 ASSERT (reps >= 0);
76 *bufp = buf + reps;
77 memset (buf, c, reps);
78 return reps;
79}
80
81static int
82gmp_sprintf_final (char **bufp)
83{
84 char *buf = *bufp;
85 *buf = '\0';
86 return 0;
87}
88
89const struct doprnt_funs_t __gmp_sprintf_funs = {
90 (doprnt_format_t) gmp_sprintf_format,
91 (doprnt_memory_t) gmp_sprintf_memory,
92 (doprnt_reps_t) gmp_sprintf_reps,
93 (doprnt_final_t) gmp_sprintf_final
94};