示例
本节包含子例程的示例。
- 以下示例使用 setlocale 子例程将语言环境从缺省 C 语言环境更改为由环境变量指定的语言环境,并与语言环境环境变量的层次结构一致:
#include <locale.h> main() { char *p; p = setlocale(LC_ALL, ""); /* ** The program will have the locale as set by the ** LC_* and LANG variables. */ } - 下面的示例使用 setlocale 子例程来获取 LC_COLLATE 类别的当前语言环境设置:
#include <stdio.h> #include <locale.h> main() { char *p; /* set the current locale to what is specified */ p = setlocale(LC_ALL, ""); /* The current locale settings for all the ** categories is pointed to by p */ /* ** Find the current setting for the ** LC_COLLATE category */ p = setlocale(LC_COLLATE, NULL); /* ** p points to a string containing the current locale ** setting for the LC_COLLATE category. */ } - 以下示例使用 setlocale 子例程来获取当前语言环境设置并将其保存以供将来使用。 该操作允许程序暂时将语言环境改为新的语言环境。 处理完成之后,语言环境可恢复为原先的状态。
#include <stdio.h> #include <locale.h> #include <string.h> #define NEW_LOCALE "MY_LOCALE" main() { char *p, *save_locale; p = setlocale(LC_ALL, ""); /* ** Initiate locale. p points to the current locale ** setting for all the categories */ save_locale = (char *)malloc(strlen(p) +1); strcpy(save_locale, p); /* Save the current locale setting */ p = setlocale(LC_ALL, NEW_LOCALE); /* Change to new locale */ /* ** Do processing ... */ /* Change back to old locale */ p = setlocale(LC_ALL, save_locale); /* Restore old locale */ free(save_locale); /* Free the memory */ } - 以下示例使用 setlocale 子例程将 LC_MESSAGES 类别设置为由环境变量所确定的语言环境。 所有其他类别依然设为 C 语言环境。
#include <locale.h> main() { char *p; /* ** The program starts in the C locale for all categories. */ p = setlocale(LC_MESSAGES, ""); /* ** At this time the LC_COLLATE, LC_CTYPE, LC_NUMERIC, ** LC_MONETARY, LC_TIME will be in the C locale. ** LC_MESSAGES will be set to the current locale setting ** as determined by the environment variables. */ } - 以下示例使用 卡莱孔夫 子例程来获取当前语言环境的小数点设置:
#include <locale.h> main() { struct lconv *ptr; char *decimal; (void)setlocale(LC_ALL, ""); ptr = localeconv(); /* ** Access the data obtained. For example, ** obtain the current decimal point setting. */ decimal = ptr->decimal_point; } - 下面的示例使用nl_langinfo 子例程来获取当前语言环境的日期和时间格式:
#include <langinfo.h> #include <locale.h> main() { char *ptr; (void)setlocale(LC_ALL, ""); ptr = nl_langinfo(D_T_FMT); } - 以下示例使用 nl_langinfo 子例程来获取当前语言环境的 Radix 字符:
#include <langinfo.h> #include <locale.h> main() { char *ptr; (void)setlocale(LC_ALL, ""); /* Set the program's locale */ ptr = nl_langinfo(RADIXCHAR); /* Obtain the radix character*/ } - 以下示例使用 nl_langinfo 子例程来获取当前语言环境的货币符号的设置:
#include <langinfo.h> #include <locale.h> main() { char *ptr; (void)setlocale(LC_ALL, ""); /* Set the program's locale */ ptr = nl_langinfo(CRNCYSTR); /* Obtain the currency string*/ /* The currency string will be "-$" in the U. S. locale. */ } - 以下示例使用 RPmatch 子例程来获取当前语言环境的肯定响应字符串和否定响应字符串的设置:
语言环境数据库中指定的肯定和否定响应不再是简单字符串;它们可以是正则表达式。 例如, yesexpr 可以是以下正则表达式,它将接受大写或小写字母
y,后跟零个或多个字母字符; 或者字符O后跟K。 因此, yesexpr 可以是以下正则表达式:([yY][:alpha:]*|OK)标准中并未包含用来检索和比较这一信息子例程。 您可以使用AIX® rpmatch(const char *response)子程序。
#include <stdio.h> #include <langinfo.h> #include <locale.h> #include <regex.h> int rpmatch(const char *); /* ** Returns 1 if yes response, 0 if no response, ** -1 otherwise */ main() { int ret; char *resp; (void)setlocale(LC_ALL, ""); do { /* ** Obtain the response to the query for yes/no strings. ** The string pointer resp points to this response. ** Check if the string is yes. */ ret = rpmatch(resp); if(ret == 1){ /* Response was yes. */ /* Process accordingly. */ }else if(ret == 0){ /* Response was negative. */ /* Process negative response. */ }else if(ret<0){ /* No match with yes/no occurred. */ continue; } }while(ret <0); } - 下面的示例提供了实现
rpmatch 子例程的方法。 请注意大多数应用程序应该使用 libc 库中的
rpmatch 子例程。 下面的 rpmatch 子例程实现仅是为了演示目的。请注意 nl_langinfo(YESEXPR) 和 nl_langinfo(NOEXPR) 用来分别获取代表肯定性响应和否定性响应的正则表达式。
#include <langinfo.h> #include <regex.h> /* ** rpmatch() performs comparison of a string to a regular expression ** using the POSIX.2 defined regular expression compile and match ** functions. The first argument is the response from the user and the ** second string is the current locale setting of the regular expression. */ int rpmatch( const char *string) { int status; int retval; regex_t re; char *pattern; pattern = nl_langinfo(YESEXPR); /* Compile the regular expression pointed to by pattern. */ if( ( status = regcomp( &re, pattern, REG_EXTENDED | REG_NOSUB )) != 0 ){ retval = -2; /*-2 indicates yes expr compile error */ return(retval); } /* Match the string with the compiled regular expression. */ status = regexec( &re, string, (size_t)0, (regmatch_t *)NULL, 0); if(status == 0){ retval = 1; /* Yes match found */ }else{ /* Check for negative response */ pattern = nl_langinfo(NOEXPR); if( ( status = regcomp( &re, pattern, REG_EXTENDED | REG_NOSUB )) != 0 ){ retval = -3;/*-3 indicates no compile error */ return(retval); } status = regexec( &re, string, (size_t)0, (regmatch_t *)NULL, 0); if(status == 0) retval = 0;/* Negative response match found */ }else retval = -1; /* The string did not match yes or no response */ regfree(&re); return(retval); }