1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99:
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 #ifndef jsprf_h
8 #define jsprf_h
9 
10 /*
11 ** API for PR printf like routines. Supports the following formats
12 **      %d - decimal
13 **      %u - unsigned decimal
14 **      %x - unsigned hex
15 **      %X - unsigned uppercase hex
16 **      %o - unsigned octal
17 **      %hd, %hu, %hx, %hX, %ho - 16-bit versions of above
18 **      %ld, %lu, %lx, %lX, %lo - 32-bit versions of above
19 **      %lld, %llu, %llx, %llX, %llo - 64 bit versions of above
20 **      %s - ascii string
21 **      %hs - ucs2 string
22 **      %c - character
23 **      %p - pointer (deals with machine dependent pointer size)
24 **      %f - float
25 **      %g - float
26 */
27 
28 #include <stdarg.h>
29 
30 #include "jstypes.h"
31 
32 /*
33 ** sprintf into a fixed size buffer. Guarantees that a NUL is at the end
34 ** of the buffer. Returns the length of the written output, NOT including
35 ** the NUL, or (uint32_t)-1 if an error occurs.
36 */
37 extern JS_PUBLIC_API(uint32_t) JS_snprintf(char* out, uint32_t outlen, const char* fmt, ...);
38 
39 /*
40 ** sprintf into a malloc'd buffer. Return a pointer to the malloc'd
41 ** buffer on success, nullptr on failure. Call "JS_smprintf_free" to release
42 ** the memory returned.
43 */
44 extern JS_PUBLIC_API(char*) JS_smprintf(const char* fmt, ...);
45 
46 /*
47 ** Free the memory allocated, for the caller, by JS_smprintf
48 */
49 extern JS_PUBLIC_API(void) JS_smprintf_free(char* mem);
50 
51 /*
52 ** "append" sprintf into a malloc'd buffer. "last" is the last value of
53 ** the malloc'd buffer. sprintf will append data to the end of last,
54 ** growing it as necessary using realloc. If last is nullptr, JS_sprintf_append
55 ** will allocate the initial string. The return value is the new value of
56 ** last for subsequent calls, or nullptr if there is a malloc failure.
57 */
58 extern JS_PUBLIC_API(char*) JS_sprintf_append(char* last, const char* fmt, ...);
59 
60 /*
61 ** va_list forms of the above.
62 */
63 extern JS_PUBLIC_API(uint32_t) JS_vsnprintf(char* out, uint32_t outlen, const char* fmt, va_list ap);
64 extern JS_PUBLIC_API(char*) JS_vsmprintf(const char* fmt, va_list ap);
65 extern JS_PUBLIC_API(char*) JS_vsprintf_append(char* last, const char* fmt, va_list ap);
66 
67 #endif /* jsprf_h */
68