本帖最后由 happy886rr 于 2017-5-21 23:13 编辑
tgetopt.hpp库 [2017-05-21 version 1.0]
TGETOPT是一款可以支持TCHAR的getopt扩展库。使用非常简单,直接在项目中#include "tgetopt.hpp"即可。从而让你的任何C、C++项目瞬间变成TCHAR版,完全支持单、宽字符。从此不再为宽字符开关解析发愁,让你的VS项目、TCAHR版项目 可以拥有与GNU一样的开关玩法。新增的T2V、V2T函数让你瞬间在 单、宽字符 与TCAHR间轻松切换。从此不必为项目中的字符集转换发愁。
TGETOPT.HPP (GETOPT FOR TCHAR, COPYRIGHT@2017~2019 BY LEO, VERSION 1.0)
摘要:
====================================================
TGETOPT.HPP库,让TCHAR支持getopt。新增两个辅助函数T2V、V2T,实现 TCHAR*
与 VOID* 的无缝转化。 自然的令任何C/C++ 项目完美支持 TCHAR* 开关解析。
====================================================
宏名:
-------------------------------------------------------------------------
// TCHARFORMAT 格式化宏
#if defined(_UNICODE) || defined(UNICODE)
#define TCHARFORMAT WCHAR
#else
#define TCHARFORMAT CHAR
#endif
// 开关解析宏
#define _OPT_TEOF -1 //OPT循环结束符
#define _OPT_TILL -2 //第一个无选项的参数序号已存入UNOPTIND,通过argv[UNOPTIND]获取
#define _OPT_TERR -3 //OPT开关错误符
// 开关解析变量
INT OPTIND=1, OPTOPT, UNOPTIND=-1;
TCHAR* OPTARG; //每次解析到的字符串
-------------------------------------------------------------------------
用法:(直接 #include "tgetopt.hpp" 即可)
-------------------------------------------------------------------------
INT _tgetopt(INT, TCHAR**, TCHAR*); //用法同GNU版getopt(),但须传递:TCAHR*参数
TCHAR* V2T(VOID*, BOOL); //将任意类型转 TCHAR*,BOOL为真:输入宽字节
VOID* T2V(TCHAR*, BOOL); //将TCHAR* 转任意类型,BOOL为真:输出宽字节
-------------------------------------------------------------------------
版本:
VERSION 1.0
源码:tgetopt.hpp- /*
- GETOPT FOR TCHAR, COPYRIGHT@2017~2019 BY LEO, VERSION 1.0
- TGETOPT.HPP
- */
-
- #ifndef _TGETOPT_HPP
- #define _TGETOPT_HPP USED
-
- #if !( defined(_WINDOWS_H) || defined(_WINDOWS_) )
- #include <windows.h>
- #endif
-
- #if !( defined(_TCHAR_H) || defined(_INC_TCHAR) )
- #include <tchar.h>
- #endif
-
- // TCHARFORMAT 格式化宏
- #if defined(_UNICODE) || defined(UNICODE)
- #define TCHARFORMAT WCHAR
- #else
- #define TCHARFORMAT CHAR
- #endif
-
- // 开关解析宏名
- #define _OPT_TEOF -1
- #define _OPT_TILL -2
- #define _OPT_TERR -3
-
- // 开关解析变量
- INT OPTIND=1, OPTOPT, UNOPTIND=-1;
- TCHAR* OPTARG;
-
- //函数声明
- INT _tgetopt(INT, TCHAR**, TCHAR*);
- TCHAR* V2T(VOID*, BOOL);
- VOID* T2V(TCHAR*, BOOL);
-
- // TCHAR 版 getopt
- INT _tgetopt(INT nargc, TCHAR* nargv[], TCHAR* ostr)
- {
- static TCHAR* place = (TCHAR*)_T("");
- static TCHAR* lastostr = NULL;
- register TCHAR* oli;
-
- if(ostr!=lastostr)
- {
- lastostr=ostr;
- place=(TCHAR*)_T("");
- }
-
- if(!*place)
- {
- if(
- (OPTIND>=nargc) ||
- (*(place=nargv[OPTIND]) !=(TCHAR)_T('-')) ||
- (!*(++place))
- )
- {
- if(*place !=(TCHAR)_T('-') && OPTIND <nargc)
- {
- place =(TCHAR*)_T("");
- if(UNOPTIND == -1)
- {
- UNOPTIND = OPTIND++;
- return _OPT_TILL;
- }
- else
- {
- return _OPT_TERR;
- }
- }
-
- place=(TCHAR*)_T("");
- return _OPT_TEOF;
- }
- if(*place == (TCHAR)_T('-') && *(place+1) == (TCHAR)_T('\0'))
- {
- ++OPTIND;
- return _OPT_TEOF;
- }
- }
-
- if(
- (OPTOPT=*place++) == (TCHAR)_T(':') ||
- !(oli=(TCHAR*)_tcschr((TCHARFORMAT*)ostr, (TCHAR)OPTOPT))
- )
- {
- if(!*place)
- {
- ++OPTIND;
- }
- }
-
- if(oli != NULL && *(++oli) !=(TCHAR)_T(':'))
- {
- OPTARG=NULL;
- if(!*place)
- {
- ++OPTIND;
- }
- }
- else
- {
- if(*place)
- {
- OPTARG=place;
- }
- else if(nargc <= ++OPTIND)
- {
- place=(TCHAR*)_T("");
- }
- else
- {
- OPTARG=nargv[OPTIND];
- }
- place=(TCHAR*)_T("");
- ++OPTIND;
- }
- return OPTOPT;
- }
-
- // VOID* 转 TCHAR*
- TCHAR* V2T(VOID* csDATA, BOOL wcsMODE)
- {
- #if defined(_UNICODE) || defined(UNICODE)
-
- if(wcsMODE == TRUE)
- {
- return (TCHAR*)csDATA;
- }
- else
- {
- INT wLEN = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR*)csDATA, -1, NULL, 0);
- WCHAR* wsDATA = (WCHAR*)malloc(wLEN + 1);
- MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR*)csDATA, -1, wsDATA, wLEN);
- wsDATA[wLEN]=L'\0';
- return (TCHAR*)wsDATA;
- }
-
- #else
-
- if(wcsMODE == TRUE)
- {
- INT aLEN = WideCharToMultiByte(CP_ACP, (DWORD)NULL, (WCHAR*)csDATA, -1, NULL, 0, NULL, NULL);
- CHAR* asDATA = (CHAR*)malloc(aLEN + 1);
- WideCharToMultiByte(CP_ACP, (DWORD)NULL, (WCHAR*)csDATA, -1, asDATA, aLEN, NULL, NULL);
- asDATA[aLEN] = '\0';
- return (TCHAR*)asDATA;
- }
- else
- {
- return (TCHAR*)csDATA;
- }
-
- #endif
- }
-
- // TCHAR* 转 VOID*
- VOID* T2V(TCHAR* csDATA, BOOL wcsMODE)
- {
- #if defined(_UNICODE) || defined(UNICODE)
-
- if(wcsMODE == TRUE)
- {
- return (VOID*)csDATA;
- }
- else
- {
- INT aLEN = WideCharToMultiByte(CP_ACP, (DWORD)NULL, (WCHAR*)csDATA, -1, NULL, 0, NULL, NULL);
- CHAR* asDATA = (CHAR*)malloc(aLEN + 1);
- WideCharToMultiByte(CP_ACP, (DWORD)NULL, (WCHAR*)csDATA, -1, asDATA, aLEN, NULL, NULL);
- asDATA[aLEN] = '\0';
- return (VOID*)asDATA;
- }
-
- #else
-
- if(wcsMODE == TRUE)
- {
- INT wLEN = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR*)csDATA, -1, NULL, 0);
- WCHAR* wsDATA = (WCHAR*)malloc(wLEN + 1);
- MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR*)csDATA, -1, wsDATA, wLEN);
- wsDATA[wLEN]=L'\0';
- return (VOID*)wsDATA;
- }
- else
- {
- return (VOID*)csDATA;
- }
-
- #endif
- }
-
- #endif
复制代码
|