2 Copyright (C) 2001-2010, 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
85 PARROT_WARN_UNUSED_RESULT
87 Parrot_vsprintf_c(PARROT_INTERP
, ARGIN(const char *pat
), va_list args
)
89 ASSERT_ARGS(Parrot_vsprintf_c
)
90 STRING
* const realpat
= string_make(interp
, pat
, strlen(pat
),
91 NULL
, PObj_external_FLAG
);
93 STRING
* const ret
= Parrot_vsprintf_s(interp
, realpat
, args
);
100 =item C<void Parrot_vsnprintf(PARROT_INTERP, char *targ, size_t len, const char
103 Similar to C<Parrot_vsprintf()> but with an option to specify the length
104 (C<len>) of the returned C string.
112 Parrot_vsnprintf(PARROT_INTERP
, ARGOUT(char *targ
),
113 size_t len
, ARGIN(const char *pat
), va_list args
)
115 ASSERT_ARGS(Parrot_vsnprintf
)
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
);
131 memcpy(targ
, str_ret
, len
);
132 Parrot_str_free_cstring(str_ret
);
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<...>.
148 PARROT_WARN_UNUSED_RESULT
149 PARROT_CANNOT_RETURN_NULL
151 Parrot_sprintf_s(PARROT_INTERP
, ARGIN(STRING
*pat
), ...)
153 ASSERT_ARGS(Parrot_sprintf_s
)
159 ret
= Parrot_vsprintf_s(interp
, pat
, args
);
168 =item C<STRING * Parrot_sprintf_c(PARROT_INTERP, const char *pat, ...)>
170 C string version of C<Parrot_sprintf_s()>.
177 PARROT_WARN_UNUSED_RESULT
178 PARROT_CANNOT_RETURN_NULL
180 Parrot_sprintf_c(PARROT_INTERP
, ARGIN(const char *pat
), ...)
182 ASSERT_ARGS(Parrot_sprintf_c
)
188 ret
= Parrot_vsprintf_c(interp
, pat
, args
);
197 =item C<void Parrot_snprintf(PARROT_INTERP, char *targ, size_t len, const char
200 Similar to C<Parrot_sprintf()> but with an option to specify the length
201 (C<len>) of the returned C string.
209 Parrot_snprintf(PARROT_INTERP
, ARGOUT(char *targ
), size_t len
,
210 ARGIN(const char *pat
), ...)
212 ASSERT_ARGS(Parrot_snprintf
)
217 Parrot_vsnprintf(interp
, targ
, len
, pat
, 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
234 PARROT_WARN_UNUSED_RESULT
235 PARROT_CANNOT_RETURN_NULL
237 Parrot_psprintf(PARROT_INTERP
, ARGIN(STRING
*pat
), ARGOUT(PMC
*ary
))
239 ASSERT_ARGS(Parrot_psprintf
)
240 SPRINTF_OBJ obj
= pmc_core
;
243 return Parrot_sprintf_format(interp
, pat
, &obj
);
248 =item C<int Parrot_secret_snprintf(char *buffer, const size_t len, const char
251 A simulation of C<snprintf> for systems that do not support it.
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
)
267 va_start(ap
, format
);
268 retval
= vsprintf(buffer
, format
, ap
);
281 F<src/misc.h>, F<src/spf_vtable.c>, F<src/spf_render.c>.
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
296 * c-file-style: "parrot"
298 * vim: expandtab shiftwidth=4: