/* */
/* */
/* (C) 2000-2004 Ullrich von Bassewitz */
-/* Römerstrasse 52 */
+/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
/* */
/* The following is a very basic vsnprintf like function called xvsnprintf. It
- * features only the basic format specifiers (especially the floating point
- * stuff is missing), but may be extended if required. Reason for supplying
- * my own implementation is that vsnprintf is standard but not implemented by
- * older compilers, and some that implement it, don't adhere to the standard
- * (for example Microsoft with its _vsnprintf).
- */
+** features only the basic format specifiers (especially the floating point
+** stuff is missing), but may be extended if required. Reason for supplying
+** my own implementation is that vsnprintf is standard but not implemented by
+** older compilers, and some that implement it, don't adhere to the standard
+** (for example Microsoft with its _vsnprintf).
+*/
typedef struct {
size_t BufFill;
/* Argument string buffer and string buffer pointer. The string buffer
- * must be big enough to hold a converted integer of the largest type
- * including an optional sign and terminating zero.
- */
+ ** must be big enough to hold a converted integer of the largest type
+ ** including an optional sign and terminating zero.
+ */
char ArgBuf[256];
int ArgLen;
ToStr (P, Val);
/* The default precision for all integer conversions is one. This means
- * that the fPrec flag is always set and does not need to be checked
- * later on.
- */
+ ** that the fPrec flag is always set and does not need to be checked
+ ** later on.
+ */
if ((P->Flags & fPrec) == 0) {
P->Flags |= fPrec;
P->Prec = 1;
unsigned WidthPadding;
/* Get the string length limited to the precision. Beware: We cannot use
- * strlen here, because if a precision is given, the string may not be
- * zero terminated.
- */
+ ** strlen here, because if a precision is given, the string may not be
+ ** zero terminated.
+ */
int Len;
if ((P->Flags & fPrec) != 0) {
const char* S = memchr (Val, '\0', P->Prec);
/* Store the current output offset (%n format spec) */
{
switch (P->LengthMod) {
- case lmChar: *va_arg (P->ap, int*) = P->BufFill;
- case lmShort: *va_arg (P->ap, int*) = P->BufFill;
- case lmInt: *va_arg (P->ap, int*) = P->BufFill;
- case lmLong: *va_arg (P->ap, long*) = P->BufFill;
- case lmIntMax: *va_arg (P->ap, intmax_t*) = P->BufFill;
- case lmSizeT: *va_arg (P->ap, size_t*) = P->BufFill;
- case lmPtrDiffT: *va_arg (P->ap, ptrdiff_t*) = P->BufFill;
- default: FAIL ("Invalid size modifier for %n format spec in xvsnprintf");
+ case lmChar: *va_arg (P->ap, int*) = P->BufFill; break;
+ case lmShort: *va_arg (P->ap, int*) = P->BufFill; break;
+ case lmInt: *va_arg (P->ap, int*) = P->BufFill; break;
+ case lmLong: *va_arg (P->ap, long*) = P->BufFill; break;
+ case lmIntMax: *va_arg (P->ap, intmax_t*) = P->BufFill; break;
+ case lmSizeT: *va_arg (P->ap, size_t*) = P->BufFill; break;
+ case lmPtrDiffT: *va_arg (P->ap, ptrdiff_t*) = P->BufFill; break;
+ default: FAIL ("Invalid size modifier for %n format spec. in xvsnprintf()");
}
}
int xvsnprintf (char* Buf, size_t Size, const char* Format, va_list ap)
/* A basic vsnprintf implementation. Does currently only support integer
- * formats.
- */
+** formats.
+*/
{
PrintfCtrl P;
int Done;
if (F == '*') {
P.Width = va_arg (P.ap, int);
/* A negative field width argument is taken as a - flag followed
- * by a positive field width.
- */
+ ** by a positive field width.
+ */
if (P.Width < 0) {
P.Flags |= fMinus;
P.Width = -P.Width;
if (F == '*') {
P.Prec = va_arg (P.ap, int);
/* A negative precision argument is taken as if the precision
- * were omitted.
- */
+ ** were omitted.
+ */
if (P.Prec < 0) {
P.Flags &= ~fPrec;
}
}
} else if (F == '-') {
/* A negative precision argument is taken as if the precision
- * were omitted.
- */
+ ** were omitted.
+ */
F = *Format++; /* Skip the minus */
while (IsDigit (F = *Format++)) ;
P.Flags &= ~fPrec;
/* Handle the length by using a precision */
if ((P.Flags & fPrec) != 0) {
/* Precision already specified, use length of string
- * if less.
- */
+ ** if less.
+ */
if ((unsigned) P.Prec > SB_GetLen (S)) {
P.Prec = SB_GetLen (S);
}
va_end (P.ap);
/* Terminate the output string and return the number of chars that had
- * been written if the buffer was large enough.
- * Beware: The terminating zero is not counted for the function result!
- */
+ ** been written if the buffer was large enough.
+ ** Beware: The terminating zero is not counted for the function result!
+ */
AddChar (&P, '\0');
return P.BufFill - 1;
}
int xsnprintf (char* Buf, size_t Size, const char* Format, ...)
/* A basic snprintf implementation. Does currently only support integer
- * formats.
- */
+** formats.
+*/
{
int Res;
va_list ap;
CHECK (Res >= 0 && (unsigned) (Res+1) < BufSize);
return Res;
}
-
-
-