| 1 |
/* GLIB - Library of useful routines for C programming |
| 2 |
* Copyright (C) 2003 Matthias Clasen |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, write to the |
| 16 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 |
* Boston, MA 02111-1307, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* Modified by the GLib Team and others 2003. See the AUTHORS |
| 22 |
* file for a list of people on the GLib Team. See the ChangeLog |
| 23 |
* files for a list of changes. These files are distributed with |
| 24 |
* GLib at ftp://ftp.gtk.org/pub/gtk/. |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifdef HAVE_CONFIG_H |
| 28 |
# include <config.h> |
| 29 |
#endif |
| 30 |
#include <string.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include "gst-printf.h" |
| 34 |
#include "vasnprintf.h" |
| 35 |
#include "printf.h" |
| 36 |
|
| 37 |
#if 0 |
| 38 |
int |
| 39 |
__gst_printf (char const *format, ...) |
| 40 |
{ |
| 41 |
va_list args; |
| 42 |
int retval; |
| 43 |
|
| 44 |
va_start (args, format); |
| 45 |
retval = __gst_vprintf (format, args); |
| 46 |
va_end (args); |
| 47 |
|
| 48 |
return retval; |
| 49 |
} |
| 50 |
|
| 51 |
int |
| 52 |
__gst_fprintf (FILE * file, char const *format, ...) |
| 53 |
{ |
| 54 |
va_list args; |
| 55 |
int retval; |
| 56 |
|
| 57 |
va_start (args, format); |
| 58 |
retval = __gst_vfprintf (file, format, args); |
| 59 |
va_end (args); |
| 60 |
|
| 61 |
return retval; |
| 62 |
} |
| 63 |
|
| 64 |
int |
| 65 |
__gst_sprintf (char *string, char const *format, ...) |
| 66 |
{ |
| 67 |
va_list args; |
| 68 |
int retval; |
| 69 |
|
| 70 |
va_start (args, format); |
| 71 |
retval = __gst_vsprintf (string, format, args); |
| 72 |
va_end (args); |
| 73 |
|
| 74 |
return retval; |
| 75 |
} |
| 76 |
|
| 77 |
int |
| 78 |
__gst_snprintf (char *string, size_t n, char const *format, ...) |
| 79 |
{ |
| 80 |
va_list args; |
| 81 |
int retval; |
| 82 |
|
| 83 |
va_start (args, format); |
| 84 |
retval = __gst_vsnprintf (string, n, format, args); |
| 85 |
va_end (args); |
| 86 |
|
| 87 |
return retval; |
| 88 |
} |
| 89 |
|
| 90 |
int |
| 91 |
__gst_vprintf (char const *format, va_list args) |
| 92 |
{ |
| 93 |
return __gst_vfprintf (stdout, format, args); |
| 94 |
} |
| 95 |
|
| 96 |
int |
| 97 |
__gst_vfprintf (FILE * file, char const *format, va_list args) |
| 98 |
{ |
| 99 |
char *result; |
| 100 |
size_t length; |
| 101 |
|
| 102 |
result = vasnprintf (NULL, &length, format, args); |
| 103 |
if (result == NULL) |
| 104 |
return -1; |
| 105 |
|
| 106 |
fwrite (result, 1, length, file); |
| 107 |
free (result); |
| 108 |
|
| 109 |
return length; |
| 110 |
} |
| 111 |
|
| 112 |
int |
| 113 |
__gst_vsprintf (char *string, char const *format, va_list args) |
| 114 |
{ |
| 115 |
char *result; |
| 116 |
size_t length; |
| 117 |
|
| 118 |
result = vasnprintf (NULL, &length, format, args); |
| 119 |
if (result == NULL) |
| 120 |
return -1; |
| 121 |
|
| 122 |
memcpy (string, result, length + 1); |
| 123 |
free (result); |
| 124 |
|
| 125 |
return length; |
| 126 |
} |
| 127 |
|
| 128 |
int |
| 129 |
__gst_vsnprintf (char *string, size_t n, char const *format, va_list args) |
| 130 |
{ |
| 131 |
char *result; |
| 132 |
size_t length; |
| 133 |
|
| 134 |
result = vasnprintf (NULL, &length, format, args); |
| 135 |
if (result == NULL) |
| 136 |
return -1; |
| 137 |
|
| 138 |
if (n > 0) { |
| 139 |
memcpy (string, result, MIN (length + 1, n)); |
| 140 |
string[n - 1] = 0; |
| 141 |
} |
| 142 |
|
| 143 |
free (result); |
| 144 |
|
| 145 |
return length; |
| 146 |
} |
| 147 |
#endif |
| 148 |
|
| 149 |
int |
| 150 |
__gst_vasprintf (char **result, char const *format, va_list args) |
| 151 |
{ |
| 152 |
size_t length; |
| 153 |
|
| 154 |
*result = vasnprintf (NULL, &length, format, args); |
| 155 |
if (*result == NULL) |
| 156 |
return -1; |
| 157 |
|
| 158 |
return length; |
| 159 |
} |
| 160 |
|
| 161 |
/* Format into fixed_buf if the result fits, otherwise into a freshly malloc'd |
| 162 |
* buffer. The caller must check whether *result == fixed_buf to know whether |
| 163 |
* to free it. */ |
| 164 |
int |
| 165 |
__gst_vasprintf_buf (char **result, char *fixed_buf, size_t fixed_buf_size, |
| 166 |
char const *format, va_list args) |
| 167 |
{ |
| 168 |
size_t length = fixed_buf_size; |
| 169 |
|
| 170 |
*result = vasnprintf (fixed_buf, &length, format, args); |
| 171 |
if (*result == NULL) |
| 172 |
return -1; |
| 173 |
|
| 174 |
return length; |
| 175 |
} |