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// Printf variants that place their output in a C++ string.
32//
33// Usage:
34//      string result = StringPrintf("%d %s\n", 10, "hello");
35//      SStringPrintf(&result, "%d %s\n", 10, "hello");
36//      StringAppendF(&result, "%d %s\n", 20, "there");
37
38#ifndef CERES_INTERNAL_STRINGPRINTF_H_
39#define CERES_INTERNAL_STRINGPRINTF_H_
40
41#include <cstdarg>
42#include <string>
43
44#include "ceres/internal/port.h"
45
46namespace ceres {
47namespace internal {
48
49#if (defined(__GNUC__) || defined(__clang__))
50// Tell the compiler to do printf format string checking if the compiler
51// supports it; see the 'format' attribute in
52// <http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html>.
53//
54// N.B.: As the GCC manual states, "[s]ince non-static C++ methods
55// have an implicit 'this' argument, the arguments of such methods
56// should be counted from two, not one."
57#define CERES_PRINTF_ATTRIBUTE(string_index, first_to_check) \
58    __attribute__((__format__ (__printf__, string_index, first_to_check)))
59#define CERES_SCANF_ATTRIBUTE(string_index, first_to_check) \
60    __attribute__((__format__ (__scanf__, string_index, first_to_check)))
61#else
62#define CERES_PRINTF_ATTRIBUTE(string_index, first_to_check)
63#endif
64
65// Return a C++ string.
66extern string StringPrintf(const char* format, ...)
67    // Tell the compiler to do printf format string checking.
68    CERES_PRINTF_ATTRIBUTE(1, 2);
69
70// Store result into a supplied string and return it.
71extern const string& SStringPrintf(string* dst, const char* format, ...)
72    // Tell the compiler to do printf format string checking.
73    CERES_PRINTF_ATTRIBUTE(2, 3);
74
75// Append result to a supplied string.
76extern void StringAppendF(string* dst, const char* format, ...)
77    // Tell the compiler to do printf format string checking.
78    CERES_PRINTF_ATTRIBUTE(2, 3);
79
80// Lower-level routine that takes a va_list and appends to a specified string.
81// All other routines are just convenience wrappers around it.
82extern void StringAppendV(string* dst, const char* format, va_list ap);
83
84#undef CERES_PRINTF_ATTRIBUTE
85
86}  // namespace internal
87}  // namespace ceres
88
89#endif  // CERES_INTERNAL_STRINGPRINTF_H_
90