printf-parse.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Parse printf format string.
  2. Copyright (C) 1999, 2002-2003, 2005, 2007, 2010-2012 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 Lesser General Public License as published by
  6. the Free Software Foundation; either version 2, 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 Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser 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. #if HAVE_FEATURES_H
  21. # include <features.h> /* for __GLIBC__, __UCLIBC__ */
  22. #endif
  23. #include "printf-args.h"
  24. /* Flags */
  25. #define FLAG_GROUP 1 /* ' flag */
  26. #define FLAG_LEFT 2 /* - flag */
  27. #define FLAG_SHOWSIGN 4 /* + flag */
  28. #define FLAG_SPACE 8 /* space flag */
  29. #define FLAG_ALT 16 /* # flag */
  30. #define FLAG_ZERO 32
  31. #if __GLIBC__ >= 2 && !defined __UCLIBC__
  32. # define FLAG_LOCALIZED 64 /* I flag, uses localized digits */
  33. #endif
  34. /* arg_index value indicating that no argument is consumed. */
  35. #define ARG_NONE (~(size_t)0)
  36. /* xxx_directive: A parsed directive.
  37. xxx_directives: A parsed format string. */
  38. /* Number of directly allocated directives (no malloc() needed). */
  39. #define N_DIRECT_ALLOC_DIRECTIVES 7
  40. /* A parsed directive. */
  41. typedef struct
  42. {
  43. const char* dir_start;
  44. const char* dir_end;
  45. int flags;
  46. const char* width_start;
  47. const char* width_end;
  48. size_t width_arg_index;
  49. const char* precision_start;
  50. const char* precision_end;
  51. size_t precision_arg_index;
  52. 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 */
  53. size_t arg_index;
  54. }
  55. char_directive;
  56. /* A parsed format string. */
  57. typedef struct
  58. {
  59. size_t count;
  60. char_directive *dir;
  61. size_t max_width_length;
  62. size_t max_precision_length;
  63. char_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
  64. }
  65. char_directives;
  66. #if ENABLE_UNISTDIO
  67. /* A parsed directive. */
  68. typedef struct
  69. {
  70. const uint8_t* dir_start;
  71. const uint8_t* dir_end;
  72. int flags;
  73. const uint8_t* width_start;
  74. const uint8_t* width_end;
  75. size_t width_arg_index;
  76. const uint8_t* precision_start;
  77. const uint8_t* precision_end;
  78. size_t precision_arg_index;
  79. 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 */
  80. size_t arg_index;
  81. }
  82. u8_directive;
  83. /* A parsed format string. */
  84. typedef struct
  85. {
  86. size_t count;
  87. u8_directive *dir;
  88. size_t max_width_length;
  89. size_t max_precision_length;
  90. u8_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
  91. }
  92. u8_directives;
  93. /* A parsed directive. */
  94. typedef struct
  95. {
  96. const uint16_t* dir_start;
  97. const uint16_t* dir_end;
  98. int flags;
  99. const uint16_t* width_start;
  100. const uint16_t* width_end;
  101. size_t width_arg_index;
  102. const uint16_t* precision_start;
  103. const uint16_t* precision_end;
  104. size_t precision_arg_index;
  105. 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 */
  106. size_t arg_index;
  107. }
  108. u16_directive;
  109. /* A parsed format string. */
  110. typedef struct
  111. {
  112. size_t count;
  113. u16_directive *dir;
  114. size_t max_width_length;
  115. size_t max_precision_length;
  116. u16_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
  117. }
  118. u16_directives;
  119. /* A parsed directive. */
  120. typedef struct
  121. {
  122. const uint32_t* dir_start;
  123. const uint32_t* dir_end;
  124. int flags;
  125. const uint32_t* width_start;
  126. const uint32_t* width_end;
  127. size_t width_arg_index;
  128. const uint32_t* precision_start;
  129. const uint32_t* precision_end;
  130. size_t precision_arg_index;
  131. 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 */
  132. size_t arg_index;
  133. }
  134. u32_directive;
  135. /* A parsed format string. */
  136. typedef struct
  137. {
  138. size_t count;
  139. u32_directive *dir;
  140. size_t max_width_length;
  141. size_t max_precision_length;
  142. u32_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
  143. }
  144. u32_directives;
  145. #endif
  146. /* Parses the format string. Fills in the number N of directives, and fills
  147. in directives[0], ..., directives[N-1], and sets directives[N].dir_start
  148. to the end of the format string. Also fills in the arg_type fields of the
  149. arguments and the needed count of arguments. */
  150. #if ENABLE_UNISTDIO
  151. extern int
  152. ulc_printf_parse (const char *format, char_directives *d, arguments *a);
  153. extern int
  154. u8_printf_parse (const uint8_t *format, u8_directives *d, arguments *a);
  155. extern int
  156. u16_printf_parse (const uint16_t *format, u16_directives *d,
  157. arguments *a);
  158. extern int
  159. u32_printf_parse (const uint32_t *format, u32_directives *d,
  160. arguments *a);
  161. #else
  162. # ifdef STATIC
  163. STATIC
  164. # else
  165. extern
  166. # endif
  167. int printf_parse (const char *format, char_directives *d, arguments *a);
  168. #endif
  169. #endif /* _PRINTF_PARSE_H */