parrotcode: Miscellaneous functions | |
Contents | C |
src/misc.c - Miscellaneous functions
Miscellaneous functions,
mainly the Parrot_sprintf
family.
Uses a generalized formatting algorithm (src/spf_render.c) with a specialized vtable (src/spf_vtable.c) to handle argument extraction.
The naming convention used is:
Parrot_v?n?sprintf
Parrot_v?sprintf_c
Parrot_v?sprintf_s
_
means "returns Parrot string" and the other letter indicates the type for the format.*/
#define IN_SPF_SYSTEM
#include "parrot/parrot.h"
/* HEADER: include/parrot/misc.h */
/*
FUNCDOC: Parrot_vsprintf_s
Almost all the other sprintf variants in this file are implemented in terms of this function (see Parrot_psprintf()
for the exception).
It in turn calls Parrot_sprintf_format()
(see src/spf_render.c).
*/
PARROT_API STRING * Parrot_vsprintf_s(Interp *interp /*NN*/, STRING *pat /*NN*/, va_list args) /* WARN_UNUSED */ { SPRINTF_OBJ obj = va_core; obj.data = PARROT_VA_TO_VAPTR(args);
return Parrot_sprintf_format(interp, pat, &obj);
}
/*
FUNCDOC: Parrot_vsprintf_c
C string version of Parrot_vsprintf_s()
.
*/
PARROT_API STRING * Parrot_vsprintf_c(Interp *interp /*NN*/, const char *pat /*NN*/, va_list args) /* WARN_UNUSED */ { STRING * const realpat = string_make(interp, pat, strlen(pat), NULL, PObj_external_FLAG);
STRING * const ret = Parrot_vsprintf_s(interp, realpat, args);
return ret;
}
/*
FUNCDOC: Parrot_vsnprintf
Similar to Parrot_vsprintf()
but with an option to specify the length (len
) of the returned C string.
*/
PARROT_API void Parrot_vsnprintf(Interp *interp /*NN*/, char *targ /*NN*/, size_t len, const char *pat /*NN*/, va_list args) { if (len == 0) return; len--; if (len) { const STRING * const ret = Parrot_vsprintf_c(interp, pat, args); /* string_transcode(interp, ret, NULL, NULL, &ret); */
if (len > ret->bufused) {
len = ret->bufused;
}
if (len)
memcpy(targ, ret->strstart, len);
}
targ[len] = 0;
}
/*
FUNCDOC: Parrot_sprintf_s
Calls Parrot_vsprintf_s()
with the va_list
obtained from ...
.
*/
PARROT_API STRING * Parrot_sprintf_s(Interp *interp /*NN*/, STRING *pat /*NN*/, ...) /* WARN_UNUSED */ { STRING *ret; va_list args;
va_start(args, pat);
ret = Parrot_vsprintf_s(interp, pat, args);
va_end(args);
return ret;
}
/*
FUNCDOC: Parrot_sprintf_c
C string version of Parrot_sprintf_s()
.
*/
PARROT_API STRING * Parrot_sprintf_c(Interp *interp /*NN*/, const char *pat /*NN*/, ...) /* WARN_UNUSED */ { STRING *ret; va_list args;
va_start(args, pat);
ret = Parrot_vsprintf_c(interp, pat, args);
va_end(args);
return ret;
}
/*
FUNCDOC: Parrot_snprintf
Similar to Parrot_sprintf()
but with an option to specify the length (len
) of the returned C string.
*/
PARROT_API void Parrot_snprintf(Interp *interp /*NN*/, char *targ /*NN*/, size_t len, const char *pat /*NN*/, ...) { va_list args;
va_start(args, pat);
Parrot_vsnprintf(interp, targ, len, pat, args);
va_end(args);
}
/*
FUNCDOC: Parrot_psprintf
Calls Parrot_sprintf_format()
with the insertion arguments in an Array
PMC.
*/
PARROT_API STRING * Parrot_psprintf(Interp *interp /*NN*/, STRING *pat /*NN*/, PMC *ary /*NN*/) /* WARN_UNUSED */ { SPRINTF_OBJ obj = pmc_core; obj.data = ary;
return Parrot_sprintf_format(interp, pat, &obj);
}
/*
src/misc.h, src/spf_vtable.c, src/spf_render.c.
This was once a simple, vararg-based implementation that existed completely within this file. When the file grew to be nearly 1,000 lines long, I split it into three. --BD
*/
/* * Local variables: * c-file-style: "parrot" * End: * vim: expandtab shiftwidth=4: */
|