php-src/ext/unicode/locale.c

97 lines
2.8 KiB
C
Raw Normal View History

2005-08-11 23:36:07 +00:00
/*
+----------------------------------------------------------------------+
2005-08-12 09:10:04 +00:00
| PHP Version 6 |
2005-08-11 23:36:07 +00:00
+----------------------------------------------------------------------+
2006-01-01 13:10:10 +00:00
| This source file is subject to version 3.01 of the PHP license, |
2005-08-11 23:36:07 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 13:10:10 +00:00
| http://www.php.net/license/3_01.txt |
2005-08-11 23:36:07 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andrei Zmievski <andrei@php.net> |
+----------------------------------------------------------------------+
*/
2005-08-12 09:10:04 +00:00
/* $Id$ */
2005-08-11 23:36:07 +00:00
#include "php_unicode.h"
#include "unicode/ubrk.h"
2005-08-11 23:36:07 +00:00
static void php_canonicalize_locale_id(char **target, int32_t *target_len, char *locale, UErrorCode *status)
{
char *canonicalized = NULL;
int32_t canonicalized_len = 128;
while (1) {
*status = U_ZERO_ERROR;
canonicalized = erealloc(canonicalized, canonicalized_len + 1);
canonicalized_len = uloc_canonicalize(locale, canonicalized, canonicalized_len, status);
if (*status != U_BUFFER_OVERFLOW_ERROR) {
break;
}
}
canonicalized[canonicalized_len] = 0;
*target = canonicalized;
*target_len = canonicalized_len;
}
2006-04-21 19:35:26 +00:00
/* {{{ proto string locale_get_default(void) U
2006-04-18 21:36:38 +00:00
Returns default locale */
2006-04-21 19:35:26 +00:00
PHP_FUNCTION(locale_get_default)
2005-08-11 23:36:07 +00:00
{
2008-02-28 14:16:25 +00:00
if (zend_parse_parameters_none() == FAILURE) {
2005-08-11 23:36:07 +00:00
return;
}
RETURN_STRING(UG(default_locale), 1);
}
2006-04-18 21:36:38 +00:00
/* }}} */
2005-08-11 23:36:07 +00:00
2006-04-21 19:35:26 +00:00
/* {{{ proto bool locale_set_default(string locale) U
2006-04-18 21:36:38 +00:00
Sets default locale */
2006-04-21 19:35:26 +00:00
PHP_FUNCTION(locale_set_default)
2005-08-11 23:36:07 +00:00
{
char *locale;
int locale_len;
char *canonicalized = NULL;
UErrorCode status = U_ZERO_ERROR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &locale, &locale_len) == FAILURE) {
return;
}
php_canonicalize_locale_id(&canonicalized, &locale_len, locale, &status);
/*
* UTODO: is this right? canonicalization does not seem to perform locale
2006-04-18 21:36:38 +00:00
* validation. See this for possible solution:
* http://sourceforge.net/mailarchive/message.php?msg_id=11953411
2005-08-11 23:36:07 +00:00
*/
if (U_FAILURE(status)) {
2005-08-18 12:56:36 +00:00
php_error(E_WARNING, "Invalid locale: %s", locale);
2005-08-11 23:36:07 +00:00
RETURN_FALSE;
}
/* don't bother if locales are identical */
if (!strcmp(UG(default_locale), canonicalized)) {
efree(canonicalized);
RETURN_FALSE;
}
efree(UG(default_locale));
UG(default_locale) = canonicalized;
zend_reset_locale_deps(TSRMLS_C);
RETURN_TRUE;
}
2006-04-18 21:36:38 +00:00
/* }}} */
2005-08-11 23:36:07 +00:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/