service_my_snprintf.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef MYSQL_SERVICE_MY_SNPRINTF_INCLUDED
  2. /* Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. /**
  14. @file
  15. my_snprintf service
  16. Portable and limited vsnprintf() implementation.
  17. This is a portable, limited vsnprintf() implementation, with some
  18. extra features. "Portable" means that it'll produce identical result
  19. on all platforms (for example, on Windows and Linux system printf %e
  20. formats the exponent differently, on different systems %p either
  21. prints leading 0x or not, %s may accept null pointer or crash on
  22. it). "Limited" means that it does not support all the C89 features.
  23. But it supports few extensions, not in any standard.
  24. my_vsnprintf(to, n, fmt, ap)
  25. @param[out] to A buffer to store the result in
  26. @param[in] n Store up to n-1 characters, followed by an end 0
  27. @param[in] fmt printf-like format string
  28. @param[in] ap Arguments
  29. @return a number of bytes written to a buffer *excluding* terminating '\0'
  30. @post
  31. The syntax of a format string is generally the same:
  32. % <flag> <width> <precision> <length modifier> <format>
  33. where everithing but the format is optional.
  34. Three one-character flags are recognized:
  35. '0' has the standard zero-padding semantics;
  36. '-' is parsed, but silently ignored;
  37. '`' (backtick) is only supported for strings (%s) and means that the
  38. string will be quoted according to MySQL identifier quoting rules.
  39. Both <width> and <precision> can be specified as numbers or '*'.
  40. If an asterisk is used, an argument of type int is consumed.
  41. <length modifier> can be 'l', 'll', or 'z'.
  42. Supported formats are 's' (null pointer is accepted, printed as
  43. "(null)"), 'b' (extension, see below), 'c', 'd', 'i', 'u', 'x', 'o',
  44. 'X', 'p' (works as 0x%x), 'f', 'g', 'M' (extension, see below).
  45. Standard syntax for positional arguments $n is supported.
  46. Extensions:
  47. Flag '`' (backtick): see above.
  48. Format 'b': binary buffer, prints exactly <precision> bytes from the
  49. argument, without stopping at '\0'.
  50. Format 'M': takes one integer, prints this integer, space, double quote
  51. error message, double quote. In other words
  52. printf("%M", n) === printf("%d \"%s\"", n, strerror(n))
  53. */
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #ifndef MYSQL_ABI_CHECK
  58. #include <stdarg.h>
  59. #include <stdlib.h>
  60. #endif
  61. extern struct my_snprintf_service_st {
  62. size_t (*my_snprintf_type)(char*, size_t, const char*, ...);
  63. size_t (*my_vsnprintf_type)(char *, size_t, const char*, va_list);
  64. } *my_snprintf_service;
  65. #ifdef MYSQL_DYNAMIC_PLUGIN
  66. #define my_vsnprintf my_snprintf_service->my_vsnprintf_type
  67. #define my_snprintf my_snprintf_service->my_snprintf_type
  68. #else
  69. size_t my_snprintf(char* to, size_t n, const char* fmt, ...);
  70. size_t my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap);
  71. #endif
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. #define MYSQL_SERVICE_MY_SNPRINTF_INCLUDED
  76. #endif