xref: /dragonfly/contrib/gmp/printf/sprintffuns.c (revision 5fb3968e)
1 /* __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 
7 Copyright 2001 Free Software Foundation, Inc.
8 
9 This file is part of the GNU MP Library.
10 
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15 
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20 
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
23 
24 #include "config.h"
25 
26 #if HAVE_STDARG
27 #include <stdarg.h>
28 #else
29 #include <varargs.h>
30 #endif
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "gmp.h"
37 #include "gmp-impl.h"
38 
39 
40 /* The data parameter "bufp" points to a "char *buf" which is the next
41    character to be written, having started as the destination from the
42    application.  This is then increased each time output is produced.  */
43 
44 
45 /* If vsprintf returns -1 then pass it upwards.  It doesn't matter that
46    "*bufp" is ruined in this case, since gmp_doprint will bail out
47    immediately anyway.  */
48 static int
49 gmp_sprintf_format (char **bufp, const char *fmt, va_list ap)
50 {
51   char  *buf = *bufp;
52   int   ret;
53   vsprintf (buf, fmt, ap);
54   ret = strlen (buf);
55   *bufp = buf + ret;
56   return ret;
57 }
58 
59 static int
60 gmp_sprintf_memory (char **bufp, const char *str, size_t len)
61 {
62   char  *buf = *bufp;
63   *bufp = buf + len;
64   memcpy (buf, str, len);
65   return len;
66 }
67 
68 static int
69 gmp_sprintf_reps (char **bufp, int c, int reps)
70 {
71   char  *buf = *bufp;
72   ASSERT (reps >= 0);
73   *bufp = buf + reps;
74   memset (buf, c, reps);
75   return reps;
76 }
77 
78 static int
79 gmp_sprintf_final (char **bufp, int c, int reps)
80 {
81   char  *buf = *bufp;
82   *buf = '\0';
83   return 0;
84 }
85 
86 const struct doprnt_funs_t  __gmp_sprintf_funs = {
87   (doprnt_format_t) gmp_sprintf_format,
88   (doprnt_memory_t) gmp_sprintf_memory,
89   (doprnt_reps_t)   gmp_sprintf_reps,
90   (doprnt_final_t)  gmp_sprintf_final
91 };
92