source: trunk/src/base/string.cpp @ 2256

Last change on this file since 2256 was 2256, checked in by sam, 7 years ago

core: implement String::Printf() for va_list arguments.

  • Property svn:keywords set to Id
File size: 1.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdio>
16
17#ifdef WIN32
18#   define WIN32_LEAN_AND_MEAN
19#   include <windows.h>
20#endif
21
22#include <cstdarg>
23
24#include "core.h"
25
26namespace lol
27{
28
29String String::Printf(char const *format, ...)
30{
31    String ret;
32
33    va_list ap;
34    va_start(ap, format);
35    ret = String::Printf(format, ap);
36    va_end(ap);
37
38    return ret;
39}
40
41String String::Printf(char const *format, va_list ap)
42{
43#if defined __CELLOS_LV2__
44    using std::vsnprintf;
45#endif
46
47    String ret;
48
49    /* Visual Studio 2010 does not support it va_copy. */
50#if defined _MSC_VER
51#   undef va_copy
52#   define va_copy(dst, src) (dst = src)
53#endif
54    va_list ap2;
55    va_copy(ap2, ap);
56#if defined _MSC_VER
57#   undef va_copy
58#endif
59
60    /* vsnprintf() tells us how many character we need, and we need to
61     * add one for the terminating null byte. */
62    size_t needed = vsnprintf(NULL, 0, format, ap2) + 1;
63
64    ((Super &)ret).Reserve(needed);
65    ret.m_count = needed;
66
67    vsnprintf(&ret[0], needed, format, ap);
68
69    return ret;
70}
71
72} /* namespace lol */
73
Note: See TracBrowser for help on using the repository browser.