stringprintf.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  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. #include "ceres/stringprintf.h"
  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. #include "ceres/internal/export.h"
  37. namespace ceres::internal {
  38. void StringAppendV(std::string* dst, const char* format, va_list ap) {
  39. // First try with a small fixed size buffer
  40. char space[1024];
  41. // It's possible for methods that use a va_list to invalidate
  42. // the data in it upon use. The fix is to make a copy
  43. // of the structure before using it and use that copy instead.
  44. va_list backup_ap;
  45. va_copy(backup_ap, ap);
  46. int result = vsnprintf(space, sizeof(space), format, backup_ap);
  47. va_end(backup_ap);
  48. if (result < sizeof(space)) {
  49. if (result >= 0) {
  50. // Normal case -- everything fit.
  51. dst->append(space, result);
  52. return;
  53. }
  54. #if defined(_MSC_VER)
  55. // Error or MSVC running out of space. MSVC 8.0 and higher
  56. // can be asked about space needed with the special idiom below:
  57. va_copy(backup_ap, ap);
  58. result = vsnprintf(nullptr, 0, format, backup_ap);
  59. va_end(backup_ap);
  60. #endif
  61. if (result < 0) {
  62. // Just an error.
  63. return;
  64. }
  65. }
  66. // Increase the buffer size to the size requested by vsnprintf,
  67. // plus one for the closing \0.
  68. int length = result + 1;
  69. char* buf = new char[length];
  70. // Restore the va_list before we use it again
  71. va_copy(backup_ap, ap);
  72. result = vsnprintf(buf, length, format, backup_ap);
  73. va_end(backup_ap);
  74. if (result >= 0 && result < length) {
  75. // It fit
  76. dst->append(buf, result);
  77. }
  78. delete[] buf;
  79. }
  80. std::string StringPrintf(const char* format, ...) {
  81. va_list ap;
  82. va_start(ap, format);
  83. std::string result;
  84. StringAppendV(&result, format, ap);
  85. va_end(ap);
  86. return result;
  87. }
  88. const std::string& SStringPrintf(std::string* dst, const char* format, ...) {
  89. va_list ap;
  90. va_start(ap, format);
  91. dst->clear();
  92. StringAppendV(dst, format, ap);
  93. va_end(ap);
  94. return *dst;
  95. }
  96. void StringAppendF(std::string* dst, const char* format, ...) {
  97. va_list ap;
  98. va_start(ap, format);
  99. StringAppendV(dst, format, ap);
  100. va_end(ap);
  101. }
  102. } // namespace ceres::internal