~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to extern/libmv/third_party/ceres/internal/ceres/stringprintf.cc

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

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