plural-exp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Expression parsing for plural form selection.
  2. Copyright (C) 2000-2001, 2003, 2005-2007 Free Software Foundation, Inc.
  3. Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Library General Public License as published
  6. by 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 GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  15. USA. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <ctype.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "plural-exp.h"
  23. #if (defined __GNUC__ && !(__APPLE_CC__ > 1) && !defined __cplusplus) \
  24. || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
  25. /* These structs are the constant expression for the germanic plural
  26. form determination. It represents the expression "n != 1". */
  27. static const struct expression plvar =
  28. {
  29. .nargs = 0,
  30. .operation = var,
  31. };
  32. static const struct expression plone =
  33. {
  34. .nargs = 0,
  35. .operation = num,
  36. .val =
  37. {
  38. .num = 1
  39. }
  40. };
  41. struct expression GERMANIC_PLURAL =
  42. {
  43. .nargs = 2,
  44. .operation = not_equal,
  45. .val =
  46. {
  47. .args =
  48. {
  49. [0] = (struct expression *) &plvar,
  50. [1] = (struct expression *) &plone
  51. }
  52. }
  53. };
  54. # define INIT_GERMANIC_PLURAL()
  55. #else
  56. /* For compilers without support for ISO C 99 struct/union initializers:
  57. Initialization at run-time. */
  58. static struct expression plvar;
  59. static struct expression plone;
  60. struct expression GERMANIC_PLURAL;
  61. static void
  62. init_germanic_plural ()
  63. {
  64. if (plone.val.num == 0)
  65. {
  66. plvar.nargs = 0;
  67. plvar.operation = var;
  68. plone.nargs = 0;
  69. plone.operation = num;
  70. plone.val.num = 1;
  71. GERMANIC_PLURAL.nargs = 2;
  72. GERMANIC_PLURAL.operation = not_equal;
  73. GERMANIC_PLURAL.val.args[0] = &plvar;
  74. GERMANIC_PLURAL.val.args[1] = &plone;
  75. }
  76. }
  77. # define INIT_GERMANIC_PLURAL() init_germanic_plural ()
  78. #endif
  79. void
  80. internal_function
  81. EXTRACT_PLURAL_EXPRESSION (const char *nullentry,
  82. const struct expression **pluralp,
  83. unsigned long int *npluralsp)
  84. {
  85. if (nullentry != NULL)
  86. {
  87. const char *plural;
  88. const char *nplurals;
  89. plural = strstr (nullentry, "plural=");
  90. nplurals = strstr (nullentry, "nplurals=");
  91. if (plural == NULL || nplurals == NULL)
  92. goto no_plural;
  93. else
  94. {
  95. char *endp;
  96. unsigned long int n;
  97. struct parse_args args;
  98. /* First get the number. */
  99. nplurals += 9;
  100. while (*nplurals != '\0' && isspace ((unsigned char) *nplurals))
  101. ++nplurals;
  102. if (!(*nplurals >= '0' && *nplurals <= '9'))
  103. goto no_plural;
  104. #if defined HAVE_STRTOUL || defined _LIBC
  105. n = strtoul (nplurals, &endp, 10);
  106. #else
  107. for (endp = nplurals, n = 0; *endp >= '0' && *endp <= '9'; endp++)
  108. n = n * 10 + (*endp - '0');
  109. #endif
  110. if (nplurals == endp)
  111. goto no_plural;
  112. *npluralsp = n;
  113. /* Due to the restrictions bison imposes onto the interface of the
  114. scanner function we have to put the input string and the result
  115. passed up from the parser into the same structure which address
  116. is passed down to the parser. */
  117. plural += 7;
  118. args.cp = plural;
  119. if (PLURAL_PARSE (&args) != 0)
  120. goto no_plural;
  121. *pluralp = args.res;
  122. }
  123. }
  124. else
  125. {
  126. /* By default we are using the Germanic form: singular form only
  127. for `one', the plural form otherwise. Yes, this is also what
  128. English is using since English is a Germanic language. */
  129. no_plural:
  130. INIT_GERMANIC_PLURAL ();
  131. *pluralp = &GERMANIC_PLURAL;
  132. *npluralsp = 2;
  133. }
  134. }