📄 libintl.cpp
字号:
if (isalpha (codeset[cnt])) *wp++ = tolower (codeset[cnt]); else if (isdigit (codeset[cnt])) *wp++ = codeset[cnt]; *wp = '\0'; } return (const char *) retval;}/* We need a sign, whether a new catalog was loaded, which can be associated with all translations. This is important if the translations are cached by one of GCC's features. */int k_nl_msg_cat_cntr = 0;/* Load the message catalogs specified by FILENAME. If it is no valid message catalog do nothing. */voidk_nl_load_domain (struct loaded_l10nfile *domain_file){ int fd; struct stat st; struct mo_file_header *data = (struct mo_file_header *) -1;#if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) int use_mmap = 0;#endif struct loaded_domain *domain; domain_file->decided = 1; domain_file->data = NULL; /* If the record does not represent a valid locale the FILENAME might be NULL. This can happen when according to the given specification the locale file name is different for XPG and CEN syntax. */ if (domain_file->filename == NULL) return; /* Try to open the addressed file. */ fd = open (domain_file->filename, O_RDONLY); if (fd == -1) return; /* We must know about the size of the file. */ if (fstat (fd, &st) != 0 && st.st_size < (off_t) sizeof (struct mo_file_header)) { /* Something went wrong. */ close (fd); return; }#if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) /* Now we are ready to load the file. If mmap() is available we try this first. If not available or it failed we try to load it. */ data = (struct mo_file_header *) mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (data != (struct mo_file_header *) -1) { /* mmap() call was successful. */ close (fd); use_mmap = 1; }#endif /* If the data is not yet available (i.e. mmap'ed) we try to load it manually. */ if (data == (struct mo_file_header *) -1) { off_t to_read; char *read_ptr; data = (struct mo_file_header *) malloc (st.st_size); if (data == NULL) return; to_read = st.st_size; read_ptr = (char *) data; do { long int nb = (long int) read (fd, read_ptr, to_read); if (nb == -1) { close (fd); return; } read_ptr += nb; to_read -= nb; } while (to_read > 0); close (fd); } /* Using the magic number we can test whether it really is a message catalog file. */ if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED) { /* The magic number is wrong: not a message catalog file. */#if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) if (use_mmap) munmap ((caddr_t) data, st.st_size); else#endif free (data); return; } domain_file->data = (struct loaded_domain *) malloc (sizeof (struct loaded_domain)); if (domain_file->data == NULL) return; domain = (struct loaded_domain *) domain_file->data; domain->data = (char *) data; domain->must_swap = data->magic != _MAGIC; /* Fill in the information about the available tables. */ switch (W (domain->must_swap, data->revision)) { case 0: domain->nstrings = W (domain->must_swap, data->nstrings); domain->orig_tab = (struct string_desc *) ((char *) data + W (domain->must_swap, data->orig_tab_offset)); domain->trans_tab = (struct string_desc *) ((char *) data + W (domain->must_swap, data->trans_tab_offset)); domain->hash_size = W (domain->must_swap, data->hash_tab_size); domain->hash_tab = (nls_uint32 *) ((char *) data + W (domain->must_swap, data->hash_tab_offset)); break; default: /* This is an illegal revision. */#if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) if (use_mmap) munmap ((caddr_t) data, st.st_size); else#endif free (data); free (domain); domain_file->data = NULL; return; } /* Show that one domain is changed. This might make some cached translations invalid. */ ++k_nl_msg_cat_cntr;}struct alias_map{ const char *alias; const char *value;};static struct alias_map *map;static size_t nmap = 0;static size_t maxmap = 0;/* Prototypes for local functions. */static size_t read_alias_file (const char *fname, int fname_len);static void extend_alias_table (void);static int alias_compare (const struct alias_map *map1, const struct alias_map *map2);const char *_nl_expand_alias (const char *name){ static const char *locale_alias_path = LOCALE_ALIAS_PATH; struct alias_map *retval; size_t added; do { struct alias_map item; item.alias = name; if (nmap > 0) retval = (struct alias_map *) bsearch (&item, map, nmap, sizeof (struct alias_map), (int (*) (const void *, const void *)) alias_compare); else retval = NULL; /* We really found an alias. Return the value. */ if (retval != NULL) return retval->value; /* Perhaps we can find another alias file. */ added = 0; while (added == 0 && locale_alias_path[0] != '\0') { const char *start; while (locale_alias_path[0] == ':') ++locale_alias_path; start = locale_alias_path; while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':') ++locale_alias_path; if (start < locale_alias_path) added = read_alias_file (start, locale_alias_path - start); } } while (added != 0); return NULL;}static size_tread_alias_file (const char *fname, int fname_len){#ifndef HAVE_ALLOCA struct block_list *block_list = NULL;#endif FILE *fp; char *full_fname; size_t added; static const char aliasfile[] = "/locale.alias"; full_fname = (char *) alloca (fname_len + sizeof aliasfile); ADD_BLOCK (block_list, full_fname); memcpy (full_fname, fname, fname_len); memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile); fp = fopen (full_fname, "r"); if (fp == NULL) { FREE_BLOCKS (block_list); return 0; } added = 0; while (!feof (fp)) { /* It is a reasonable approach to use a fix buffer here because a) we are only interested in the first two fields b) these fields must be usable as file names and so must not be that long */ char buf[BUFSIZ]; char *alias; char *value; char *cp; if (fgets (buf, BUFSIZ, fp) == NULL) /* EOF reached. */ break; cp = buf; /* Ignore leading white space. */ while (isspace (cp[0])) ++cp; /* A leading '#' signals a comment line. */ if (cp[0] != '\0' && cp[0] != '#') { alias = cp++; while (cp[0] != '\0' && !isspace (cp[0])) ++cp; /* Terminate alias name. */ if (cp[0] != '\0') *cp++ = '\0'; /* Now look for the beginning of the value. */ while (isspace (cp[0])) ++cp; if (cp[0] != '\0') { char *tp; size_t len; value = cp++; while (cp[0] != '\0' && !isspace (cp[0])) ++cp; /* Terminate value. */ if (cp[0] == '\n') { /* This has to be done to make the following test for the end of line possible. We are looking for the terminating '\n' which do not overwrite here. */ *cp++ = '\0'; *cp = '\n'; } else if (cp[0] != '\0') *cp++ = '\0'; if (nmap >= maxmap) extend_alias_table (); /* We cannot depend on strdup available in the libc. Sigh! */ len = strlen (alias) + 1; tp = (char *) malloc (len); if (tp == NULL) { FREE_BLOCKS (block_list); return added; } memcpy (tp, alias, len); map[nmap].alias = tp; len = strlen (value) + 1; tp = (char *) malloc (len); if (tp == NULL) { FREE_BLOCKS (block_list); return added; } memcpy (tp, value, len); map[nmap].value = tp; ++nmap; ++added; } } /* Possibly not the whole line fits into the buffer. Ignore the rest of the line. */ while (strchr (cp, '\n') == NULL) { cp = buf; if (fgets (buf, BUFSIZ, fp) == NULL) /* Make sure the inner loop will be left. The outer loop will exit at the `feof' test. */ *cp = '\n'; } } /* Should we test for ferror()? I think we have to silently ignore errors. --drepper */ fclose (fp); if (added > 0) qsort (map, nmap, sizeof (struct alias_map), (int (*) (const void *, const void *)) alias_compare); FREE_BLOCKS (block_list); return added;}static voidextend_alias_table (){ size_t new_size; struct alias_map *new_map; new_size = maxmap == 0 ? 100 : 2 * maxmap; new_map = (struct alias_map *) malloc (new_size * sizeof (struct alias_map)); if (new_map == NULL) /* Simply don't extend: we don't have any more core. */ return; memcpy (new_map, map, nmap * sizeof (struct alias_map)); if (maxmap != 0) free (map); map = new_map; maxmap = new_size;}static intalias_compare (const struct alias_map *map1, const struct alias_map *map2){#if defined HAVE_STRCASECMP return strcasecmp (map1->alias, map2->alias);#else const unsigned char *p1 = (const unsigned char *) map1->alias; const unsigned char *p2 = (const unsigned char *) map2->alias; unsigned char c1, c2; if (p1 == p2) return 0; do { /* I know this seems to be odd but the tolower() function in some systems libc cannot handle nonalpha characters. */ c1 = isupper (*p1) ? tolower (*p1) : *p1; c2 = isupper (*p2) ? tolower (*p2) : *p2; if (c1 == '\0') break; ++p1; ++p2; } while (c1 == c2); return c1 - c2;#endif}/* Name of the default text domain. */extern const char _nl_default_default_domain[];/* Default text domain in which entries for gettext(3) are to be found. */extern const char *k_nl_current_default_domain;/* Set the current default message catalog to DOMAINNAME. If DOMAINNAME is null, return the current default. If DOMAINNAME is "", reset to the default of "messages". */char *k_textdomain (const char *domainname){ char *old; /* A NULL pointer requests the current setting. */ if (domainname == NULL) return (char *) k_nl_current_default_domain; old = (char *) k_nl_current_default_domain; /* If domain name is the null string set to default domain "messages". */ if (domainname[0] == '\0' || strcmp (domainname, _nl_default_default_domain) == 0) k_nl_current_default_domain = _nl_default_default_domain; else { /* If the following malloc fails `k_nl_current_default_domain' will be NULL. This value will be returned and so signals we are out of core. */ size_t len = strlen (domainname) + 1; char *cp = (char *) malloc (len); if (cp != NULL) memcpy (cp, domainname, len); k_nl_current_default_domain = cp; } if (old != _nl_default_default_domain) free (old); return (char *) k_nl_current_default_domain;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -