1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2010-2018, Free Software Foundation, Inc. *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
32 /* This file provides OS-dependent support for the Ada.Locales package. */
38 typedef char char4
[4];
40 /* Table containing equivalences between ISO_639_1 codes and their ISO_639_3
41 alpha-3 code plus their language name. */
43 static char* iso_639
[] =
46 "ab", "abk", "Abkhazian",
47 "ae", "ave", "Avestan",
48 "af", "afr", "Afrikaans",
50 "am", "amh", "Amharic",
51 "an", "arg", "Aragonese",
52 "ar", "ara", "Arabic",
53 "as", "asm", "Assamese",
54 "av", "ava", "Avaric",
55 "ay", "aym", "Aymara",
56 "az", "aze", "Azerbaijani",
58 "ba", "bak", "Bashkir",
59 "be", "bel", "Belarusian",
60 "bg", "bul", "Bulgarian",
61 "bi", "bis", "Bislama",
62 "bm", "bam", "Bambara",
63 "bn", "ben", "Bengali",
64 "bo", "bod", "Tibetan",
65 "br", "bre", "Breton",
66 "bs", "bos", "Bosnian",
68 "ca", "cat", "Catalan",
69 "ce", "che", "Chechen",
70 "ch", "cha", "Chamorro",
71 "co", "cos", "Corsican",
74 "cu", "chu", "Church Slavic",
75 "cv", "chv", "Chuvash",
78 "da", "dan", "Danish",
79 "de", "deu", "German",
80 "dv", "div", "Divehi",
81 "dz", "dzo", "Dzongkha",
84 "el", "ell", "Modern Greek",
85 "en", "eng", "English",
86 "eo", "epo", "Esperanto",
87 "es", "spa", "Spanish",
88 "et", "est", "Estonian",
89 "eu", "eus", "Basque",
91 "fa", "fas", "Persian",
93 "fi", "fin", "Finnish",
94 "fj", "fij", "Fijian",
95 "fo", "fao", "Faroese",
96 "fr", "fra", "French",
97 "fy", "fry", "Western Frisian",
100 "gd", "gla", "Scottish Gaelic",
101 "gl", "glg", "Galician",
102 "gn", "grn", "Guarani",
103 "gu", "guj", "Gujarati",
106 "ha", "hau", "Hausa",
107 "he", "heb", "Hebrew",
108 "hi", "hin", "Hindi",
109 "ho", "hmo", "Hiri Motu",
110 "hr", "hrv", "Croatian",
111 "ht", "hat", "Haitian",
112 "hu", "hun", "Hungarian",
113 "hy", "hye", "Armenian",
114 "hz", "her", "Herero",
116 "ia", "ina", "Interlingua",
117 "id", "ind", "Indonesian",
118 "ie", "ile", "Interlingue",
120 "ii", "iii", "Sichuan Yi",
121 "ik", "ipk", "Inupiaq",
123 "is", "isl", "Icelandic",
124 "it", "ita", "Italian",
125 "iu", "iku", "Inuktitut",
127 "ja", "jpn", "Japanese",
128 "jv", "jav", "Javanese",
130 "ka", "kat", "Georgian",
131 "kg", "kon", "Kongo",
132 "ki", "kik", "Kikuyu",
133 "kj", "kua", "Kuanyama",
134 "kk", "kaz", "Kazakh",
135 "kl", "kal", "Kalaallisut",
136 "km", "khm", "Central Khmer",
137 "kn", "kan", "Kannada",
138 "ko", "kor", "Korean",
139 "kr", "kau", "Kanuri",
140 "ks", "kas", "Kashmiri",
141 "ku", "kur", "Kurdish",
143 "kw", "cor", "Cornish",
144 "ky", "kir", "Kirghiz",
146 "la", "lat", "Latin",
147 "lb", "ltz", "Luxembourgish",
148 "lg", "lug", "Ganda",
149 "li", "lim", "Limburgan",
150 "ln", "lin", "Lingala",
152 "lt", "lit", "Lithuanian",
153 "lu", "lub", "Luba-Katanga",
154 "lv", "lav", "Latvian",
156 "mg", "mlg", "Malagasy",
157 "mh", "mah", "Marshallese",
158 "mi", "mri", "Maori",
159 "mk", "mkd", "Macedonian",
160 "ml", "mal", "Malayalam",
161 "mn", "mon", "Mongolian",
162 "mr", "mar", "Marathi",
163 "ms", "msa", "Malay",
164 "mt", "mlt", "Maltese",
165 "my", "mya", "Burmese",
167 "na", "nau", "Nauru",
168 "nb", "nob", "Norwegian Bokmal",
169 "nd", "nde", "North Ndebele",
170 "ne", "nep", "Nepali",
171 "ng", "ndo", "Ndonga",
172 "nl", "nld", "Dutch",
173 "nn", "nno", "Norwegian Nynorsk",
174 "no", "nor", "Norwegian",
175 "nr", "nbl", "South Ndebele",
176 "nv", "nav", "Navajo",
177 "ny", "nya", "Nyanja",
179 "oc", "oci", "Occitan",
180 "oj", "oji", "Ojibwa",
181 "om", "orm", "Oromo",
182 "or", "ori", "Oriya",
183 "os", "oss", "Ossetian",
185 "pa", "pan", "Panjabi",
187 "pl", "pol", "Polish",
188 "ps", "pus", "Pushto",
189 "pt", "por", "Portuguese",
191 "qu", "que", "Quechua",
193 "rm", "roh", "Romansh",
194 "rn", "run", "Rundi",
195 "ro", "ron", "Romanian",
196 "ru", "rus", "Russian",
197 "rw", "kin", "Kinyarwanda",
199 "sa", "san", "Sanskrit",
200 "sc", "srd", "Sardinian",
201 "sd", "snd", "Sindhi",
202 "se", "sme", "Northern Sami",
203 "sg", "sag", "Sango",
204 "sh", "hbs", "Serbo-Croatian",
205 "si", "sin", "Sinhala",
206 "sk", "slk", "Slovak",
207 "sl", "slv", "Slovenian",
208 "sm", "smo", "Samoan",
209 "sn", "sna", "Shona",
210 "so", "som", "Somali",
211 "sq", "sqi", "Albanian",
212 "sr", "srp", "Serbian",
213 "ss", "ssw", "Swati",
214 "st", "sot", "Southern Sotho",
215 "su", "sun", "Sundanese",
216 "sv", "swe", "Swedish",
217 "sw", "swa", "Swahili",
219 "ta", "tam", "Tamil",
220 "te", "tel", "Telugu",
221 "tg", "tgk", "Tajik",
223 "ti", "tir", "Tigrinya",
224 "tk", "tuk", "Turkmen",
225 "tl", "tgl", "Tagalog",
226 "tn", "tsn", "Tswana",
227 "to", "ton", "Tonga",
228 "tr", "tur", "Turkish",
229 "ts", "tso", "Tsonga",
230 "tt", "tat", "Tatar",
232 "ty", "tah", "Tahitian",
234 "ug", "uig", "Uighur",
235 "uk", "ukr", "Ukrainian",
237 "uz", "uzb", "Uzbek",
239 "ve", "ven", "Venda",
240 "vi", "vie", "Vietnamese",
241 "vo", "vol", "Volapuk",
243 "wa", "wln", "Walloon",
244 "wo", "wol", "Wolof",
246 "xh", "xho", "Xhosa",
248 "yi", "yid", "Yiddish",
249 "yo", "yor", "Yoruba",
251 "za", "zha", "Zhuang",
252 "zh", "zho", "Chinese",
256 /* Table containing equivalences between ISO_3166 alpha-2 codes and country
257 names. This table has several entries for codes that have several valid
260 static char* iso_3166
[] =
264 "AE", "United Arab Emirates",
266 "AG", "Antigua and Barbuda",
270 "AN", "Netherlands Antilles",
274 "AS", "American Samoa",
278 "AX", "Aland Islands",
281 "BA", "Bosnia and Herzegovina",
285 "BF", "Burkina Faso",
290 "BL", "Saint Barthélemy",
292 "BN", "Brunei Darussalam",
293 "BO", "Bolivia, Plurinational State of",
294 "BQ", "Bonaire, Sint Eustatius and Saba",
298 "BV", "Bouvet Island",
304 "CC", "Cocos (Keeling) Islands",
305 "CD", "Congo, Democratic Republic of the",
306 "CF", "Central African Republic",
309 "CI", "Côte d'Ivoire",
310 "CK", "Cook Islands",
314 "CN", "People’s Republic of China",
319 "CS", "Czechoslovakia",
323 "CX", "Christmas Island",
325 "CZ", "Czech Republic",
331 "DO", "Dominican Republic",
337 "EH", "Western Sahara",
344 "FK", "Falkland Islands (Malvinas)",
345 "FM", "Micronesia, Federated States of",
346 "FO", "Faroe Islands",
350 "GB", "United Kingdom",
351 "GB", "United-Kingdom",
354 "GB", "Great Britain",
357 "GF", "French Guiana",
365 "GQ", "Equatorial Guinea",
367 "GS", "South Georgia and the South Sandwich Islands",
370 "GW", "Guinea-Bissau",
375 "HM", "Heard Island and McDonald Islands",
386 "IO", "British Indian Ocean Territory",
389 "IR", "Iran, Islamic Republic of",
403 "KN", "Saint Kitts and Nevis",
404 "KP", "Korea, Democratic People's Republic of",
405 "KR", "Korea, Republic of",
407 "KY", "Cayman Islands",
410 "LA", "Lao People's Democratic Republic",
413 "LI", "Liechtenstein",
424 "MD", "Moldova, Republic of",
426 "MF", "Saint Martin",
428 "MH", "Marshall Islands",
434 "MP", "Northern Mariana Islands",
447 "NC", "New Caledonia",
449 "NF", "Norfolk Island",
465 "PF", "French Polynesia",
466 "PG", "Papua New Guinea",
470 "PM", "Saint Pierre and Miquelon",
473 "PS", "Palestine, State of",
483 "RU", "Russian Federation",
486 "SA", "Saudi Arabia",
487 "SB", "Solomon Islands",
492 "SH", "Saint Helena, Ascension and Tristan da Cunha",
494 "SJ", "Svalbard and Jan Mayen",
496 "SL", "Sierra Leone",
503 "SX", "Sint Maarten (Dutch part)",
504 "SY", "Syrian Arab Republic",
507 "TC", "Turks and Caicos Islands",
509 "TF", "French Southern Territories",
515 "TM", "Turkmenistan",
520 "TT", "Trinidad and Tobago",
523 "TW", "Taiwan, Province of China",
525 "TZ", "Tanzania, United Republic of",
529 "UM", "United States Minor Outlying Islands",
530 "US", "United States",
531 "US", "United States of America",
532 "US", "United-States"
536 "VA", "Holy See (Vatican City State)",
537 "VC", "Saint Vincent and the Grenadines",
539 "VE", "Venezuela, Bolivarian Republic of",
540 "VG", "Virgin Islands, British",
541 "VI", "Virgin Islands, U.S.",
544 "WF", "Wallis and Futuna",
551 "ZA", "South Africa",
556 /* Utility function to perform case insensitive string comparison. Returns 1
557 if both strings are equal and 0 otherwise. */
560 str_case_equals (const char *s1
, const char *s2
) {
561 while (*s1
!= '\0' && *s2
!= '\0' && tolower(*s1
) == tolower(*s2
)) {
566 return (*s1
== '\0') && (*s2
== '\0');
569 /* Utility function to copy length characters of a string. The target string
570 must have space to store the extra string null terminator. */
573 str_copy (char *target
, char *source
, int length
) {
574 for (; length
> 0; source
++, target
++, length
--) {
581 /* Utility function to search for the last byte of the lc_all string to be
582 processed. Required because in some targets (for example, AIX), the
583 string returned by setlocale() has duplicates. */
586 str_get_last_byte (char *lc_all
) {
587 char* first_space
= NULL
;
588 char* second_space
= NULL
;
589 char* last_byte
= NULL
;
592 /* Search for the 1st space (if any) */
593 while (*s1
!= ' ' && *s1
!= '\0')
602 /* Skip this space and search for the 2nd one (if available) */
604 while (*s1
!= ' ' && *s1
!= '\0')
613 /* Search for the last byte of lc_all */
619 /* Check if the two strings match */
621 int len1
= first_space
- lc_all
;
622 int len2
= second_space
- first_space
- 1;
626 char* p2
= first_space
+ 1;
628 /* Compare their contents */
629 while (*p1
== *p2
&& p2
!= second_space
) {
634 /* if the two strings match then update the last byte */
636 if (p2
== second_space
) {
637 last_byte
= first_space
;
647 /* Utility function to search in the iso_639_1 table for an iso-639-1 code;
648 returns the corresponding iso-639-3 code or NULL if not found. */
651 iso_639_1_to_639_3(char* iso_639_1_code
) {
652 int len
= sizeof(iso_639
)/sizeof(iso_639
[0]);
656 for (j
=0; j
< len
/3; j
++) {
657 char* s1
= iso_639_1_code
;
660 if (s1
[0]==s2
[0] && s1
[1]==s2
[1]) {
671 /* Utility function to search in the iso_639_1 table for a language name;
672 returns the corresponding iso-639-3 code or NULL if not found. */
675 language_name_to_639_3(char* name
) {
676 int len
= sizeof(iso_639
)/sizeof(iso_639
[0]);
681 for (j
=0; j
< len
/3; j
++) {
682 if (str_case_equals(name
, *p
)) {
693 /* Utility function to search in the iso_3166 table for a country name;
694 returns the corresponding iso-3166 code or NULL if not found. */
697 country_name_to_3166 (char* name
) {
698 int len
= sizeof(iso_3166
)/sizeof(iso_3166
[0]);
703 for (j
=0; j
< len
/2; j
++) {
704 if (str_case_equals(name
, *p
)) {
716 c_get_language_code needs to fill in the Alpha-3 encoding of the
717 language code (3 lowercase letters). That should be "und" if the
718 language is unknown. [see Ada.Locales]
721 c_get_language_code (char4 p
) {
722 char* Saved_Locale
= setlocale(LC_ALL
, NULL
);
723 char iso_639_3_code
[] = "und"; /* Language Unknown */
727 /* Get locales set in the environment */
729 setlocale(LC_ALL
, "");
730 lc_all
= setlocale(LC_ALL
, NULL
);
732 /* The string returned by setlocale has the following format:
734 language[_territory][.code-set][@modifier]
736 where language is an ISO 639 language code, territory is an ISO 3166
737 country code, and codeset is a character set or encoding identifier
738 like ISO-8859-1 or UTF-8.
741 if (lc_all
!= NULL
) {
745 /* Copy the language part (which may be an ISO-639-1 code, an ISO-639-3
746 code, or a language name) adding a string terminator */
748 while (*s
!= '_' && *s
!= '.' && *s
!= '@' && *s
!= '\0')
751 lang_length
= s
- lc_all
;
753 /* Handle conversion of ISO-639-1 to ISO-639-3 */
755 if (lang_length
== 2) {
759 /* Duplicate the ISO-639-1 code adding the null terminator required to
760 search for the equivalent ISO-639-3 code; we cannot just append the
761 null terminator since the pointer may reference non-writable memory.
764 str_copy(iso_639_1
, lc_all
, lang_length
);
765 to_iso_639_3
= iso_639_1_to_639_3(iso_639_1
);
768 str_copy(iso_639_3_code
, to_iso_639_3
, 3);
770 /* Copy the ISO-639-3 code (adding a null terminator) */
772 } else if (lang_length
== 3) {
773 str_copy(iso_639_3_code
, lc_all
, lang_length
);
775 /* Handle conversion of language name to ISO-639-3 */
777 } else if (lang_length
> 3) {
778 char name_copy
[lang_length
+ 1];
781 /* Duplicate the ISO-639-1 code adding the null terminator required to
782 search for the equivalent ISO-639-3 code; we cannot just append the
783 null terminator since the pointer may reference non-writable memory.
786 str_copy(name_copy
, lc_all
, lang_length
);
787 to_iso_639_3
= language_name_to_639_3(name_copy
);
790 str_copy(iso_639_3_code
, to_iso_639_3
, 3);
794 /* Copy out the computed ISO_639_3 code */
796 result
= iso_639_3_code
;
797 for (; *result
!= '\0'; p
++, result
++)
800 /* Restore the original locale settings */
802 setlocale(LC_ALL
, Saved_Locale
);
808 c_get_country_code needs to fill in the Alpha-2 encoding of the
809 country code (2 uppercase letters). That should be "ZZ" if the
810 country is unknown. [see Ada.Locales]
813 c_get_country_code (char4 p
) {
814 char* Saved_Locale
= setlocale(LC_ALL
, NULL
);
815 char iso_3166_code
[] = "ZZ"; /* Country Unknown */
819 /* Get locales set in the environment */
821 setlocale(LC_ALL
, "");
822 lc_all
= setlocale(LC_ALL
, NULL
);
824 /* The string returned by setlocale has the following format:
826 language[_territory][.code-set][@modifier]
828 where language is an ISO 639 language code, territory is an ISO 3166
829 country code, and codeset is a character set or encoding identifier
830 like ISO-8859-1 or UTF-8.
833 if (lc_all
!= NULL
) {
836 char* last_byte
= str_get_last_byte(lc_all
);
837 int country_length
= 0;
839 /* Search for the beginning of the country code */
842 while (*s1
!= '_' && *s1
!= '.' && *s1
!= '@' && s1
!= last_byte
)
849 while (*s2
!= '.' && *s2
!= '@' && s2
!= last_byte
)
852 country_length
= s2
- s1
;
854 if (country_length
== 2) {
855 str_copy(iso_3166_code
, s1
, country_length
);
857 /* setlocale returned us the country name */
859 } else if (country_length
> 3) {
860 char name_copy
[country_length
+ 1];
863 str_copy(name_copy
, s1
, country_length
);
864 to_3166
= country_name_to_3166(name_copy
);
867 str_copy(iso_3166_code
, to_3166
, 2);
872 /* Copy out the computed ISO_3166 code */
874 result
= iso_3166_code
;
875 for (; *result
!= '\0'; p
++, result
++)
878 /* Restore the original locale settings */
880 setlocale(LC_ALL
, Saved_Locale
);