Line data Source code
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 : /*
8 : * Portable safe sprintf code.
9 : *
10 : * Author: Kipp E.B. Hickman
11 : */
12 :
13 : #include "jsprf.h"
14 :
15 : #include "mozilla/Printf.h"
16 :
17 : #include "jsalloc.h"
18 :
19 : using namespace js;
20 :
21 : typedef mozilla::SmprintfPolicyPointer<js::SystemAllocPolicy> JSSmprintfPointer;
22 :
23 8261 : JS_PUBLIC_API(JS::UniqueChars) JS_smprintf(const char* fmt, ...)
24 : {
25 : va_list ap;
26 8261 : va_start(ap, fmt);
27 16521 : JSSmprintfPointer result = mozilla::Vsmprintf<js::SystemAllocPolicy>(fmt, ap);
28 8261 : va_end(ap);
29 16521 : return JS::UniqueChars(result.release());
30 : }
31 :
32 3956 : JS_PUBLIC_API(void) JS_smprintf_free(char* mem)
33 : {
34 3956 : mozilla::SmprintfFree<js::SystemAllocPolicy>(mem);
35 3956 : }
36 :
37 7 : JS_PUBLIC_API(JS::UniqueChars) JS_sprintf_append(JS::UniqueChars&& last, const char* fmt, ...)
38 : {
39 : va_list ap;
40 7 : va_start(ap, fmt);
41 14 : JSSmprintfPointer lastPtr(last.release());
42 : JSSmprintfPointer result =
43 14 : mozilla::VsmprintfAppend<js::SystemAllocPolicy>(Move(lastPtr), fmt, ap);
44 7 : va_end(ap);
45 14 : return JS::UniqueChars(result.release());
46 : }
47 :
48 0 : JS_PUBLIC_API(JS::UniqueChars) JS_vsmprintf(const char* fmt, va_list ap)
49 : {
50 0 : return JS::UniqueChars(mozilla::Vsmprintf<js::SystemAllocPolicy>(fmt, ap).release());
51 : }
52 :
53 0 : JS_PUBLIC_API(JS::UniqueChars) JS_vsprintf_append(JS::UniqueChars&& last,
54 : const char* fmt, va_list ap)
55 : {
56 0 : JSSmprintfPointer lastPtr(last.release());
57 0 : return JS::UniqueChars(mozilla::VsmprintfAppend<js::SystemAllocPolicy>(Move(lastPtr),
58 0 : fmt, ap).release());
59 : }
|