2 Copyright (C) 2001-2008, Parrot Foundation.
7 src/misc.c - Miscellaneous functions
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.
18 The naming convention used is:
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.
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>).
60 PARROT_WARN_UNUSED_RESULT
61 PARROT_CANNOT_RETURN_NULL
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
77 C string version of C<Parrot_vsprintf_s()>.
84 PARROT_CANNOT_RETURN_NULL
86 Parrot_vsprintf_c(PARROT_INTERP
, ARGIN(const char *pat
), va_list args
)
88 ASSERT_ARGS(Parrot_vsprintf_c
)
89 STRING
* const realpat
= string_make(interp
, pat
, strlen(pat
),
90 NULL
, PObj_external_FLAG
);
92 STRING
* const ret
= Parrot_vsprintf_s(interp
, realpat
, args
);
99 =item C<void Parrot_vsnprintf(PARROT_INTERP, char *targ, size_t len, const char
102 Similar to C<Parrot_vsprintf()> but with an option to specify the length
103 (C<len>) of the returned C string.
111 Parrot_vsnprintf(PARROT_INTERP
, ARGOUT(char *targ
),
112 size_t len
, ARGIN(const char *pat
), va_list args
)
114 ASSERT_ARGS(Parrot_vsnprintf
)
119 const STRING
* const ret
= Parrot_vsprintf_c(interp
, pat
, args
);
120 /* string_transcode(interp, ret, NULL, NULL, &ret); */
122 if (len
> ret
->bufused
) {
127 memcpy(targ
, ret
->strstart
, len
);
134 =item C<STRING * Parrot_sprintf_s(PARROT_INTERP, STRING *pat, ...)>
136 Calls C<Parrot_vsprintf_s()> with the C<va_list> obtained from C<...>.
143 PARROT_WARN_UNUSED_RESULT
144 PARROT_CANNOT_RETURN_NULL
146 Parrot_sprintf_s(PARROT_INTERP
, ARGIN(STRING
*pat
), ...)
148 ASSERT_ARGS(Parrot_sprintf_s
)
154 ret
= Parrot_vsprintf_s(interp
, pat
, args
);
163 =item C<STRING * Parrot_sprintf_c(PARROT_INTERP, const char *pat, ...)>
165 C string version of C<Parrot_sprintf_s()>.
172 PARROT_WARN_UNUSED_RESULT
173 PARROT_CANNOT_RETURN_NULL
175 Parrot_sprintf_c(PARROT_INTERP
, ARGIN(const char *pat
), ...)
177 ASSERT_ARGS(Parrot_sprintf_c
)
183 ret
= Parrot_vsprintf_c(interp
, pat
, args
);
192 =item C<void Parrot_snprintf(PARROT_INTERP, char *targ, size_t len, const char
195 Similar to C<Parrot_sprintf()> but with an option to specify the length
196 (C<len>) of the returned C string.
204 Parrot_snprintf(PARROT_INTERP
, ARGOUT(char *targ
), size_t len
,
205 ARGIN(const char *pat
), ...)
207 ASSERT_ARGS(Parrot_snprintf
)
212 Parrot_vsnprintf(interp
, targ
, len
, pat
, args
);
219 =item C<STRING * Parrot_psprintf(PARROT_INTERP, STRING *pat, PMC *ary)>
221 Calls C<Parrot_sprintf_format()> with the insertion arguments in an
229 PARROT_WARN_UNUSED_RESULT
230 PARROT_CANNOT_RETURN_NULL
232 Parrot_psprintf(PARROT_INTERP
, ARGIN(STRING
*pat
), ARGOUT(PMC
*ary
))
234 ASSERT_ARGS(Parrot_psprintf
)
235 SPRINTF_OBJ obj
= pmc_core
;
238 return Parrot_sprintf_format(interp
, pat
, &obj
);
243 =item C<int Parrot_secret_snprintf(char *buffer, const size_t len, const char
246 A simulation of C<snprintf> for systems that do not support it.
255 Parrot_secret_snprintf(ARGOUT(char *buffer
), SHIM(const size_t len
),
256 ARGIN(const char *format
), ...)
258 ASSERT_ARGS(Parrot_secret_snprintf
)
261 va_start(ap
, format
);
262 retval
= vsprintf(buffer
, format
, ap
);
275 F<src/misc.h>, F<src/spf_vtable.c>, F<src/spf_render.c>.
279 This was once a simple, vararg-based implementation that existed
280 completely within this file. When the file grew to be nearly 1000
281 lines long, I split it into three. --BD
290 * c-file-style: "parrot"
292 * vim: expandtab shiftwidth=4: