blob: 100bbff9e8a814d8fed95eb1d2c787b33bdc048c [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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
Austin Schuh3de38b02024-06-25 18:25:10 -070039#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080040
Austin Schuh3de38b02024-06-25 18:25:10 -070041namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080042
Austin Schuh3de38b02024-06-25 18:25:10 -070043void StringAppendV(std::string* dst, const char* format, va_list ap) {
Austin Schuh70cc9552019-01-21 19:46:48 -080044 // First try with a small fixed size buffer
45 char space[1024];
46
47 // It's possible for methods that use a va_list to invalidate
48 // the data in it upon use. The fix is to make a copy
49 // of the structure before using it and use that copy instead.
50 va_list backup_ap;
51 va_copy(backup_ap, ap);
52 int result = vsnprintf(space, sizeof(space), format, backup_ap);
53 va_end(backup_ap);
54
55 if (result < sizeof(space)) {
56 if (result >= 0) {
57 // Normal case -- everything fit.
58 dst->append(space, result);
59 return;
60 }
61
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080062#if defined(_MSC_VER)
Austin Schuh70cc9552019-01-21 19:46:48 -080063 // Error or MSVC running out of space. MSVC 8.0 and higher
64 // can be asked about space needed with the special idiom below:
65 va_copy(backup_ap, ap);
Austin Schuh3de38b02024-06-25 18:25:10 -070066 result = vsnprintf(nullptr, 0, format, backup_ap);
Austin Schuh70cc9552019-01-21 19:46:48 -080067 va_end(backup_ap);
68#endif
69
70 if (result < 0) {
71 // Just an error.
72 return;
73 }
74 }
75
76 // Increase the buffer size to the size requested by vsnprintf,
77 // plus one for the closing \0.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080078 int length = result + 1;
Austin Schuh70cc9552019-01-21 19:46:48 -080079 char* buf = new char[length];
80
81 // Restore the va_list before we use it again
82 va_copy(backup_ap, ap);
83 result = vsnprintf(buf, length, format, backup_ap);
84 va_end(backup_ap);
85
86 if (result >= 0 && result < length) {
87 // It fit
88 dst->append(buf, result);
89 }
90 delete[] buf;
91}
92
Austin Schuh3de38b02024-06-25 18:25:10 -070093std::string StringPrintf(const char* format, ...) {
Austin Schuh70cc9552019-01-21 19:46:48 -080094 va_list ap;
95 va_start(ap, format);
Austin Schuh3de38b02024-06-25 18:25:10 -070096 std::string result;
Austin Schuh70cc9552019-01-21 19:46:48 -080097 StringAppendV(&result, format, ap);
98 va_end(ap);
99 return result;
100}
101
Austin Schuh3de38b02024-06-25 18:25:10 -0700102const std::string& SStringPrintf(std::string* dst, const char* format, ...) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800103 va_list ap;
104 va_start(ap, format);
105 dst->clear();
106 StringAppendV(dst, format, ap);
107 va_end(ap);
108 return *dst;
109}
110
Austin Schuh3de38b02024-06-25 18:25:10 -0700111void StringAppendF(std::string* dst, const char* format, ...) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800112 va_list ap;
113 va_start(ap, format);
114 StringAppendV(dst, format, ap);
115 va_end(ap);
116}
117
Austin Schuh3de38b02024-06-25 18:25:10 -0700118} // namespace ceres::internal