nl_langinfo.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* User interface for extracting locale-dependent parameters.
  2. Copyright (C) 1995, 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library 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 the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <stddef.h>
  17. #include <time.h>
  18. #include <langinfo.h>
  19. #include <limits.h>
  20. #include <errno.h>
  21. #include <locale.h>
  22. #include <langinfo.h>
  23. #include <sys/types.h>
  24. /* Structure describing locale data in core for a category. */
  25. struct locale_data
  26. {
  27. const char *name;
  28. const char *filedata; /* Region mapping the file data. */
  29. off_t filesize; /* Size of the file (and the region). */
  30. int mmaped; /* If nonzero the data is mmaped. */
  31. unsigned int usage_count; /* Counter for users. */
  32. unsigned int nstrings; /* Number of strings below. */
  33. union locale_data_value
  34. {
  35. const wchar_t *wstr;
  36. const char *string;
  37. unsigned int word;
  38. }
  39. values[0]; /* Items, usually pointers into `filedata'. */
  40. };
  41. /* For each category declare two external variables (with weak references):
  42. * extern const struct locale_data *_nl_current_CATEGORY;
  43. * This points to the current locale's in-core data for CATEGORY.
  44. * extern const struct locale_data _nl_C_CATEGORY;
  45. * This contains the built-in "C"/"POSIX" locale's data for CATEGORY.
  46. * Both are weak references; if &_nl_current_CATEGORY is zero,
  47. * then nothing is using the locale data. */
  48. #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
  49. extern struct locale_data *_nl_current_##category; \
  50. extern struct locale_data _nl_C_##category;
  51. #include "categories.c"
  52. #undef DEFINE_CATEGORY
  53. /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
  54. * Elements are zero for categories whose data is never used. */
  55. struct locale_data *const _nl_C[] =
  56. {
  57. #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
  58. [category] = &_nl_C_##category,
  59. #include "categories.c"
  60. #undef DEFINE_CATEGORY
  61. };
  62. /* Return a string with the data for locale-dependent parameter ITEM. */
  63. char * nl_langinfo (nl_item item)
  64. {
  65. int category = _NL_ITEM_CATEGORY (item);
  66. unsigned int index = _NL_ITEM_INDEX (item);
  67. const struct locale_data *data;
  68. if (category < 0 || category >= LC_ALL)
  69. /* Bogus category: bogus item. */
  70. return (char *) "";
  71. data = _nl_C[category];
  72. if (index >= data->nstrings)
  73. /* Bogus index for this category: bogus item. */
  74. return (char *) "";
  75. /* Return the string for the specified item. */
  76. return (char *) data->values[index].string;
  77. }