Board logo

标题: 支持TCHAR的getopt发布 [打印本页]

作者: happy886rr    时间: 2017-5-21 23:04     标题: 支持TCHAR的getopt发布

本帖最后由 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
  1. /*
  2. GETOPT FOR TCHAR, COPYRIGHT@2017~2019 BY LEO, VERSION 1.0
  3. TGETOPT.HPP
  4. */
  5. #ifndef _TGETOPT_HPP
  6. #define _TGETOPT_HPP USED
  7. #if !( defined(_WINDOWS_H) || defined(_WINDOWS_) )
  8. #include <windows.h>
  9. #endif
  10. #if !( defined(_TCHAR_H) || defined(_INC_TCHAR) )
  11. #include   <tchar.h>
  12. #endif
  13. // TCHARFORMAT 格式化宏
  14. #if defined(_UNICODE) || defined(UNICODE)
  15. #define TCHARFORMAT WCHAR
  16. #else
  17. #define TCHARFORMAT CHAR
  18. #endif
  19. // 开关解析宏名
  20. #define _OPT_TEOF -1
  21. #define _OPT_TILL -2
  22. #define _OPT_TERR -3
  23. // 开关解析变量
  24. INT OPTIND=1, OPTOPT, UNOPTIND=-1;
  25. TCHAR* OPTARG;
  26. //函数声明
  27. INT   _tgetopt(INT, TCHAR**, TCHAR*);
  28. TCHAR* V2T(VOID*, BOOL);
  29. VOID*  T2V(TCHAR*, BOOL);
  30. // TCHAR 版 getopt
  31. INT _tgetopt(INT nargc, TCHAR* nargv[], TCHAR* ostr)
  32. {
  33. static TCHAR* place = (TCHAR*)_T("");
  34. static TCHAR* lastostr = NULL;
  35. register TCHAR* oli;
  36. if(ostr!=lastostr)
  37. {
  38. lastostr=ostr;
  39. place=(TCHAR*)_T("");
  40. }
  41. if(!*place)
  42. {
  43. if(
  44.     (OPTIND>=nargc)                           ||
  45.     (*(place=nargv[OPTIND]) !=(TCHAR)_T('-')) ||
  46.     (!*(++place))
  47. )
  48. {
  49. if(*place !=(TCHAR)_T('-') && OPTIND <nargc)
  50. {
  51. place =(TCHAR*)_T("");
  52. if(UNOPTIND == -1)
  53. {
  54. UNOPTIND = OPTIND++;
  55. return _OPT_TILL;
  56. }
  57. else
  58. {
  59. return _OPT_TERR;
  60. }
  61. }
  62. place=(TCHAR*)_T("");
  63. return _OPT_TEOF;
  64. }
  65. if(*place == (TCHAR)_T('-') && *(place+1) == (TCHAR)_T('\0'))
  66. {
  67. ++OPTIND;
  68. return _OPT_TEOF;
  69. }
  70. }
  71. if(
  72.     (OPTOPT=*place++) == (TCHAR)_T(':') ||
  73.     !(oli=(TCHAR*)_tcschr((TCHARFORMAT*)ostr, (TCHAR)OPTOPT))
  74. )
  75. {
  76. if(!*place)
  77. {
  78. ++OPTIND;
  79. }
  80. }
  81. if(oli != NULL && *(++oli) !=(TCHAR)_T(':'))
  82. {
  83. OPTARG=NULL;
  84. if(!*place)
  85. {
  86. ++OPTIND;
  87. }
  88. }
  89. else
  90. {
  91. if(*place)
  92. {
  93. OPTARG=place;
  94. }
  95. else if(nargc <= ++OPTIND)
  96. {
  97. place=(TCHAR*)_T("");
  98. }
  99. else
  100. {
  101. OPTARG=nargv[OPTIND];
  102. }
  103. place=(TCHAR*)_T("");
  104. ++OPTIND;
  105. }
  106. return OPTOPT;
  107. }
  108. // VOID* 转 TCHAR*
  109. TCHAR* V2T(VOID* csDATA, BOOL wcsMODE)
  110. {
  111. #if defined(_UNICODE) || defined(UNICODE)
  112. if(wcsMODE == TRUE)
  113. {
  114. return (TCHAR*)csDATA;
  115. }
  116. else
  117. {
  118. INT wLEN = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR*)csDATA, -1, NULL, 0);
  119. WCHAR* wsDATA = (WCHAR*)malloc(wLEN + 1);
  120. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR*)csDATA, -1, wsDATA, wLEN);
  121. wsDATA[wLEN]=L'\0';
  122. return (TCHAR*)wsDATA;
  123. }
  124. #else
  125. if(wcsMODE == TRUE)
  126. {
  127. INT aLEN = WideCharToMultiByte(CP_ACP, (DWORD)NULL, (WCHAR*)csDATA, -1, NULL, 0, NULL, NULL);
  128. CHAR* asDATA = (CHAR*)malloc(aLEN + 1);
  129. WideCharToMultiByte(CP_ACP, (DWORD)NULL, (WCHAR*)csDATA, -1, asDATA, aLEN, NULL, NULL);
  130. asDATA[aLEN] = '\0';
  131. return (TCHAR*)asDATA;
  132. }
  133. else
  134. {
  135. return (TCHAR*)csDATA;
  136. }
  137. #endif
  138. }
  139. // TCHAR* 转 VOID*
  140. VOID* T2V(TCHAR* csDATA, BOOL wcsMODE)
  141. {
  142. #if defined(_UNICODE) || defined(UNICODE)
  143. if(wcsMODE == TRUE)
  144. {
  145. return (VOID*)csDATA;
  146. }
  147. else
  148. {
  149. INT aLEN = WideCharToMultiByte(CP_ACP, (DWORD)NULL, (WCHAR*)csDATA, -1, NULL, 0, NULL, NULL);
  150. CHAR* asDATA = (CHAR*)malloc(aLEN + 1);
  151. WideCharToMultiByte(CP_ACP, (DWORD)NULL, (WCHAR*)csDATA, -1, asDATA, aLEN, NULL, NULL);
  152. asDATA[aLEN] = '\0';
  153. return (VOID*)asDATA;
  154. }
  155. #else
  156. if(wcsMODE == TRUE)
  157. {
  158. INT wLEN = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR*)csDATA, -1, NULL, 0);
  159. WCHAR* wsDATA = (WCHAR*)malloc(wLEN + 1);
  160. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR*)csDATA, -1, wsDATA, wLEN);
  161. wsDATA[wLEN]=L'\0';
  162. return (VOID*)wsDATA;
  163. }
  164. else
  165. {
  166. return (VOID*)csDATA;
  167. }
  168. #endif
  169. }
  170. #endif
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2