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

/gettext-0.17/gettext-tools/src/format.h

https://github.com/MacIrssi/MILibs
C Header | 176 lines | 81 code | 24 blank | 71 comment | 5 complexity | f85df4b915c76b72b20faee73e46127e MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. /* Format strings.
  2. Copyright (C) 2001-2007 Free Software Foundation, Inc.
  3. Written by Bruno Haible <haible@clisp.cons.org>, 2001.
  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 of the License, or
  7. (at your option) 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
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef _FORMAT_H
  15. #define _FORMAT_H
  16. #include <stdbool.h>
  17. #include "pos.h" /* Get lex_pos_ty. */
  18. #include "message.h" /* Get NFORMATS. */
  19. #include "error.h" /* Get fallback definition of __attribute__. */
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* These indicators are set by the parse function at the appropriate
  24. positions. */
  25. enum
  26. {
  27. /* Set on the first byte of a format directive. */
  28. FMTDIR_START = 1 << 0,
  29. /* Set on the last byte of a format directive. */
  30. FMTDIR_END = 1 << 1,
  31. /* Set on the last byte of an invalid format directive, where a parse error
  32. was recognized. */
  33. FMTDIR_ERROR = 1 << 2
  34. };
  35. /* Macro for use inside a parser:
  36. Sets an indicator at the position corresponding to PTR.
  37. Assumes local variables 'fdi' and 'format_start' are defined. */
  38. #define FDI_SET(ptr, flag) \
  39. if (fdi != NULL) \
  40. fdi[(ptr) - format_start] |= (flag)/*;*/
  41. /* This type of callback is responsible for showing an error. */
  42. typedef void (*formatstring_error_logger_t) (const char *format, ...)
  43. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
  44. __attribute__ ((__format__ (__printf__, 1, 2)))
  45. #endif
  46. ;
  47. /* This structure describes a format string parser for a language. */
  48. struct formatstring_parser
  49. {
  50. /* Parse the given string as a format string.
  51. If translated is true, some extensions available only to msgstr but not
  52. to msgid strings are recognized.
  53. If fdi is non-NULL, it must be a an array of strlen (string) zero bytes.
  54. Return a freshly allocated structure describing
  55. 1. the argument types/names needed for the format string,
  56. 2. the total number of format directives.
  57. Return NULL if the string is not a valid format string. In this case,
  58. also set *invalid_reason to an error message explaining why.
  59. In both cases, set FMTDIR_* bits at the appropriate positions in fdi. */
  60. void * (*parse) (const char *string, bool translated, char *fdi, char **invalid_reason);
  61. /* Free a format string descriptor, returned by parse(). */
  62. void (*free) (void *descr);
  63. /* Return the number of format directives.
  64. A string that can be output literally has 0 format directives. */
  65. int (*get_number_of_directives) (void *descr);
  66. /* Return true if the format string, although valid, contains directives that
  67. make it appear unlikely that the string was meant as a format string.
  68. A NULL function is equivalent to a function that always returns false. */
  69. bool (*is_unlikely_intentional) (void *descr);
  70. /* Verify that the argument types/names in msgid_descr and those in
  71. msgstr_descr are the same (if equality=true), or (if equality=false)
  72. that those of msgid_descr extend those of msgstr_descr (i.e.
  73. msgstr_descr may omit some of the arguments of msgid_descr).
  74. If not, signal an error using error_logger (only if error_logger != NULL)
  75. and return true. Otherwise return false. */
  76. bool (*check) (void *msgid_descr, void *msgstr_descr, bool equality, formatstring_error_logger_t error_logger, const char *pretty_msgstr);
  77. };
  78. /* Format string parsers, each defined in its own file. */
  79. extern DLL_VARIABLE struct formatstring_parser formatstring_c;
  80. extern DLL_VARIABLE struct formatstring_parser formatstring_objc;
  81. extern DLL_VARIABLE struct formatstring_parser formatstring_sh;
  82. extern DLL_VARIABLE struct formatstring_parser formatstring_python;
  83. extern DLL_VARIABLE struct formatstring_parser formatstring_lisp;
  84. extern DLL_VARIABLE struct formatstring_parser formatstring_elisp;
  85. extern DLL_VARIABLE struct formatstring_parser formatstring_librep;
  86. extern DLL_VARIABLE struct formatstring_parser formatstring_scheme;
  87. extern DLL_VARIABLE struct formatstring_parser formatstring_smalltalk;
  88. extern DLL_VARIABLE struct formatstring_parser formatstring_java;
  89. extern DLL_VARIABLE struct formatstring_parser formatstring_csharp;
  90. extern DLL_VARIABLE struct formatstring_parser formatstring_awk;
  91. extern DLL_VARIABLE struct formatstring_parser formatstring_pascal;
  92. extern DLL_VARIABLE struct formatstring_parser formatstring_ycp;
  93. extern DLL_VARIABLE struct formatstring_parser formatstring_tcl;
  94. extern DLL_VARIABLE struct formatstring_parser formatstring_perl;
  95. extern DLL_VARIABLE struct formatstring_parser formatstring_perl_brace;
  96. extern DLL_VARIABLE struct formatstring_parser formatstring_php;
  97. extern DLL_VARIABLE struct formatstring_parser formatstring_gcc_internal;
  98. extern DLL_VARIABLE struct formatstring_parser formatstring_qt;
  99. extern DLL_VARIABLE struct formatstring_parser formatstring_kde;
  100. extern DLL_VARIABLE struct formatstring_parser formatstring_boost;
  101. /* Table of all format string parsers. */
  102. extern DLL_VARIABLE struct formatstring_parser *formatstring_parsers[NFORMATS];
  103. /* Returns an array of the ISO C 99 <inttypes.h> format directives and other
  104. format flags or directives with a system dependent expansion contained in
  105. the argument string. *intervalsp is assigned to a freshly allocated array
  106. of intervals (startpos pointing to '<', endpos to the character after '>'),
  107. and *lengthp is assigned to the number of intervals in this array. */
  108. struct interval
  109. {
  110. size_t startpos;
  111. size_t endpos;
  112. };
  113. extern void
  114. get_sysdep_c_format_directives (const char *string, bool translated,
  115. struct interval **intervalsp, size_t *lengthp);
  116. /* Returns the number of unnamed arguments consumed by a Python format
  117. string. */
  118. extern unsigned int get_python_format_unnamed_arg_count (const char *string);
  119. /* Check whether both formats strings contain compatible format
  120. specifications for format type i (0 <= i < NFORMATS).
  121. PLURAL_DISTRIBUTION is either NULL or an array of nplurals elements,
  122. PLURAL_DISTRIBUTION[j] being true if the value j appears to be assumed
  123. infinitely often by the plural formula.
  124. Return the number of errors that were seen. */
  125. extern int
  126. check_msgid_msgstr_format_i (const char *msgid, const char *msgid_plural,
  127. const char *msgstr, size_t msgstr_len,
  128. size_t i,
  129. const unsigned char *plural_distribution,
  130. unsigned long plural_distribution_length,
  131. formatstring_error_logger_t error_logger);
  132. /* Check whether both formats strings contain compatible format
  133. specifications.
  134. PLURAL_DISTRIBUTION is either NULL or an array of nplurals elements,
  135. PLURAL_DISTRIBUTION[j] being true if the value j appears to be assumed
  136. infinitely often by the plural formula.
  137. PLURAL_DISTRIBUTION_LENGTH is the length of the PLURAL_DISTRIBUTION array.
  138. Return the number of errors that were seen. */
  139. extern int
  140. check_msgid_msgstr_format (const char *msgid, const char *msgid_plural,
  141. const char *msgstr, size_t msgstr_len,
  142. const enum is_format is_format[NFORMATS],
  143. const unsigned char *plural_distribution,
  144. unsigned long plural_distribution_length,
  145. formatstring_error_logger_t error_logger);
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif /* _FORMAT_H */