4
0

printf-parse.h 4.5 KB

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