|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: Sanjay Ghemawat
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/stringprintf.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <cerrno>
|
|
Packit |
ea1746 |
#include <cstdarg> // For va_list and related operations
|
|
Packit |
ea1746 |
#include <cstdio> // MSVC requires this for _vsnprintf
|
|
Packit |
ea1746 |
#include <string>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/internal/port.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
using std::string;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// va_copy() was defined in the C99 standard. However, it did not appear in the
|
|
Packit |
ea1746 |
// C++ standard until C++11. This means that if Ceres is being compiled with a
|
|
Packit |
ea1746 |
// strict pre-C++11 standard (e.g. -std=c++03), va_copy() will NOT be defined,
|
|
Packit |
ea1746 |
// as we are using the C++ compiler (it would however be defined if we were
|
|
Packit |
ea1746 |
// using the C compiler). Note however that both GCC & Clang will in fact
|
|
Packit |
ea1746 |
// define va_copy() when compiling for C++ if the C++ standard is not explicitly
|
|
Packit |
ea1746 |
// specified (i.e. no -std=c++<XX> arg), even though it should not strictly be
|
|
Packit |
ea1746 |
// defined unless -std=c++11 (or greater) was passed.
|
|
Packit |
ea1746 |
#if !defined(va_copy)
|
|
Packit |
ea1746 |
#if defined (__GNUC__)
|
|
Packit |
ea1746 |
// On GCC/Clang, if va_copy() is not defined (C++ standard < C++11 explicitly
|
|
Packit |
ea1746 |
// specified), use the internal __va_copy() version, which should be present
|
|
Packit |
ea1746 |
// in even very old GCC versions.
|
|
Packit |
ea1746 |
#define va_copy(d, s) __va_copy(d, s)
|
|
Packit |
ea1746 |
#else
|
|
Packit |
ea1746 |
// Some older versions of MSVC do not have va_copy(), in which case define it.
|
|
Packit |
ea1746 |
// Although this is required for older MSVC versions, it should also work for
|
|
Packit |
ea1746 |
// other non-GCC/Clang compilers which also do not defined va_copy().
|
|
Packit |
ea1746 |
#define va_copy(d, s) ((d) = (s))
|
|
Packit |
ea1746 |
#endif // defined (__GNUC__)
|
|
Packit |
ea1746 |
#endif // !defined(va_copy)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void StringAppendV(string* dst, const char* format, va_list ap) {
|
|
Packit |
ea1746 |
// First try with a small fixed size buffer
|
|
Packit |
ea1746 |
char space[1024];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// It's possible for methods that use a va_list to invalidate
|
|
Packit |
ea1746 |
// the data in it upon use. The fix is to make a copy
|
|
Packit |
ea1746 |
// of the structure before using it and use that copy instead.
|
|
Packit |
ea1746 |
va_list backup_ap;
|
|
Packit |
ea1746 |
va_copy(backup_ap, ap);
|
|
Packit |
ea1746 |
int result = vsnprintf(space, sizeof(space), format, backup_ap);
|
|
Packit |
ea1746 |
va_end(backup_ap);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (result < sizeof(space)) {
|
|
Packit |
ea1746 |
if (result >= 0) {
|
|
Packit |
ea1746 |
// Normal case -- everything fit.
|
|
Packit |
ea1746 |
dst->append(space, result);
|
|
Packit |
ea1746 |
return;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#if defined (_MSC_VER)
|
|
Packit |
ea1746 |
// Error or MSVC running out of space. MSVC 8.0 and higher
|
|
Packit |
ea1746 |
// can be asked about space needed with the special idiom below:
|
|
Packit |
ea1746 |
va_copy(backup_ap, ap);
|
|
Packit |
ea1746 |
result = vsnprintf(NULL, 0, format, backup_ap);
|
|
Packit |
ea1746 |
va_end(backup_ap);
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (result < 0) {
|
|
Packit |
ea1746 |
// Just an error.
|
|
Packit |
ea1746 |
return;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Increase the buffer size to the size requested by vsnprintf,
|
|
Packit |
ea1746 |
// plus one for the closing \0.
|
|
Packit |
ea1746 |
int length = result+1;
|
|
Packit |
ea1746 |
char* buf = new char[length];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Restore the va_list before we use it again
|
|
Packit |
ea1746 |
va_copy(backup_ap, ap);
|
|
Packit |
ea1746 |
result = vsnprintf(buf, length, format, backup_ap);
|
|
Packit |
ea1746 |
va_end(backup_ap);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (result >= 0 && result < length) {
|
|
Packit |
ea1746 |
// It fit
|
|
Packit |
ea1746 |
dst->append(buf, result);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
delete[] buf;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
string StringPrintf(const char* format, ...) {
|
|
Packit |
ea1746 |
va_list ap;
|
|
Packit |
ea1746 |
va_start(ap, format);
|
|
Packit |
ea1746 |
string result;
|
|
Packit |
ea1746 |
StringAppendV(&result, format, ap);
|
|
Packit |
ea1746 |
va_end(ap);
|
|
Packit |
ea1746 |
return result;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const string& SStringPrintf(string* dst, const char* format, ...) {
|
|
Packit |
ea1746 |
va_list ap;
|
|
Packit |
ea1746 |
va_start(ap, format);
|
|
Packit |
ea1746 |
dst->clear();
|
|
Packit |
ea1746 |
StringAppendV(dst, format, ap);
|
|
Packit |
ea1746 |
va_end(ap);
|
|
Packit |
ea1746 |
return *dst;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void StringAppendF(string* dst, const char* format, ...) {
|
|
Packit |
ea1746 |
va_list ap;
|
|
Packit |
ea1746 |
va_start(ap, format);
|
|
Packit |
ea1746 |
StringAppendV(dst, format, ap);
|
|
Packit |
ea1746 |
va_end(ap);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|