printf-parse.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Parse printf format string.
  2. Copyright (C) 1999, 2002-2003, 2005, 2007, 2009-2010 Free Software
  3. Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef _PRINTF_PARSE_H
  16. #define _PRINTF_PARSE_H
  17. /* This file can be parametrized with the following macros:
  18. ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
  19. STATIC Set to 'static' to declare the function static. */
  20. #include "printf-args.h"
  21. /* Flags */
  22. #define FLAG_GROUP 1 /* ' flag */
  23. #define FLAG_LEFT 2 /* - flag */
  24. #define FLAG_SHOWSIGN 4 /* + flag */
  25. #define FLAG_SPACE 8 /* space flag */
  26. #define FLAG_ALT 16 /* # flag */
  27. #define FLAG_ZERO 32
  28. /* arg_index value indicating that no argument is consumed. */
  29. #define ARG_NONE (~(size_t)0)
  30. /* xxx_directive: A parsed directive.
  31. xxx_directives: A parsed format string. */
  32. /* A parsed directive. */
  33. typedef struct
  34. {
  35. const char* dir_start;
  36. const char* dir_end;
  37. int flags;
  38. const char* width_start;
  39. const char* width_end;
  40. size_t width_arg_index;
  41. const char* precision_start;
  42. const char* precision_end;
  43. size_t precision_arg_index;
  44. char conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
  45. size_t arg_index;
  46. }
  47. char_directive;
  48. /* A parsed format string. */
  49. typedef struct
  50. {
  51. size_t count;
  52. char_directive *dir;
  53. size_t max_width_length;
  54. size_t max_precision_length;
  55. }
  56. char_directives;
  57. #if ENABLE_UNISTDIO
  58. /* A parsed directive. */
  59. typedef struct
  60. {
  61. const uint8_t* dir_start;
  62. const uint8_t* dir_end;
  63. int flags;
  64. const uint8_t* width_start;
  65. const uint8_t* width_end;
  66. size_t width_arg_index;
  67. const uint8_t* precision_start;
  68. const uint8_t* precision_end;
  69. size_t precision_arg_index;
  70. uint8_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
  71. size_t arg_index;
  72. }
  73. u8_directive;
  74. /* A parsed format string. */
  75. typedef struct
  76. {
  77. size_t count;
  78. u8_directive *dir;
  79. size_t max_width_length;
  80. size_t max_precision_length;
  81. }
  82. u8_directives;
  83. /* A parsed directive. */
  84. typedef struct
  85. {
  86. const uint16_t* dir_start;
  87. const uint16_t* dir_end;
  88. int flags;
  89. const uint16_t* width_start;
  90. const uint16_t* width_end;
  91. size_t width_arg_index;
  92. const uint16_t* precision_start;
  93. const uint16_t* precision_end;
  94. size_t precision_arg_index;
  95. uint16_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
  96. size_t arg_index;
  97. }
  98. u16_directive;
  99. /* A parsed format string. */
  100. typedef struct
  101. {
  102. size_t count;
  103. u16_directive *dir;
  104. size_t max_width_length;
  105. size_t max_precision_length;
  106. }
  107. u16_directives;
  108. /* A parsed directive. */
  109. typedef struct
  110. {
  111. const uint32_t* dir_start;
  112. const uint32_t* dir_end;
  113. int flags;
  114. const uint32_t* width_start;
  115. const uint32_t* width_end;
  116. size_t width_arg_index;
  117. const uint32_t* precision_start;
  118. const uint32_t* precision_end;
  119. size_t precision_arg_index;
  120. uint32_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
  121. size_t arg_index;
  122. }
  123. u32_directive;
  124. /* A parsed format string. */
  125. typedef struct
  126. {
  127. size_t count;
  128. u32_directive *dir;
  129. size_t max_width_length;
  130. size_t max_precision_length;
  131. }
  132. u32_directives;
  133. #endif
  134. /* Parses the format string. Fills in the number N of directives, and fills
  135. in directives[0], ..., directives[N-1], and sets directives[N].dir_start
  136. to the end of the format string. Also fills in the arg_type fields of the
  137. arguments and the needed count of arguments. */
  138. #if ENABLE_UNISTDIO
  139. extern int
  140. ulc_printf_parse (const char *format, char_directives *d, arguments *a);
  141. extern int
  142. u8_printf_parse (const uint8_t *format, u8_directives *d, arguments *a);
  143. extern int
  144. u16_printf_parse (const uint16_t *format, u16_directives *d,
  145. arguments *a);
  146. extern int
  147. u32_printf_parse (const uint32_t *format, u32_directives *d,
  148. arguments *a);
  149. #else
  150. # ifdef STATIC
  151. STATIC
  152. # else
  153. extern
  154. # endif
  155. int printf_parse (const char *format, char_directives *d, arguments *a);
  156. #endif
  157. #endif /* _PRINTF_PARSE_H */