blob: d1e016aa2d165904541be4c034653a0da78c69aa [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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 <cerrno>
32#include <cstdarg> // For va_list and related operations
33#include <cstdio> // MSVC requires this for _vsnprintf
34#include <string>
35#include <vector>
36
37#include "ceres/internal/port.h"
38
39namespace ceres {
40namespace internal {
41
42#ifdef COMPILER_MSVC
43enum { IS_COMPILER_MSVC = 1 };
44#else
45enum { IS_COMPILER_MSVC = 0 };
46#endif
47
48void StringAppendV(string* dst, const char* format, va_list ap) {
49 // First try with a small fixed size buffer
50 char space[1024];
51
52 // It's possible for methods that use a va_list to invalidate
53 // the data in it upon use. The fix is to make a copy
54 // of the structure before using it and use that copy instead.
55 va_list backup_ap;
56 va_copy(backup_ap, ap);
57 int result = vsnprintf(space, sizeof(space), format, backup_ap);
58 va_end(backup_ap);
59
60 if (result < sizeof(space)) {
61 if (result >= 0) {
62 // Normal case -- everything fit.
63 dst->append(space, result);
64 return;
65 }
66
67 if (IS_COMPILER_MSVC) {
68 // Error or MSVC running out of space. MSVC 8.0 and higher
69 // can be asked about space needed with the special idiom below:
70 va_copy(backup_ap, ap);
71 result = vsnprintf(NULL, 0, format, backup_ap);
72 va_end(backup_ap);
73 }
74
75 if (result < 0) {
76 // Just an error.
77 return;
78 }
79 }
80
81 // Increase the buffer size to the size requested by vsnprintf,
82 // plus one for the closing \0.
83 int length = result+1;
84 char* buf = new char[length];
85
86 // Restore the va_list before we use it again
87 va_copy(backup_ap, ap);
88 result = vsnprintf(buf, length, format, backup_ap);
89 va_end(backup_ap);
90
91 if (result >= 0 && result < length) {
92 // It fit
93 dst->append(buf, result);
94 }
95 delete[] buf;
96}
97
98
99string StringPrintf(const char* format, ...) {
100 va_list ap;
101 va_start(ap, format);
102 string result;
103 StringAppendV(&result, format, ap);
104 va_end(ap);
105 return result;
106}
107
108const string& SStringPrintf(string* dst, const char* format, ...) {
109 va_list ap;
110 va_start(ap, format);
111 dst->clear();
112 StringAppendV(dst, format, ap);
113 va_end(ap);
114 return *dst;
115}
116
117void StringAppendF(string* dst, const char* format, ...) {
118 va_list ap;
119 va_start(ap, format);
120 StringAppendV(dst, format, ap);
121 va_end(ap);
122}
123
124} // namespace internal
125} // namespace ceres