完善hlocale组件

This commit is contained in:
HEYAHONG 2025-05-06 18:05:09 +08:00
parent fac0b51fa1
commit 6a2181f0c2
No known key found for this signature in database
GPG Key ID: 97E3E469FE2C920B
3 changed files with 75 additions and 0 deletions

View File

@ -50,4 +50,59 @@ const char *hlocale_locale_get(void)
return locale;
}
bool hlocale_charset_is_utf8(void)
{
const char *locale=hlocale_locale_get();
if(locale!=NULL && locale[0]!='\0')
{
/*
* windows下根据代码页判断
*/
if(strstr(locale,"CP65001")!=NULL || strstr(locale,"cp65001")!=NULL)
{
return true;
}
/*
* UTF-8
*/
if(strstr(locale,"UTF-8")!=NULL || strstr(locale,"utf-8")!=NULL || strstr(locale,"UTF8")!=NULL || strstr(locale,"utf8")!=NULL)
{
return true;
}
}
return false;
}
bool hlocale_charset_is_gb2312(void)
{
const char *locale=hlocale_locale_get();
if(locale!=NULL && locale[0]!='\0')
{
/*
* windows下根据代码页判断
*/
if(strstr(locale,"CP936")!=NULL || strstr(locale,"cp936")!=NULL)
{
return true;
}
if(strstr(locale,"CP20936")!=NULL || strstr(locale,"cp20936")!=NULL)
{
return true;
}
//FreeBSD
if(strstr(locale,"eucCN")!=NULL)
{
return true;
}
//其它环境下
if(strstr(locale,"GB2312")!=NULL || strstr(locale,"gb2312")!=NULL)
{
return true;
}
if(strstr(locale,"GBK")!=NULL || strstr(locale,"gbk")!=NULL)
{
return true;
}
}
return false;
}

View File

@ -8,6 +8,8 @@
**************************************************************/
#ifndef __HLOCALE_H__
#define __HLOCALE_H__
#include "stdint.h"
#include "stdbool.h"
#ifdef __cplusplus
extern "C"
@ -24,6 +26,22 @@ extern "C"
*/
const char *hlocale_locale_get(void);
/** \brief 字符集是否是UTF-8
*
* \return bool UTF-8
*
*/
bool hlocale_charset_is_utf8(void);
/** \brief 字符集是否是GB2312
* ,GBK也视作GB2312
*
* \return bool GB2312
*
*/
bool hlocale_charset_is_gb2312(void);
#ifdef __cplusplus
}
#endif // __cplusplus

View File

@ -1270,6 +1270,8 @@ static int hlocale_test(int argc,const char *argv[])
{
{
printf("hlocale:%s\r\n",hlocale_locale_get());
printf("hlocale:UTF-8 = %s\r\n",hlocale_charset_is_utf8()?"true":"false");
printf("hlocale:GB2312 = %s\r\n",hlocale_charset_is_gb2312()?"true":"false");
}
{
const char *const ascii_test_string="testtest";