1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /*
8  * This code was copied from xpcom/ds/nsTextFormatter r1.3
9  *           Memory model and Frozen linkage changes only.
10  *                           -- Prasad <prasad@medhas.org>
11  */
12 
13 #ifndef nsTextFormatter_h___
14 #define nsTextFormatter_h___
15 
16 /*
17  ** API for PR printf like routines. Supports the following formats
18  **	%d - decimal
19  **	%u - unsigned decimal
20  **	%x - unsigned hex
21  **	%X - unsigned uppercase hex
22  **	%o - unsigned octal
23  **	%hd, %hu, %hx, %hX, %ho - 16-bit versions of above
24  **	%ld, %lu, %lx, %lX, %lo - 32-bit versions of above
25  **	%lld, %llu, %llx, %llX, %llo - 64 bit versions of above
26  **	%s - utf8 string
27  **	%S - char16_t string
28  **	%c - character
29  **	%p - pointer (deals with machine dependent pointer size)
30  **	%f - float
31  **	%g - float
32  */
33 #include "prio.h"
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include "nscore.h"
37 #include "nsStringGlue.h"
38 
39 #ifdef XPCOM_GLUE
40 #error "nsTextFormatter is not available in the standalone glue due to NSPR dependencies."
41 #endif
42 
43 class nsTextFormatter
44 {
45 public:
46 
47   /*
48    * sprintf into a fixed size buffer. Guarantees that the buffer is null
49    * terminated. Returns the length of the written output, NOT including the
50    * null terminator, or (uint32_t)-1 if an error occurs.
51    */
52   static uint32_t snprintf(char16_t* aOut, uint32_t aOutLen,
53                            const char16_t* aFmt, ...);
54 
55   /*
56    * sprintf into a moz_xmalloc'd buffer. Return a pointer to
57    * buffer on success, nullptr on failure.
58    */
59   static char16_t* smprintf(const char16_t* aFmt, ...);
60 
61   static uint32_t ssprintf(nsAString& aOut, const char16_t* aFmt, ...);
62 
63   /*
64    * va_list forms of the above.
65    */
66   static uint32_t vsnprintf(char16_t* aOut, uint32_t aOutLen, const char16_t* aFmt,
67                             va_list aAp);
68   static char16_t* vsmprintf(const char16_t* aFmt, va_list aAp);
69   static uint32_t vssprintf(nsAString& aOut, const char16_t* aFmt, va_list aAp);
70 
71   /*
72    * Free the memory allocated, for the caller, by smprintf.
73    * -- Deprecated --
74    * Callers can substitute calling smprintf_free with free
75    */
76   static void smprintf_free(char16_t* aMem);
77 
78 };
79 
80 #endif /* nsTextFormatter_h___ */
81