From 6a2181f0c2929985f41fdf54eea813c03b50488d Mon Sep 17 00:00:00 2001 From: HEYAHONG <2229388563@qq.com> Date: Tue, 6 May 2025 18:05:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84hlocale=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hbox/hlocale/hlocale.c | 55 ++++++++++++++++++++++++++++++++++++++++++ hbox/hlocale/hlocale.h | 18 ++++++++++++++ test/HBoxTest/main.cpp | 2 ++ 3 files changed, 75 insertions(+) diff --git a/hbox/hlocale/hlocale.c b/hbox/hlocale/hlocale.c index 7fbd12c..ec391c7 100644 --- a/hbox/hlocale/hlocale.c +++ b/hbox/hlocale/hlocale.c @@ -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; +} diff --git a/hbox/hlocale/hlocale.h b/hbox/hlocale/hlocale.h index 5ba43b1..88cb3e3 100644 --- a/hbox/hlocale/hlocale.h +++ b/hbox/hlocale/hlocale.h @@ -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 diff --git a/test/HBoxTest/main.cpp b/test/HBoxTest/main.cpp index c90410c..2f70d10 100644 --- a/test/HBoxTest/main.cpp +++ b/test/HBoxTest/main.cpp @@ -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";