PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ts/ink_sprintf.cc

https://github.com/anorsich/trafficserver
C++ | 152 lines | 80 code | 19 blank | 53 comment | 21 complexity | 218ae48682b44f86c2bf940602df4f15 MD5 | raw file
Possible License(s): Apache-2.0
  1. /** @file
  2. A brief file description
  3. @section license License
  4. Licensed to the Apache Software Foundation (ASF) under one
  5. or more contributor license agreements. See the NOTICE file
  6. distributed with this work for additional information
  7. regarding copyright ownership. The ASF licenses this file
  8. to you under the Apache License, Version 2.0 (the
  9. "License"); you may not use this file except in compliance
  10. with the License. You may obtain a copy of the License at
  11. http://www.apache.org/licenses/LICENSE-2.0
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. */
  18. /*****************************************************************************
  19. ink_sprintf.cc
  20. This file implements some Inktomi variants of sprintf, to do bounds
  21. checking and length counting.
  22. ****************************************************************************/
  23. #include "ink_sprintf.h"
  24. #define NUL '\0'
  25. //////////////////////////////////////////////////////////////////////////////
  26. //
  27. // int ink_bsprintf(char *buffer, char *format, ...)
  28. // int ink_bvsprintf(char *buffer, char *format, va_list ap)
  29. //
  30. // This is a very simplified version of sprintf that has the following
  31. // behavior:
  32. //
  33. // (1) the length in output characters is returned, including final NUL
  34. // (2) buffer can be NULL, for just counting the output chars
  35. // (3) only %s and %d are supported, with no field modifiers
  36. //
  37. //////////////////////////////////////////////////////////////////////////////
  38. int
  39. ink_bsprintf(char *buffer, const char *format, ...)
  40. {
  41. int l;
  42. va_list ap;
  43. va_start(ap, format);
  44. l = ink_bvsprintf(buffer, format, ap);
  45. va_end(ap);
  46. return (l);
  47. }
  48. int
  49. ink_bvsprintf(char *buffer, const char *format, va_list ap)
  50. {
  51. int d_val;
  52. const char *s;
  53. char *d, *p, *s_val, d_buffer[32];
  54. va_list ap_local;
  55. va_copy(ap_local, ap);
  56. s = format;
  57. d = buffer;
  58. while (*s) {
  59. /////////////////////////////
  60. // handle non-% characters //
  61. /////////////////////////////
  62. if (buffer) // if have output buffer
  63. while (*s && (*s != '%')) {
  64. *d++ = *s++;
  65. } // really copy, else
  66. else
  67. while (*s && (*s != '%')) {
  68. d++;
  69. s++;
  70. } // pass over string
  71. ///////////////////////////
  72. // handle NUL characters //
  73. ///////////////////////////
  74. if (*s == NUL)
  75. break; // end of string
  76. /////////////////////////
  77. // handle % characters //
  78. /////////////////////////
  79. ++s; // consume % character
  80. switch (*s) // dispatch on flag
  81. {
  82. case 's': // %s pattern
  83. ++s; // consume 's'
  84. s_val = va_arg(ap_local, char *); // grab string argument
  85. p = s_val; // temporary pointer
  86. if (buffer) // if have output buffer
  87. while (*p) {
  88. *d++ = *p++;
  89. } // copy value
  90. else // else
  91. while (*p) {
  92. d++;
  93. p++;
  94. } // pass over value
  95. break;
  96. case 'd': // %d pattern
  97. ++s; // consume 'd'
  98. d_val = va_arg(ap_local, int); // grab integer argument
  99. snprintf(d_buffer, sizeof(d_buffer), "%d", d_val); // stringify integer
  100. p = d_buffer; // temporary pointer
  101. if (buffer) // if have output buffer
  102. while (*p) {
  103. *d++ = *p++;
  104. } // copy value
  105. else // else
  106. while (*p) {
  107. d++;
  108. p++;
  109. } // pass over value
  110. break;
  111. default: // something else
  112. if (buffer)
  113. *d = *s; // copy unknown character
  114. ++d;
  115. ++s;
  116. break;
  117. }
  118. }
  119. if (buffer)
  120. *d = NUL;
  121. ++d;
  122. va_end(ap_local);
  123. return (int) (d - buffer);
  124. }