Board logo

标题: opt2byte.LIB命令行开关解析库 [打印本页]

作者: happy886rr    时间: 2016-12-5 22:30     标题: opt2byte.LIB命令行开关解析库

本帖最后由 happy886rr 于 2017-1-7 19:54 编辑

[2016/12/06]修复一处笔误。

THE OPT TO BYTE ARRAY LIB, COPYRIGHT@2016~2018 BY HAPPY, VERSION 1.0
opt2byte.LIB

摘要:
=============================================================================
这是可以解析命令行参数的库,用来帮助你快速构建命令行第三方工具的开关解析部分。
从此你无需再写开关解析。
=============================================================================


函数原型:
-----------------------------------------------------------------------------
//OPT转BYTE数组
byte* __cdecl Opt2Byte(
        int argc,                                  //argc
        char** argv,                           //argv
        char* optstr                            //开关正则
);
-----------------------------------------------------------------------------


引入:
-----------------------------------------------------------------------------
#include "opt2byte.h"                            //先引入头文件
#pragram commet(lib,"opt2byte.LIB");  //在项目中引入库文件
-lopt2byte -L./                                       //gcc连接参数
-----------------------------------------------------------------------------


命令开关正则语法:
-----------------------------------------------------------------------------
char* optstr="=/C* /U:utf-8  /CS /T* /D* /M* /?";

在上句opt正则中,其中第一个引号后的等号表示精确开关匹配,
即区分大小写匹配,不为等号则忽略大小写匹配。

其中*代表后继参数,一个*代表/C开关有一个后继参数。
其中:代表连参符,即/U开关后边无需空格即可连接参数,如/U:"utf-8"。
其中无*的开关表示没有任何后继参数。

opt2byte.LIB库,最大可支持99个开关解析,最多处理254个命令行参数。 当然你可以
在源码中修改以使其支持不限量的数目。

opt2byte.LIB库全部为本人原创,且不需要任何其他库文件,效率性能秒杀getopt函数,
无法解析的开关argv[unknown]将存入bp[100~127]的无法解析空间。 bp[0]表示解析状
态,即bp[0]为0时表示正常解析,否则其值为发生错误的argv序号。bp[1~99]为各开关
解析到的命令行参数argv序号。
-----------------------------------------------------------------------------


英译:
-----------------------------------------------------------------------------
THE OPT TO BYTE ARRAY LIB, COPYRIGHT@2016~2018 BY HAPPY, VERSION 1.0
-----------------------------------------------------------------------------
This is a library of command-line arguments  that can  help you quickly build
switch-resolution parts of command-line third-party tools.From then on you do
not have to repeat the write switch every time parsing.
-----------------------------------------------------------------------------
12/05/2016


核心代码:
  1. /*
  2. OPT转BYTE库
  3. THE OPT TO BYTE ARRAY LIB, COPYRIGHT@2016~2018 BY HAPPY, VERSION 1.0
  4. opt2byte.LIB
  5. LINK: -lopt2byte -L./
  6. */
  7. #ifndef byte
  8. typedef unsigned char byte;
  9. #endif
  10. #ifndef calloc
  11. #include <stdlib.h>
  12. #endif
  13. //OPT转BYTE数组函数
  14. byte* Opt2Byte(int argc, char** argv, char* optstr)
  15. {
  16. //计算有效参数数目
  17. int oargc=argc-1;
  18. //接收指针
  19. char** oargv=argv;
  20. //参数累加器
  21. int i=0;
  22. //无法解析参数的存放区间
  23. int unknown=100;
  24. //optstr首字符为等号,则视为所有开关均区分大小写
  25. byte  ostrcase=(*optstr=='=')?1:0;
  26. //判断是否开启大小写,进而修正ostr
  27. char* ostr=(ostrcase==1)?optstr+1:optstr;
  28. //opt比特数组
  29. byte* bp=(byte*)calloc(128, sizeof(byte));
  30. //如果没有参数,则置bp数组首位为255
  31. if(oargc==0){bp[0]=0xFF;}
  32. //开启主循环
  33. while((++i)<=oargc){
  34. //扫描指针归位
  35. char* op=ostr;
  36. //开关序号归位
  37. int   m=0;
  38. while(*op!='\0'){
  39. //过滤空格
  40. while(*op==' '){op++;}
  41. //开关序号累加器
  42. m++;
  43. //将参数送入比较器
  44. char* vp=oargv[i];
  45. while(*vp!='\0'){
  46. //临时辅助指针
  47. char tp1=*op, tp2=*vp;
  48. //ostrcase为0,则忽略大写比较
  49. if(ostrcase==0){
  50. tp1=('a'<= tp1 && tp1<='z')?tp1-32:tp1;
  51. tp2=('a'<= tp2 && tp2<='z')?tp2-32:tp2;
  52. }
  53. //不相等则中断
  54. if(tp1!=tp2){break;}
  55. op++, vp++;
  56. }
  57. //vp指针移到末尾,则视为解析成功
  58. if(
  59. (*vp=='\0')  &&
  60. (
  61. *op==' ' ||
  62. *op=='*' ||
  63. *op=='\0'
  64. )
  65. ){
  66. //判断是否为连参开关
  67. if(*(op-1)==':'){
  68. bp[m]=i;
  69. break;
  70. //判断是否为无参数开关
  71. }else if(*op==' '||*op=='\0'){
  72. bp[m]=i;
  73. break;
  74. //判断是否为带参数开关
  75. }else if(*op=='*' && i+1<=oargc){
  76. bp[m]=i+1;
  77. break;
  78. //无法识别的开关末端,将错误位置存入bp首位
  79. }else{
  80. bp[0]=i;
  81. return bp;
  82. }
  83. }else{
  84. //扫描指针后移
  85. while(*op!='\0'&& *op!=' '){op++;}
  86. }
  87. }
  88. //判断是否为无法解析的参数,并存入 unknown区间
  89. if(bp[m]!=i && bp[m]!=i+1 && *op!='*'){
  90. bp[unknown++]=i;
  91. continue;
  92. }
  93. //如果为*代表有后继参数,则参数器累加。
  94. while(*op=='*'){
  95. op++, i++;
  96. if(i>oargc){
  97. bp[0]=i-1;
  98. return bp;
  99. }
  100. //如果为开关符且参数不足,则将错误位置存入bp首位
  101. if(oargv[i][0]==ostr[0]){
  102. bp[0]=i;
  103. return bp;
  104. }
  105. }
  106. }
  107. //返回byte数组指针
  108. return bp;
  109. }
复制代码
使用示例:
  1. #include <stdio.h>
  2. #include "opt2byte.h"
  3. int main(int argc, char** argv)
  4. {
  5. int i;
  6. char* optstr="=/D**  /U: /S /T";                           //这里填写命令开关正则
  7. byte* bp=Opt2Byte(argc, argv, optstr);                   //这里用来接收转化后的BYTE数组
  8. if(bp[0]==255){printf("没有任何参数\n");}
  9. if(bp[0]!=0){printf("在%s附近出现错误,可能是缺少参数,或是无法解析的开关\n", argv[bp[0]]);}
  10.                   
  11. for(i=1; i<=4; i++){printf("%d\n",bp[i]); }            //打印分析后的开关BYTE数组参数
  12. return 0;
  13. }
复制代码
下载,base64加权码
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. ::*********BASE64 加权解码器*********
  4. set $=set [#]&CALL :BASE64_PRESS&set [$]=A&((for %%Z in ([,],{,},_,$,#,-,@) do (set [$]=![$]!![$]!&for %%S in (![$]!) do (for /l %%i in (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