1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsTextFormatter.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,77 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * This code was copied from xpcom/ds/nsTextFormatter r1.3 1.11 + * Memory model and Frozen linkage changes only. 1.12 + * -- Prasad <prasad@medhas.org> 1.13 + */ 1.14 + 1.15 +#ifndef nsTextFormatter_h___ 1.16 +#define nsTextFormatter_h___ 1.17 + 1.18 +/* 1.19 + ** API for PR printf like routines. Supports the following formats 1.20 + ** %d - decimal 1.21 + ** %u - unsigned decimal 1.22 + ** %x - unsigned hex 1.23 + ** %X - unsigned uppercase hex 1.24 + ** %o - unsigned octal 1.25 + ** %hd, %hu, %hx, %hX, %ho - 16-bit versions of above 1.26 + ** %ld, %lu, %lx, %lX, %lo - 32-bit versions of above 1.27 + ** %lld, %llu, %llx, %llX, %llo - 64 bit versions of above 1.28 + ** %s - utf8 string 1.29 + ** %S - char16_t string 1.30 + ** %c - character 1.31 + ** %p - pointer (deals with machine dependent pointer size) 1.32 + ** %f - float 1.33 + ** %g - float 1.34 + */ 1.35 +#include "prio.h" 1.36 +#include <stdio.h> 1.37 +#include <stdarg.h> 1.38 +#include "nscore.h" 1.39 +#include "nsStringGlue.h" 1.40 + 1.41 +#ifdef XPCOM_GLUE 1.42 +#error "nsTextFormatter is not available in the standalone glue due to NSPR dependencies." 1.43 +#endif 1.44 + 1.45 +class NS_COM_GLUE nsTextFormatter { 1.46 + 1.47 + public: 1.48 + 1.49 + /* 1.50 + * sprintf into a fixed size buffer. Guarantees that the buffer is null 1.51 + * terminated. Returns the length of the written output, NOT including the 1.52 + * null terminator, or (uint32_t)-1 if an error occurs. 1.53 + */ 1.54 + static uint32_t snprintf(char16_t *out, uint32_t outlen, const char16_t *fmt, ...); 1.55 + 1.56 + /* 1.57 + * sprintf into a nsMemory::Alloc'd buffer. Return a pointer to 1.58 + * buffer on success, nullptr on failure. 1.59 + */ 1.60 + static char16_t* smprintf(const char16_t *fmt, ...); 1.61 + 1.62 + static uint32_t ssprintf(nsAString& out, const char16_t* fmt, ...); 1.63 + 1.64 + /* 1.65 + * va_list forms of the above. 1.66 + */ 1.67 + static uint32_t vsnprintf(char16_t *out, uint32_t outlen, const char16_t *fmt, va_list ap); 1.68 + static char16_t* vsmprintf(const char16_t *fmt, va_list ap); 1.69 + static uint32_t vssprintf(nsAString& out, const char16_t *fmt, va_list ap); 1.70 + 1.71 + /* 1.72 + * Free the memory allocated, for the caller, by smprintf. 1.73 + * -- Deprecated -- 1.74 + * Callers can substitute calling smprintf_free with nsMemory::Free 1.75 + */ 1.76 + static void smprintf_free(char16_t *mem); 1.77 + 1.78 +}; 1.79 + 1.80 +#endif /* nsTextFormatter_h___ */