michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef jsprf_h michael@0: #define jsprf_h michael@0: michael@0: /* michael@0: ** API for PR printf like routines. Supports the following formats michael@0: ** %d - decimal michael@0: ** %u - unsigned decimal michael@0: ** %x - unsigned hex michael@0: ** %X - unsigned uppercase hex michael@0: ** %o - unsigned octal michael@0: ** %hd, %hu, %hx, %hX, %ho - 16-bit versions of above michael@0: ** %ld, %lu, %lx, %lX, %lo - 32-bit versions of above michael@0: ** %lld, %llu, %llx, %llX, %llo - 64 bit versions of above michael@0: ** %s - ascii string michael@0: ** %hs - ucs2 string michael@0: ** %c - character michael@0: ** %p - pointer (deals with machine dependent pointer size) michael@0: ** %f - float michael@0: ** %g - float michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include "jstypes.h" michael@0: michael@0: /* michael@0: ** sprintf into a fixed size buffer. Guarantees that a NUL is at the end michael@0: ** of the buffer. Returns the length of the written output, NOT including michael@0: ** the NUL, or (uint32_t)-1 if an error occurs. michael@0: */ michael@0: extern JS_PUBLIC_API(uint32_t) JS_snprintf(char *out, uint32_t outlen, const char *fmt, ...); michael@0: michael@0: /* michael@0: ** sprintf into a malloc'd buffer. Return a pointer to the malloc'd michael@0: ** buffer on success, nullptr on failure. Call "JS_smprintf_free" to release michael@0: ** the memory returned. michael@0: */ michael@0: extern JS_PUBLIC_API(char*) JS_smprintf(const char *fmt, ...); michael@0: michael@0: /* michael@0: ** Free the memory allocated, for the caller, by JS_smprintf michael@0: */ michael@0: extern JS_PUBLIC_API(void) JS_smprintf_free(char *mem); michael@0: michael@0: /* michael@0: ** "append" sprintf into a malloc'd buffer. "last" is the last value of michael@0: ** the malloc'd buffer. sprintf will append data to the end of last, michael@0: ** growing it as necessary using realloc. If last is nullptr, JS_sprintf_append michael@0: ** will allocate the initial string. The return value is the new value of michael@0: ** last for subsequent calls, or nullptr if there is a malloc failure. michael@0: */ michael@0: extern JS_PUBLIC_API(char*) JS_sprintf_append(char *last, const char *fmt, ...); michael@0: michael@0: /* michael@0: ** va_list forms of the above. michael@0: */ michael@0: extern JS_PUBLIC_API(uint32_t) JS_vsnprintf(char *out, uint32_t outlen, const char *fmt, va_list ap); michael@0: extern JS_PUBLIC_API(char*) JS_vsmprintf(const char *fmt, va_list ap); michael@0: extern JS_PUBLIC_API(char*) JS_vsprintf_append(char *last, const char *fmt, va_list ap); michael@0: michael@0: #endif /* jsprf_h */