诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

177 rivejä
6.2 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2012 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // from google3/base/stringprintf.cc
  31. #include <google/protobuf/stubs/stringprintf.h>
  32. #include <errno.h>
  33. #include <stdarg.h> // For va_list and related operations
  34. #include <stdio.h> // MSVC requires this for _vsnprintf
  35. #include <vector>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/stubs/logging.h>
  38. namespace google {
  39. namespace protobuf {
  40. #ifdef _MSC_VER
  41. enum { IS_COMPILER_MSVC = 1 };
  42. #ifndef va_copy
  43. // Define va_copy for MSVC. This is a hack, assuming va_list is simply a
  44. // pointer into the stack and is safe to copy.
  45. #define va_copy(dest, src) ((dest) = (src))
  46. #endif
  47. #else
  48. enum { IS_COMPILER_MSVC = 0 };
  49. #endif
  50. void StringAppendV(string* dst, const char* format, va_list ap) {
  51. // First try with a small fixed size buffer
  52. static const int kSpaceLength = 1024;
  53. char space[kSpaceLength];
  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, kSpaceLength, format, backup_ap);
  60. va_end(backup_ap);
  61. if (result < kSpaceLength) {
  62. if (result >= 0) {
  63. // Normal case -- everything fit.
  64. dst->append(space, result);
  65. return;
  66. }
  67. if (IS_COMPILER_MSVC) {
  68. // Error or MSVC running out of space. MSVC 8.0 and higher
  69. // can be asked about space needed with the special idiom below:
  70. va_copy(backup_ap, ap);
  71. result = vsnprintf(nullptr, 0, format, backup_ap);
  72. va_end(backup_ap);
  73. }
  74. if (result < 0) {
  75. // Just an error.
  76. return;
  77. }
  78. }
  79. // Increase the buffer size to the size requested by vsnprintf,
  80. // plus one for the closing \0.
  81. int length = result+1;
  82. char* buf = new char[length];
  83. // Restore the va_list before we use it again
  84. va_copy(backup_ap, ap);
  85. result = vsnprintf(buf, length, format, backup_ap);
  86. va_end(backup_ap);
  87. if (result >= 0 && result < length) {
  88. // It fit
  89. dst->append(buf, result);
  90. }
  91. delete[] buf;
  92. }
  93. string StringPrintf(const char* format, ...) {
  94. va_list ap;
  95. va_start(ap, format);
  96. string result;
  97. StringAppendV(&result, format, ap);
  98. va_end(ap);
  99. return result;
  100. }
  101. const string& SStringPrintf(string* dst, const char* format, ...) {
  102. va_list ap;
  103. va_start(ap, format);
  104. dst->clear();
  105. StringAppendV(dst, format, ap);
  106. va_end(ap);
  107. return *dst;
  108. }
  109. void StringAppendF(string* dst, const char* format, ...) {
  110. va_list ap;
  111. va_start(ap, format);
  112. StringAppendV(dst, format, ap);
  113. va_end(ap);
  114. }
  115. // Max arguments supported by StringPrintVector
  116. const int kStringPrintfVectorMaxArgs = 32;
  117. // An empty block of zero for filler arguments. This is const so that if
  118. // printf tries to write to it (via %n) then the program gets a SIGSEGV
  119. // and we can fix the problem or protect against an attack.
  120. static const char string_printf_empty_block[256] = { '\0' };
  121. string StringPrintfVector(const char* format, const std::vector<string>& v) {
  122. GOOGLE_CHECK_LE(v.size(), kStringPrintfVectorMaxArgs)
  123. << "StringPrintfVector currently only supports up to "
  124. << kStringPrintfVectorMaxArgs << " arguments. "
  125. << "Feel free to add support for more if you need it.";
  126. // Add filler arguments so that bogus format+args have a harder time
  127. // crashing the program, corrupting the program (%n),
  128. // or displaying random chunks of memory to users.
  129. const char* cstr[kStringPrintfVectorMaxArgs];
  130. for (int i = 0; i < v.size(); ++i) {
  131. cstr[i] = v[i].c_str();
  132. }
  133. for (int i = v.size(); i < GOOGLE_ARRAYSIZE(cstr); ++i) {
  134. cstr[i] = &string_printf_empty_block[0];
  135. }
  136. // I do not know any way to pass kStringPrintfVectorMaxArgs arguments,
  137. // or any way to build a va_list by hand, or any API for printf
  138. // that accepts an array of arguments. The best I can do is stick
  139. // this COMPILE_ASSERT right next to the actual statement.
  140. GOOGLE_COMPILE_ASSERT(kStringPrintfVectorMaxArgs == 32, arg_count_mismatch);
  141. return StringPrintf(format,
  142. cstr[0], cstr[1], cstr[2], cstr[3], cstr[4],
  143. cstr[5], cstr[6], cstr[7], cstr[8], cstr[9],
  144. cstr[10], cstr[11], cstr[12], cstr[13], cstr[14],
  145. cstr[15], cstr[16], cstr[17], cstr[18], cstr[19],
  146. cstr[20], cstr[21], cstr[22], cstr[23], cstr[24],
  147. cstr[25], cstr[26], cstr[27], cstr[28], cstr[29],
  148. cstr[30], cstr[31]);
  149. }
  150. } // namespace protobuf
  151. } // namespace google