1 // 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
39 namespace ceres {
40 namespace internal {
41
42 #ifdef _MSC_VER
43 enum { IS_COMPILER_MSVC = 1 };
44 #define va_copy(d,s) ((d) = (s))
45 #else
46 enum { IS_COMPILER_MSVC = 0 };
47 #endif
48
StringAppendV(string * dst,const char * format,va_list ap)49 void StringAppendV(string* dst, const char* format, va_list ap) {
50 // First try with a small fixed size buffer
51 char space[1024];
52
53 // It's possible for methods that use a va_list to invalidate
54 // the data in it upon use. The fix is to make a copy
55 // of the structure before using it and use that copy instead.
56 va_list backup_ap;
57 va_copy(backup_ap, ap);
58 int result = vsnprintf(space, sizeof(space), format, backup_ap);
59 va_end(backup_ap);
60
61 if (result < sizeof(space)) {
62 if (result >= 0) {
63 // Normal case -- everything fit.
64 dst->append(space, result);
65 return;
66 }
67
68 if (IS_COMPILER_MSVC) {
69 // Error or MSVC running out of space. MSVC 8.0 and higher
70 // can be asked about space needed with the special idiom below:
71 va_copy(backup_ap, ap);
72 result = vsnprintf(NULL, 0, format, backup_ap);
73 va_end(backup_ap);
74 }
75
76 if (result < 0) {
77 // Just an error.
78 return;
79 }
80 }
81
82 // Increase the buffer size to the size requested by vsnprintf,
83 // plus one for the closing \0.
84 int length = result+1;
85 char* buf = new char[length];
86
87 // Restore the va_list before we use it again
88 va_copy(backup_ap, ap);
89 result = vsnprintf(buf, length, format, backup_ap);
90 va_end(backup_ap);
91
92 if (result >= 0 && result < length) {
93 // It fit
94 dst->append(buf, result);
95 }
96 delete[] buf;
97 }
98
99
StringPrintf(const char * format,...)100 string StringPrintf(const char* format, ...) {
101 va_list ap;
102 va_start(ap, format);
103 string result;
104 StringAppendV(&result, format, ap);
105 va_end(ap);
106 return result;
107 }
108
SStringPrintf(string * dst,const char * format,...)109 const string& SStringPrintf(string* dst, const char* format, ...) {
110 va_list ap;
111 va_start(ap, format);
112 dst->clear();
113 StringAppendV(dst, format, ap);
114 va_end(ap);
115 return *dst;
116 }
117
StringAppendF(string * dst,const char * format,...)118 void StringAppendF(string* dst, const char* format, ...) {
119 va_list ap;
120 va_start(ap, format);
121 StringAppendV(dst, format, ap);
122 va_end(ap);
123 }
124
125 } // namespace internal
126 } // namespace ceres
127