10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer 20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/ 40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without 60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met: 70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice, 90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer. 100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice, 110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer in the documentation 120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and/or other materials provided with the distribution. 130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be 140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used to endorse or promote products derived from this software without 150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// specific prior written permission. 160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE. 280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: Sanjay Ghemawat 300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Printf variants that place their output in a C++ string. 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Usage: 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// string result = StringPrintf("%d %s\n", 10, "hello"); 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SStringPrintf(&result, "%d %s\n", 10, "hello"); 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// StringAppendF(&result, "%d %s\n", 20, "there"); 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_STRINGPRINTF_H_ 390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_STRINGPRINTF_H_ 400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <cstdarg> 420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <string> 430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/port.h" 450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres { 470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal { 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#if (defined(__GNUC__) || defined(__clang__)) 500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Tell the compiler to do printf format string checking if the compiler 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// supports it; see the 'format' attribute in 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// <http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html>. 530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// N.B.: As the GCC manual states, "[s]ince non-static C++ methods 550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// have an implicit 'this' argument, the arguments of such methods 560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// should be counted from two, not one." 570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_PRINTF_ATTRIBUTE(string_index, first_to_check) \ 580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong __attribute__((__format__ (__printf__, string_index, first_to_check))) 590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_SCANF_ATTRIBUTE(string_index, first_to_check) \ 600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong __attribute__((__format__ (__scanf__, string_index, first_to_check))) 610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#else 620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_PRINTF_ATTRIBUTE(string_index, first_to_check) 630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif 640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Return a C++ string. 660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongextern string StringPrintf(const char* format, ...) 670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Tell the compiler to do printf format string checking. 681d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling CERES_PRINTF_ATTRIBUTE(1, 2); 690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Store result into a supplied string and return it. 710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongextern const string& SStringPrintf(string* dst, const char* format, ...) 720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Tell the compiler to do printf format string checking. 731d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling CERES_PRINTF_ATTRIBUTE(2, 3); 740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Append result to a supplied string. 760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongextern void StringAppendF(string* dst, const char* format, ...) 770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Tell the compiler to do printf format string checking. 781d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling CERES_PRINTF_ATTRIBUTE(2, 3); 790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Lower-level routine that takes a va_list and appends to a specified string. 810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// All other routines are just convenience wrappers around it. 820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongextern void StringAppendV(string* dst, const char* format, va_list ap); 830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#undef CERES_PRINTF_ATTRIBUTE 850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace internal 870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace ceres 880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif // CERES_INTERNAL_STRINGPRINTF_H_ 90