1 /* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
3 Copyright (c) 2014-2015 Datalight, Inc.
4 All Rights Reserved Worldwide.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; use version 2 of the License.
10 This program is distributed in the hope that it will be useful,
11 but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /* Businesses and individuals that for commercial or other reasons cannot
20 comply with the terms of the GPLv2 license may obtain a commercial license
21 before incorporating Reliance Edge into proprietary software for
22 distribution in any form. Visit http://www.datalight.com/reliance-edge for
26 @brief Implements functions for printing.
28 These functions are intended to be used in portable test code, which cannot
29 assume the standard I/O functions will be available. Similar to their ANSI
30 C counterparts, these functions allow formatting text strings and (if the
31 configuration allows it) outputing formatted text. The latter ability
32 relies on the RedOsOutputString() OS service function.
34 Do *not* use these functions in code which can safely assume the standard
35 I/O functions are available (e.g., in host tools code).
37 Do *not* use these functions from within the file system driver. These
38 functions use variable arguments and thus are not MISRA-C:2012 compliant.
41 #include <redtestutils.h>
46 /** @brief Maximum number of bytes of output supported by RedPrintf().
48 Typically only Datalight code uses these functions, and that could should be
49 written to respect this limit, so it should not normally be necessary to
52 #define OUTPUT_BUFFER_SIZE 256U
78 PRINTTYPE type; /* The PRFMT_* type found */
79 uint32_t ulSpecifierIdx; /* Returns a pointer to the % sign */
83 bool fHasIllegalType; /* TRUE if an illegal sequence was skipped over */
88 /* Our output handlers are written for standard fixed width data types. Map
89 the standard ANSI C data types onto our handlers. Currently this code has
90 the following requirements:
92 1) shorts must be either 16 or 32 bits
93 2) ints must be either 16 or 32 bits
94 3) longs must be between 32 or 64 bits
95 4) long longs must be 64 bits
97 #if (USHRT_MAX == 0xFFFFU)
98 #define MAPSHORT PRFMT_SIGNED16BIT
99 #define MAPUSHORT PRFMT_UNSIGNED16BIT
100 #define MAPHEXUSHORT PRFMT_HEX16BIT
101 #elif (USHRT_MAX == 0xFFFFFFFFU)
102 #define MAPSHORT PRFMT_SIGNED32BIT
103 #define MAPUSHORT PRFMT_UNSIGNED32BIT
104 #define MAPHEXUSHORT PRFMT_HEX32BIT
106 #error "The 'short' data type does not have a 16 or 32-bit width"
109 #if (UINT_MAX == 0xFFFFU)
110 #define MAPINT PRFMT_SIGNED16BIT
111 #define MAPUINT PRFMT_UNSIGNED16BIT
112 #define MAPHEXUINT PRFMT_HEX16BIT
113 #elif (UINT_MAX == 0xFFFFFFFFU)
114 #define MAPINT PRFMT_SIGNED32BIT
115 #define MAPUINT PRFMT_UNSIGNED32BIT
116 #define MAPHEXUINT PRFMT_HEX32BIT
118 #error "The 'int' data type does not have a 16 or 32-bit width"
121 #if (ULONG_MAX == 0xFFFFFFFFU)
122 #define MAPLONG PRFMT_SIGNED32BIT
123 #define MAPULONG PRFMT_UNSIGNED32BIT
124 #define MAPHEXULONG PRFMT_HEX32BIT
125 #elif (ULONG_MAX <= UINT64_SUFFIX(0xFFFFFFFFFFFFFFFF))
126 /* We've run into unusual environments where "longs" are 40-bits wide.
127 In this event, map them to 64-bit types so no data is lost.
129 #define MAPLONG PRFMT_SIGNED64BIT
130 #define MAPULONG PRFMT_UNSIGNED64BIT
131 #define MAPHEXULONG PRFMT_HEX64BIT
133 #error "The 'long' data type is not between 32 and 64 bits wide"
136 #if defined(ULLONG_MAX) && (ULLONG_MAX != UINT64_SUFFIX(0xFFFFFFFFFFFFFFFF))
137 #error "The 'long long' data type is not 64 bits wide"
139 #define MAPLONGLONG PRFMT_SIGNED64BIT
140 #define MAPULONGLONG PRFMT_UNSIGNED64BIT
141 #define MAPHEXULONGLONG PRFMT_HEX64BIT
145 static uint32_t ProcessFormatSegment(char *pcBuffer, uint32_t ulBufferLen, const char *pszFormat, PRINTFORMAT *pFormat, uint32_t *pulSpecifierLen);
146 static uint32_t ParseFormatSpecifier(char const *pszFormat, PRINTFORMAT *pFormatType);
147 static PRINTTYPE ParseFormatType(const char *pszFormat, uint32_t *pulTypeLen);
148 static uint32_t LtoA(char *pcBuffer, uint32_t ulBufferLen, int32_t lNum, uint32_t ulFillLen, char cFill);
149 static uint32_t LLtoA(char *pcBuffer, uint32_t ulBufferLen, int64_t llNum, uint32_t ulFillLen, char cFill);
150 static uint32_t ULtoA(char *pcBuffer, uint32_t ulBufferLen, uint32_t ulNum, bool fHex, uint32_t ulFillLen, char cFill);
151 static uint32_t ULLtoA(char *pcBuffer, uint32_t ulBufferLen, uint64_t ullNum, bool fHex, uint32_t ulFillLen, char cFill);
152 static uint32_t FinishToA(const char *pcDigits, uint32_t ulDigits, char *pcOutBuffer, uint32_t ulBufferLen, uint32_t ulFillLen, char cFill);
155 /* Digits for the *LtoA() routines.
157 static const char gacDigits[] = "0123456789ABCDEF";
160 #if REDCONF_OUTPUT == 1
161 /** @brief Print formatted data with a variable length argument list.
163 This function provides a subset of the ANSI C printf() functionality with
164 several extensions to support fixed size data types.
166 See RedVSNPrintf() for the list of supported types.
168 @param pszFormat A pointer to the null-terminated format string.
169 @param ... The variable length argument list.
172 const char *pszFormat,
177 va_start(arglist, pszFormat);
179 RedVPrintf(pszFormat, arglist);
185 /** @brief Print formatted data using a pointer to a variable length argument
188 This function provides a subset of the ANSI C vprintf() functionality.
190 See RedVSNPrintf() for the list of supported types.
192 This function accommodates a maximum output length of #OUTPUT_BUFFER_SIZE.
193 If this function must truncate the output, and the original string was
194 \n terminated, the truncated output will be \n terminated as well.
196 @param pszFormat A pointer to the null-terminated format string.
197 @param arglist The variable length argument list.
200 const char *pszFormat,
203 char achBuffer[OUTPUT_BUFFER_SIZE];
205 if(RedVSNPrintf(achBuffer, sizeof(achBuffer), pszFormat, arglist) == -1)
207 /* Ensture the buffer is null terminated.
209 achBuffer[sizeof(achBuffer) - 1U] = '\0';
211 /* If the original string was \n terminated and the new one is not, due to
212 truncation, stuff a \n into the new one.
214 if(pszFormat[RedStrLen(pszFormat) - 1U] == '\n')
216 achBuffer[sizeof(achBuffer) - 2U] = '\n';
220 RedOsOutputString(achBuffer);
222 #endif /* #if REDCONF_OUTPUT == 1 */
225 /** @brief Format arguments into a string using a subset of the ANSI C
226 vsprintf() functionality.
228 This function is modeled after the Microsoft _snprint() extension to the
229 ANSI C sprintf() function, and allows a buffer length to be specified so
230 that overflow is avoided.
232 See RedVSNPrintf() for the list of supported types.
234 @param pcBuffer A pointer to the output buffer
235 @param ulBufferLen The output buffer length
236 @param pszFormat A pointer to the null terminated format string
237 @param ... Variable argument list
239 @return The length output, or -1 if the buffer filled up. If -1 is
240 returned, the output buffer may not be null-terminated.
244 uint32_t ulBufferLen,
245 const char *pszFormat,
251 va_start(arglist, pszFormat);
253 iLen = RedVSNPrintf(pcBuffer, ulBufferLen, pszFormat, arglist);
261 /** @brief Format arguments into a string using a subset of the ANSI C
262 vsprintf() functionality.
264 This function is modeled after the Microsoft _vsnprint() extension to the
265 ANSI C vsprintf() function, and requires a buffer length to be specified so
266 that overflow is avoided.
268 The following ANSI C standard formatting codes are supported:
271 | ---- | ---------------------------------- |
272 | %c | Format a character |
273 | %s | Format a null-terminated C string |
274 | %hd | Format a signed short |
275 | %hu | Format an unsigned short |
276 | %d | Format a signed integer |
277 | %u | Format an unsigned integer |
278 | %ld | Format a signed long |
279 | %lu | Format an unsigned long |
280 | %lld | Format a signed long long |
281 | %llu | Format an unsigned long long |
282 | %hx | Format a short in hex |
283 | %x | Format an integer in hex |
284 | %lx | Format a long in hex |
285 | %llx | Format a long long in hex |
286 | %p | Format a pointer (hex value) |
288 @note All formatting codes are case-sensitive.
290 Fill characters and field widths are supported per the ANSI standard, as is
291 left justification with the '-' character.
293 The only supported fill characters are '0', ' ', and '_'.
295 '*' is supported to specify variable length field widths.
297 Hexidecimal numbers are always displayed in upper case. Formatting codes
298 which specifically request upper case (e.g., "%lX") are not supported.
300 Unsupported behaviors:
301 - Precision is not supported.
302 - Floating point is not supported.
305 - There is a subtle difference in the return value for this function versus
306 the Microsoft implementation. In the Microsoft version, if the buffer
307 exactly fills up, but there is no room for a null-terminator, the return
308 value will be the length of the buffer. In this code, -1 will be returned
310 - When using left justified strings, the only supported fill character is a
311 space, regardless of what may be specified. It is not clear if this is
312 ANSI standard or just the way the Microsoft function works, but we emulate
313 the Microsoft behavior.
315 @param pcBuffer A pointer to the output buffer.
316 @param ulBufferLen The output buffer length.
317 @param pszFormat A pointer to the null terminated ANSI format string.
318 @param arglist Variable argument list.
320 @return The length output, or -1 if the buffer filled up. If -1 is
321 returned, the output buffer may not be null-terminated.
323 int32_t RedVSNPrintf(
325 uint32_t ulBufferLen,
326 const char *pszFormat,
329 uint32_t ulBufIdx = 0U;
330 uint32_t ulFmtIdx = 0U;
333 while((pszFormat[ulFmtIdx] != '\0') && (ulBufIdx < ulBufferLen))
336 uint32_t ulSpecifierLen;
339 /* Process the next segment of the format string, outputting
340 any non-format specifiers, as output buffer space allows,
341 and return information about the next format specifier.
343 ulWidth = ProcessFormatSegment(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, &pszFormat[ulFmtIdx], &fmt, &ulSpecifierLen);
346 REDASSERT(ulWidth <= (ulBufferLen - ulBufIdx));
351 /* If no specifier was found, or if the output buffer is
352 full, we're done -- get out.
354 if((ulSpecifierLen == 0U) || (ulBufIdx == ulBufferLen))
359 /* Otherwise, the math should add up for these things...
361 REDASSERT(&pszFormat[fmt.ulSpecifierIdx] == &pszFormat[ulWidth]);
363 /* Point past the specifier, to the next piece of the format string.
365 ulFmtIdx = ulFmtIdx + fmt.ulSpecifierIdx + ulSpecifierLen;
369 int iFillLen = va_arg(arglist, int);
373 fmt.ulFillLen = (uint32_t)iFillLen;
377 /* Bogus fill length. Ignore.
385 case PRFMT_DOUBLEPERCENT:
387 /* Nothing to do. A single percent has already been output,
388 and we just finished skipping past the second percent.
393 /*-----------------> Small int handling <------------------
395 * Values smaller than "int" will be promoted to "int" by
396 * the compiler, so we must retrieve them using "int" when
397 * calling va_arg(). Once we've done that, we immediately
398 * put the value into the desired data type.
399 *---------------------------------------------------------*/
403 pcBuffer[ulBufIdx] = (char)va_arg(arglist, int);
407 case PRFMT_SIGNED8BIT:
409 int8_t num = (int8_t)va_arg(arglist, int);
411 ulBufIdx += LtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, num, fmt.ulFillLen, fmt.cFillChar);
414 case PRFMT_UNSIGNED8BIT:
416 uint8_t bNum = (uint8_t)va_arg(arglist, unsigned);
418 ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, bNum, false, fmt.ulFillLen, fmt.cFillChar);
423 uint8_t bNum = (uint8_t)va_arg(arglist, unsigned);
425 ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, bNum, true, fmt.ulFillLen, fmt.cFillChar);
428 case PRFMT_SIGNED16BIT:
430 int16_t num = (int16_t)va_arg(arglist, int);
432 ulBufIdx += LtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, num, fmt.ulFillLen, fmt.cFillChar);
435 case PRFMT_UNSIGNED16BIT:
437 uint16_t uNum = (uint16_t)va_arg(arglist, unsigned);
439 ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, uNum, false, fmt.ulFillLen, fmt.cFillChar);
444 uint16_t uNum = (uint16_t)va_arg(arglist, unsigned);
446 ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, uNum, true, fmt.ulFillLen, fmt.cFillChar);
449 case PRFMT_SIGNED32BIT:
451 int32_t lNum = va_arg(arglist, int32_t);
453 ulBufIdx += LtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, lNum, fmt.ulFillLen, fmt.cFillChar);
456 case PRFMT_UNSIGNED32BIT:
458 uint32_t ulNum = va_arg(arglist, uint32_t);
460 ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ulNum, false, fmt.ulFillLen, fmt.cFillChar);
465 uint32_t ulNum = va_arg(arglist, uint32_t);
467 ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ulNum, true, fmt.ulFillLen, fmt.cFillChar);
470 case PRFMT_SIGNED64BIT:
472 int64_t llNum = va_arg(arglist, int64_t);
474 ulBufIdx += LLtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, llNum, fmt.ulFillLen, fmt.cFillChar);
477 case PRFMT_UNSIGNED64BIT:
479 uint64_t ullNum = va_arg(arglist, uint64_t);
481 ulBufIdx += ULLtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ullNum, false, fmt.ulFillLen, fmt.cFillChar);
486 uint64_t ullNum = va_arg(arglist, uint64_t);
488 ulBufIdx += ULLtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ullNum, true, fmt.ulFillLen, fmt.cFillChar);
493 const void *ptr = va_arg(arglist, const void *);
495 /* Assert our assumption.
497 REDASSERT(sizeof(void *) <= 8U);
499 /* Format as either a 64-bit or a 32-bit value.
501 if(sizeof(void *) > 4U)
503 /* Attempt to quiet warnings.
505 uintptr_t ptrval = (uintptr_t)ptr;
506 uint64_t ullPtrVal = (uint64_t)ptrval;
508 ulBufIdx += ULLtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ullPtrVal, true, fmt.ulFillLen, fmt.cFillChar);
512 /* Attempt to quiet warnings.
514 uintptr_t ptrval = (uintptr_t)ptr;
515 uint32_t ulPtrVal = (uint32_t)ptrval;
517 ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ulPtrVal, true, fmt.ulFillLen, fmt.cFillChar);
522 case PRFMT_ANSISTRING:
524 const char *pszArg = va_arg(arglist, const char *);
525 uint32_t ulArgIdx = 0U;
532 if(fmt.ulFillLen > 0U)
534 if(!fmt.fLeftJustified)
536 uint32_t ulLen = RedStrLen(pszArg);
538 /* So long as we are not left justifying, fill as many
539 characters as is necessary to make the string right
542 while(((ulBufferLen - ulBufIdx) > 0U) && (fmt.ulFillLen > ulLen))
544 pcBuffer[ulBufIdx] = fmt.cFillChar;
550 /* Move as many characters as we have space for into the
553 while(((ulBufferLen - ulBufIdx) > 0U) && (pszArg[ulArgIdx] != '\0'))
555 pcBuffer[ulBufIdx] = pszArg[ulArgIdx];
558 if(fmt.ulFillLen > 0U)
564 /* If there is any space left to fill, do it (the string
565 must have been left justified).
567 while(((ulBufferLen - ulBufIdx) > 0U) && (fmt.ulFillLen > 0U))
569 /* This is NOT a typo -- when using left justified
570 strings, spaces are the only allowed fill character.
573 pcBuffer[ulBufIdx] = ' ';
580 /* No fill characters, just move up to as many
581 characters as we have space for in the output
584 while(((ulBufferLen - ulBufIdx) > 0U) && (pszArg[ulArgIdx] != '\0'))
586 pcBuffer[ulBufIdx] = pszArg[ulArgIdx];
601 /* If there is space, tack on a null and return the output length
602 processed, not including the null.
604 if(ulBufIdx < ulBufferLen)
606 pcBuffer[ulBufIdx] = '\0';
607 iLen = (int32_t)ulBufIdx;
611 /* Not enough space, just return -1, with no null termination
620 /** @brief Process the next segment of the format string, outputting any
621 non-format specifiers, as output buffer space allows, and return
622 information about the next format specifier.
624 @note If the returned value is the same as the supplied @p ulBufferLen,
625 the output buffer will not be null-terminated. In all other cases,
626 the result will be null-terminated. The returned length will never
627 include the null in the count.
629 @param pcBuffer The output buffer.
630 @param ulBufferLen The output buffer length.
631 @param pszFormat The format string to process.
632 @param pFormat The PRINTFORMAT structure to fill.
633 @param pulSpecifierLen Returns the length of any format specifier string,
634 or zero if no specifier was found.
636 @return The count of characters from pszFormatt which were processed and
638 - If zero is returned and *pulSpecifierLen is non-zero, then
639 a format specifier string was found at the start of pszFmt.
640 - If non-zero is returned and *pulSpecifierLen is zero, then
641 no format specifier string was found, and the entire pszFmt
642 string was copied to pBuffer (or as much as will fit).
644 static uint32_t ProcessFormatSegment(
646 uint32_t ulBufferLen,
647 const char *pszFormat,
648 PRINTFORMAT *pFormat,
649 uint32_t *pulSpecifierLen)
651 uint32_t ulWidth = 0U;
653 /* Find the next format specifier string, and information about it.
655 *pulSpecifierLen = ParseFormatSpecifier(pszFormat, pFormat);
657 if(*pulSpecifierLen == 0U)
659 /* If no specifier was found at all, then simply output the full length
660 of the string, or as much as will fit.
662 ulWidth = REDMIN(ulBufferLen, RedStrLen(pszFormat));
664 RedMemCpy(pcBuffer, pszFormat, ulWidth);
668 /* If we encountered a double percent, skip past one of them so it is
669 copied into the output buffer.
671 if(pFormat->type == PRFMT_DOUBLEPERCENT)
673 pFormat->ulSpecifierIdx++;
675 /* A double percent specifier always has a length of two. Since
676 we're processing one of those percent signs, reduce the length
677 to one. Assert it so.
679 REDASSERT(*pulSpecifierLen == 2U);
681 (*pulSpecifierLen)--;
684 /* So long as the specifier is not the very first thing in the format
687 if(pFormat->ulSpecifierIdx != 0U)
689 /* A specifier was found, but there is other data preceding it.
690 Copy as much as allowed to the output buffer.
692 ulWidth = REDMIN(ulBufferLen, pFormat->ulSpecifierIdx);
694 RedMemCpy(pcBuffer, pszFormat, ulWidth);
698 /* If there is room in the output buffer, null-terminate whatever is there.
699 But note that the returned length never includes the null.
701 if(ulWidth < ulBufferLen)
703 pcBuffer[ulWidth] = 0U;
710 /** @brief Parse the specified format string for a valid RedVSNPrintf() format
711 sequence, and return information about it.
713 @param pszFormat The format string to process.
714 @param pFormatType The PRINTFORMAT structure to fill. The data is only
715 valid if a non-zero length is returned.
717 @return The length of the full format specifier string, starting at
718 pFormat->ulSpecifierIdx. Returns zero if a valid specifier was
721 static uint32_t ParseFormatSpecifier(
722 char const *pszFormat,
723 PRINTFORMAT *pFormatType)
725 bool fContainsIllegalSequence = false;
729 while(pszFormat[ulIdx] != '\0')
735 if(pszFormat[ulIdx] != '%')
741 RedMemSet(pFormatType, 0U, sizeof(*pFormatType));
743 /* Record the location of the start of the format sequence
745 pFormatType->ulSpecifierIdx = ulIdx;
748 if(pszFormat[ulIdx] == '-')
750 pFormatType->fLeftJustified = true;
754 if((pszFormat[ulIdx] == '0') || (pszFormat[ulIdx] == '_'))
756 pFormatType->cFillChar = pszFormat[ulIdx];
761 pFormatType->cFillChar = ' ';
764 if(pszFormat[ulIdx] == '*')
766 pFormatType->fHasVarWidth = true;
769 else if(ISDIGIT(pszFormat[ulIdx]))
771 pFormatType->ulFillLen = (uint32_t)RedAtoI(&pszFormat[ulIdx]);
772 while(ISDIGIT(pszFormat[ulIdx]))
783 pFormatType->type = ParseFormatType(&pszFormat[ulIdx], &ulTypeLen);
784 if(pFormatType->type != PRFMT_UNKNOWN)
786 /* Even though we are returning successfully, keep track of
787 whether an illegal sequence was encountered and skipped.
789 pFormatType->fHasIllegalType = fContainsIllegalSequence;
791 ulLen = (ulIdx - pFormatType->ulSpecifierIdx) + ulTypeLen;
795 /* In the case of an unrecognized type string, simply ignore
796 it entirely. Reset the pointer to the position following
797 the percent sign, so it is not found again.
799 fContainsIllegalSequence = false;
800 ulIdx = pFormatType->ulSpecifierIdx + 1U;
808 /** @brief Parse a RedPrintf() format type string to determine the proper data
811 @param pszFormat The format string to process. This must be a pointer to
812 the character following any width or justification
814 @param pulTypeLen The location in which to store the type length. The
815 value will be 0 if PRFMT_UNKNOWN is returned.
817 @return Rhe PRFMT_* type value, or PRFMT_UNKNOWN if the type is not
820 static PRINTTYPE ParseFormatType(
821 const char *pszFormat,
822 uint32_t *pulTypeLen)
824 PRINTTYPE fmtType = PRFMT_UNKNOWN;
827 switch(pszFormat[ulIdx])
830 fmtType = PRFMT_DOUBLEPERCENT;
833 fmtType = PRFMT_CHAR;
836 fmtType = PRFMT_ANSISTRING;
839 fmtType = PRFMT_POINTER;
848 fmtType = MAPHEXUINT;
853 switch(pszFormat[ulIdx])
862 fmtType = MAPHEXUSHORT;
872 switch(pszFormat[ulIdx])
881 fmtType = MAPHEXULONG;
886 switch(pszFormat[ulIdx])
889 fmtType = MAPLONGLONG;
892 fmtType = MAPULONGLONG;
896 fmtType = MAPHEXULONGLONG;
912 if(fmtType != PRFMT_UNKNOWN)
914 *pulTypeLen = ulIdx + 1U;
925 /** @brief Format a signed 32-bit integer as a base 10 ASCII string.
927 @note If the output buffer length is exhausted, the result will *not* be
930 @note If the @p ulFillLen value is greater than or equal to the buffer
931 length, the result will not be null-terminated, even if the
932 formatted portion of the data is shorter than the buffer length.
934 @param pcBuffer The output buffer
935 @param ulBufferLen A pointer to the output buffer length
936 @param lNum The 32-bit signed number to convert
937 @param ulFillLen The fill length, if any
938 @param cFill The fill character to use
940 @return The length of the string.
942 static uint32_t LtoA(
944 uint32_t ulBufferLen,
958 char ach[12U]; /* big enough for a int32_t in base 10 */
959 uint32_t ulDigits = 0U;
966 ulNum = (uint32_t)-lNum;
971 ulNum = (uint32_t)lNum;
976 ach[ulDigits] = gacDigits[ulNum % 10U];
988 ulLen = FinishToA(ach, ulDigits, pcBuffer, ulBufferLen, ulFillLen, cFill);
995 /** @brief Format a signed 64-bit integer as a base 10 ASCII string.
997 @note If the output buffer length is exhausted, the result will *not* be
1000 @note If the @p ulFillLen value is greater than or equal to the buffer
1001 length, the result will not be null-terminated, even if the
1002 formatted portion of the data is shorter than the buffer length.
1004 @param pcBuffer The output buffer
1005 @param ulBufferLen A pointer to the output buffer length
1006 @param llNum The 64-bit signed number to convert
1007 @param ulFillLen The fill length, if any
1008 @param cFill The fill character to use
1010 @return The length of the string.
1012 static uint32_t LLtoA(
1014 uint32_t ulBufferLen,
1021 if(pcBuffer == NULL)
1028 char ach[12U]; /* big enough for a int32_t in base 10 */
1029 uint32_t ulDigits = 0U;
1036 ullNum = (uint64_t)-llNum;
1041 ullNum = (uint64_t)llNum;
1044 /* Not allowed to assume that 64-bit division is OK, so use a
1045 software division routine.
1049 uint64_t ullQuotient;
1050 uint32_t ulRemainder;
1052 /* Note: RedUint64DivMod32() is smart enough to use normal division
1053 once ullNumericVal <= UINT32_MAX.
1055 ullQuotient = RedUint64DivMod32(ullNum, 10U, &ulRemainder);
1057 ach[ulDigits] = gacDigits[ulRemainder];
1058 ullNum = ullQuotient;
1065 ach[ulDigits] = '-';
1069 ulLen = FinishToA(ach, ulDigits, pcBuffer, ulBufferLen, ulFillLen, cFill);
1076 /** @brief Format an unsigned 32-bit integer as an ASCII string as decimal or
1079 @note If the output buffer length is exhausted, the result will *not* be
1082 @param pcBuffer The output buffer
1083 @param ulBufferLen The output buffer length
1084 @param ulNum The 32-bit unsigned number to convert
1085 @param fHex If true, format as hex; if false, decimal.
1086 @param ulFillLen The fill length, if any
1087 @param cFill The fill character to use
1089 @return The length of the string.
1091 static uint32_t ULtoA(
1093 uint32_t ulBufferLen,
1101 if(pcBuffer == NULL)
1108 char ach[11U]; /* Big enough for a uint32_t in radix 10 */
1109 uint32_t ulDigits = 0U;
1110 uint32_t ulNumericVal = ulNum;
1111 uint32_t ulRadix = fHex ? 16U : 10U;
1115 ach[ulDigits] = gacDigits[ulNumericVal % ulRadix];
1116 ulNumericVal = ulNumericVal / ulRadix;
1119 while(ulNumericVal > 0U);
1121 ulLen = FinishToA(ach, ulDigits, pcBuffer, ulBufferLen, ulFillLen, cFill);
1128 /** @brief Format an unsigned 64-bit integer as an ASCII string as decimal or
1131 @note If the output buffer length is exhausted, the result will *not* be
1134 @param pcBuffer The output buffer.
1135 @param ulBufferLen The output buffer length.
1136 @param ullNum The unsigned 64-bit number to convert.
1137 @param fHex If true, format as hex; if false, decimal.
1138 @param ulFillLen The fill length, if any.
1139 @param cFill The fill character to use.
1141 @return The length of the string.
1143 static uint32_t ULLtoA(
1145 uint32_t ulBufferLen,
1153 if(pcBuffer == NULL)
1161 char ach[21U]; /* Big enough for a uint64_t in radix 10 */
1162 uint32_t ulDigits = 0U;
1163 uint64_t ullNumericVal = ullNum;
1167 /* We can figure out the digits using bit operations.
1171 ach[ulDigits] = gacDigits[ullNumericVal & 15U];
1172 ullNumericVal >>= 4U;
1175 while(ullNumericVal > 0U);
1179 /* Not allowed to assume that 64-bit division is OK, so use a
1180 software division routine.
1184 uint64_t ullQuotient;
1185 uint32_t ulRemainder;
1187 /* Note: RedUint64DivMod32() is smart enough to use normal division
1188 once ullNumericVal <= UINT32_MAX.
1190 ullQuotient = RedUint64DivMod32(ullNumericVal, 10U, &ulRemainder);
1192 ach[ulDigits] = gacDigits[ulRemainder];
1193 ullNumericVal = ullQuotient;
1196 while(ullNumericVal > 0U);
1199 ulLen = FinishToA(ach, ulDigits, pcBuffer, ulBufferLen, ulFillLen, cFill);
1206 /** @brief Finish converting a number into an ASCII string representing that
1209 This helper function contains common logic that needs to run at the end of
1210 all the "toA" functions. It adds the fill character and reverses the digits
1213 @param pcDigits The digits (and sign) for the ASCII string, in reverse
1214 order as they were computed.
1215 @param ulDigits The number of digit characters.
1216 @param pcOutBuffer The output buffer.
1217 @param ulBufferLen The length of the output buffer.
1218 @param ulFillLen The fill length. If the number string is shorter than
1219 this, the remaining bytes are filled with @p cFill.
1220 @param cFill The fill character.
1222 @return The length of @p pcOutBuffer.
1224 static uint32_t FinishToA(
1225 const char *pcDigits,
1228 uint32_t ulBufferLen,
1232 uint32_t ulIdx = 0U;
1233 uint32_t ulDigitIdx = ulDigits;
1235 /* user may have asked for a fill char
1237 if(ulFillLen > ulDigits)
1239 uint32_t ulFillRem = ulFillLen - ulDigits;
1241 while((ulFillRem > 0U) && (ulIdx < ulBufferLen))
1243 pcOutBuffer[ulIdx] = cFill;
1249 /* reverse the string
1251 while((ulDigitIdx > 0) && (ulIdx < ulBufferLen))
1254 pcOutBuffer[ulIdx] = pcDigits[ulDigitIdx];
1258 if(ulIdx < ulBufferLen)
1260 pcOutBuffer[ulIdx] = '\0';