You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plural-exp.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Expression parsing for plural form selection.
  2. Copyright (C) 2000-2001, 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  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__ && !defined __APPLE_CC__) \
  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, struct expression **pluralp,
  82. unsigned long int *npluralsp)
  83. {
  84. if (nullentry != NULL)
  85. {
  86. const char *plural;
  87. const char *nplurals;
  88. plural = strstr (nullentry, "plural=");
  89. nplurals = strstr (nullentry, "nplurals=");
  90. if (plural == NULL || nplurals == NULL)
  91. goto no_plural;
  92. else
  93. {
  94. char *endp;
  95. unsigned long int n;
  96. struct parse_args args;
  97. /* First get the number. */
  98. nplurals += 9;
  99. while (*nplurals != '\0' && isspace ((unsigned char) *nplurals))
  100. ++nplurals;
  101. if (!(*nplurals >= '0' && *nplurals <= '9'))
  102. goto no_plural;
  103. #if defined HAVE_STRTOUL || defined _LIBC
  104. n = strtoul (nplurals, &endp, 10);
  105. #else
  106. for (endp = nplurals, n = 0; *endp >= '0' && *endp <= '9'; endp++)
  107. n = n * 10 + (*endp - '0');
  108. #endif
  109. if (nplurals == endp)
  110. goto no_plural;
  111. *npluralsp = n;
  112. /* Due to the restrictions bison imposes onto the interface of the
  113. scanner function we have to put the input string and the result
  114. passed up from the parser into the same structure which address
  115. is passed down to the parser. */
  116. plural += 7;
  117. args.cp = plural;
  118. if (PLURAL_PARSE (&args) != 0)
  119. goto no_plural;
  120. *pluralp = args.res;
  121. }
  122. }
  123. else
  124. {
  125. /* By default we are using the Germanic form: singular form only
  126. for `one', the plural form otherwise. Yes, this is also what
  127. English is using since English is a Germanic language. */
  128. no_plural:
  129. INIT_GERMANIC_PLURAL ();
  130. *pluralp = &GERMANIC_PLURAL;
  131. *npluralsp = 2;
  132. }
  133. }