blob: b0e2acce8f942e0a7baf67fa163c6ae116de923d [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: Sanjay Ghemawat
30
31#include "ceres/stringprintf.h"
32
33#include <cerrno>
34#include <cstdarg> // For va_list and related operations
35#include <cstdio> // MSVC requires this for _vsnprintf
36#include <string>
37#include <vector>
38
39#include "ceres/internal/port.h"
40
41namespace ceres {
42namespace internal {
43
44using std::string;
45
Austin Schuh70cc9552019-01-21 19:46:48 -080046void StringAppendV(string* dst, const char* format, va_list ap) {
47 // First try with a small fixed size buffer
48 char space[1024];
49
50 // It's possible for methods that use a va_list to invalidate
51 // the data in it upon use. The fix is to make a copy
52 // of the structure before using it and use that copy instead.
53 va_list backup_ap;
54 va_copy(backup_ap, ap);
55 int result = vsnprintf(space, sizeof(space), format, backup_ap);
56 va_end(backup_ap);
57
58 if (result < sizeof(space)) {
59 if (result >= 0) {
60 // Normal case -- everything fit.
61 dst->append(space, result);
62 return;
63 }
64
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080065#if defined(_MSC_VER)
Austin Schuh70cc9552019-01-21 19:46:48 -080066 // Error or MSVC running out of space. MSVC 8.0 and higher
67 // can be asked about space needed with the special idiom below:
68 va_copy(backup_ap, ap);
69 result = vsnprintf(NULL, 0, format, backup_ap);
70 va_end(backup_ap);
71#endif
72
73 if (result < 0) {
74 // Just an error.
75 return;
76 }
77 }
78
79 // Increase the buffer size to the size requested by vsnprintf,
80 // plus one for the closing \0.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080081 int length = result + 1;
Austin Schuh70cc9552019-01-21 19:46:48 -080082 char* buf = new char[length];
83
84 // Restore the va_list before we use it again
85 va_copy(backup_ap, ap);
86 result = vsnprintf(buf, length, format, backup_ap);
87 va_end(backup_ap);
88
89 if (result >= 0 && result < length) {
90 // It fit
91 dst->append(buf, result);
92 }
93 delete[] buf;
94}
95
Austin Schuh70cc9552019-01-21 19:46:48 -080096string StringPrintf(const char* format, ...) {
97 va_list ap;
98 va_start(ap, format);
99 string result;
100 StringAppendV(&result, format, ap);
101 va_end(ap);
102 return result;
103}
104
105const string& SStringPrintf(string* dst, const char* format, ...) {
106 va_list ap;
107 va_start(ap, format);
108 dst->clear();
109 StringAppendV(dst, format, ap);
110 va_end(ap);
111 return *dst;
112}
113
114void StringAppendF(string* dst, const char* format, ...) {
115 va_list ap;
116 va_start(ap, format);
117 StringAppendV(dst, format, ap);
118 va_end(ap);
119}
120
121} // namespace internal
122} // namespace ceres