fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / src / misc.c
blob7edbae56086acce84070c5f9f92a6eb8d14a1459
1 /*
2 Copyright (C) 2001-2010, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/misc.c - Miscellaneous functions
9 =head1 DESCRIPTION
11 Miscellaneous functions, mainly the C<Parrot_sprintf> family.
13 Uses a generalized formatting algorithm (F<src/spf_render.c>) with
14 a specialized vtable (F<src/spf_vtable.c>) to handle argument extraction.
16 =head2 Functions
18 The naming convention used is:
20 =over 4
22 =item C<Parrot_v?n?sprintf>
24 A (nearly) drop-in replacement for v?n?sprintf.
26 =item C<Parrot_v?sprintf_c>
28 Takes a C-string format, returns a Parrot string.
30 =item C<Parrot_v?sprintf_s>
32 Takes a Parrot string format, returns a Parrot string.
34 So the C<_> means "returns Parrot string" and the other letter indicates
35 the type for the format.
37 =cut
41 #define IN_SPF_SYSTEM
43 #include "parrot/parrot.h"
45 /* HEADERIZER HFILE: include/parrot/misc.h */
49 =item C<STRING * Parrot_vsprintf_s(PARROT_INTERP, STRING *pat, va_list args)>
51 Almost all the other sprintf variants in this file are implemented in
52 terms of this function (see C<Parrot_psprintf()> for the exception). It
53 in turn calls C<Parrot_sprintf_format()> (see F<src/spf_render.c>).
55 =cut
59 PARROT_EXPORT
60 PARROT_WARN_UNUSED_RESULT
61 PARROT_CANNOT_RETURN_NULL
62 STRING *
63 Parrot_vsprintf_s(PARROT_INTERP, ARGIN(STRING *pat), va_list args)
65 ASSERT_ARGS(Parrot_vsprintf_s)
66 SPRINTF_OBJ obj = va_core;
67 obj.data = PARROT_VA_TO_VAPTR(args);
69 return Parrot_sprintf_format(interp, pat, &obj);
74 =item C<STRING * Parrot_vsprintf_c(PARROT_INTERP, const char *pat, va_list
75 args)>
77 C string version of C<Parrot_vsprintf_s()>.
79 =cut
83 PARROT_EXPORT
84 PARROT_CANNOT_RETURN_NULL
85 PARROT_WARN_UNUSED_RESULT
86 STRING *
87 Parrot_vsprintf_c(PARROT_INTERP, ARGIN(const char *pat), va_list args)
89 ASSERT_ARGS(Parrot_vsprintf_c)
90 STRING * const realpat = Parrot_str_new_init(interp, pat, strlen(pat),
91 Parrot_default_encoding_ptr, PObj_external_FLAG);
93 STRING * const ret = Parrot_vsprintf_s(interp, realpat, args);
95 return ret;
100 =item C<void Parrot_vsnprintf(PARROT_INTERP, char *targ, size_t len, const char
101 *pat, va_list args)>
103 Similar to C<Parrot_vsprintf()> but with an option to specify the length
104 (C<len>) of the returned C string.
106 =cut
110 PARROT_EXPORT
111 void
112 Parrot_vsnprintf(PARROT_INTERP, ARGOUT(char *targ),
113 size_t len, ARGIN(const char *pat), va_list args)
115 ASSERT_ARGS(Parrot_vsnprintf)
117 if (len == 0)
118 return;
119 --len;
120 if (len) {
121 const STRING * const ret = Parrot_vsprintf_c(interp, pat, args);
122 /* string_transcode(interp, ret, NULL, NULL, &ret); */
124 char * const str_ret = Parrot_str_to_cstring(interp, ret);
125 const size_t str_len = strlen(str_ret);
126 if (len > str_len) {
127 len = str_len;
130 if (len)
131 memcpy(targ, str_ret, len);
132 Parrot_str_free_cstring(str_ret);
134 targ[len] = 0;
139 =item C<STRING * Parrot_sprintf_s(PARROT_INTERP, STRING *pat, ...)>
141 Calls C<Parrot_vsprintf_s()> with the C<va_list> obtained from C<...>.
143 =cut
147 PARROT_EXPORT
148 PARROT_WARN_UNUSED_RESULT
149 PARROT_CANNOT_RETURN_NULL
150 STRING *
151 Parrot_sprintf_s(PARROT_INTERP, ARGIN(STRING *pat), ...)
153 ASSERT_ARGS(Parrot_sprintf_s)
154 STRING *ret;
155 va_list args;
157 va_start(args, pat);
159 ret = Parrot_vsprintf_s(interp, pat, args);
161 va_end(args);
163 return ret;
168 =item C<STRING * Parrot_sprintf_c(PARROT_INTERP, const char *pat, ...)>
170 C string version of C<Parrot_sprintf_s()>.
172 =cut
176 PARROT_EXPORT
177 PARROT_WARN_UNUSED_RESULT
178 PARROT_CANNOT_RETURN_NULL
179 STRING *
180 Parrot_sprintf_c(PARROT_INTERP, ARGIN(const char *pat), ...)
182 ASSERT_ARGS(Parrot_sprintf_c)
183 STRING *ret;
184 va_list args;
186 va_start(args, pat);
188 ret = Parrot_vsprintf_c(interp, pat, args);
190 va_end(args);
192 return ret;
197 =item C<void Parrot_snprintf(PARROT_INTERP, char *targ, size_t len, const char
198 *pat, ...)>
200 Similar to C<Parrot_sprintf()> but with an option to specify the length
201 (C<len>) of the returned C string.
203 =cut
207 PARROT_EXPORT
208 void
209 Parrot_snprintf(PARROT_INTERP, ARGOUT(char *targ), size_t len,
210 ARGIN(const char *pat), ...)
212 ASSERT_ARGS(Parrot_snprintf)
213 va_list args;
215 va_start(args, pat);
217 Parrot_vsnprintf(interp, targ, len, pat, args);
219 va_end(args);
224 =item C<STRING * Parrot_psprintf(PARROT_INTERP, STRING *pat, PMC *ary)>
226 Calls C<Parrot_sprintf_format()> with the insertion arguments in an
227 C<Array> PMC.
229 =cut
233 PARROT_EXPORT
234 PARROT_WARN_UNUSED_RESULT
235 PARROT_CANNOT_RETURN_NULL
236 STRING *
237 Parrot_psprintf(PARROT_INTERP, ARGIN(STRING *pat), ARGOUT(PMC *ary))
239 ASSERT_ARGS(Parrot_psprintf)
240 SPRINTF_OBJ obj = pmc_core;
241 obj.data = ary;
243 return Parrot_sprintf_format(interp, pat, &obj);
248 =item C<int Parrot_secret_snprintf(char *buffer, const size_t len, const char
249 *format, ...)>
251 A simulation of C<snprintf> for systems that do not support it.
253 =cut
258 PARROT_EXPORT
259 PARROT_IGNORABLE_RESULT
261 Parrot_secret_snprintf(ARGOUT(char *buffer), SHIM(const size_t len),
262 ARGIN(const char *format), ...)
264 ASSERT_ARGS(Parrot_secret_snprintf)
265 int retval;
266 va_list ap;
267 va_start(ap, format);
268 retval = vsprintf(buffer, format, ap);
269 va_end(ap);
271 return retval;
277 =back
279 =head1 SEE ALSO
281 F<src/misc.h>, F<src/spf_vtable.c>, F<src/spf_render.c>.
283 =head1 HISTORY
285 This was once a simple, vararg-based implementation that existed
286 completely within this file. When the file grew to be nearly 1000
287 lines long, I split it into three. --BD
289 =cut
295 * Local variables:
296 * c-file-style: "parrot"
297 * End:
298 * vim: expandtab shiftwidth=4: