main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <QCoreApplication>
  2. #include <QFile>
  3. #include <iostream>
  4. #include <memory>
  5. #include <getopt.h>
  6. #include "carmakercvt.h"
  7. static char gstr_inputpath[256];
  8. static char gstr_outputpath[256];
  9. void print_useage()
  10. {
  11. std::cout<<" -i --input $xodrfile : set input file path. eq. -i d:/lk.xodr"<<std::endl;
  12. std::cout<<" -o --output $outputfile : set output file. eq. -o d:/test.rd5"<<std::endl;
  13. std::cout<<" -h --help print help"<<std::endl;
  14. }
  15. int GetOptLong(int argc, char *argv[]) {
  16. int nRtn = 0;
  17. int opt; // getopt_long() 的返回值
  18. int digit_optind = 0; // 设置短参数类型及是否需要参数
  19. (void)digit_optind;
  20. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  21. // 第几个元素的描述,即是long_opts的下标值
  22. int option_index = 0;
  23. // 设置短参数类型及是否需要参数
  24. const char *optstring = "i:o:h";
  25. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  26. /*
  27. struct option {
  28. const char * name; // 参数的名称
  29. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  30. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  31. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  32. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  33. };
  34. 其中:
  35. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  36. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  37. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  38. */
  39. static struct option long_options[] = {
  40. {"input", required_argument, NULL, 'i'},
  41. {"output", required_argument, NULL, 'o'},
  42. {"help", no_argument, NULL, 'h'},
  43. // {"optarg", optional_argument, NULL, 'o'},
  44. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  45. };
  46. while ( (opt = getopt_long(argc,
  47. argv,
  48. optstring,
  49. long_options,
  50. &option_index)) != -1) {
  51. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  52. // printf("optarg = %s\n", optarg); // 参数内容
  53. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  54. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  55. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  56. // printf("\n");
  57. switch(opt)
  58. {
  59. case 'i':
  60. strncpy(gstr_inputpath,optarg,255);
  61. break;
  62. case 'o':
  63. strncpy(gstr_outputpath,optarg,255);
  64. break;
  65. case 'h':
  66. print_useage();
  67. nRtn = 1; //because use -h
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. return nRtn;
  74. }
  75. std::string GetFileName(char * strpath)
  76. {
  77. int npos = -1;
  78. int nsize = static_cast<int>(strnlen(strpath,256));
  79. int i;
  80. for(i=(nsize-2);i>=0;i--)
  81. {
  82. if((strpath[i] == '/')||(strpath[i] == '\\'))
  83. {
  84. npos = i+1;
  85. break;
  86. }
  87. }
  88. std::string strrtn;
  89. if(npos == -1)
  90. {
  91. strrtn = strpath;
  92. }
  93. else
  94. {
  95. char strname[256];
  96. strncpy(strname,strpath+npos,256);
  97. strrtn = strname;
  98. }
  99. return strrtn;
  100. }
  101. int main(int argc, char *argv[])
  102. {
  103. QCoreApplication a(argc, argv);
  104. int nRtn = GetOptLong(argc,argv);
  105. if(nRtn == 1) //show help,so exit.
  106. {
  107. return 0;
  108. }
  109. #ifdef TESTC
  110. snprintf(gstr_inputpath,255,"/home/yuchuli/demodata/lk.xodr");
  111. snprintf(gstr_outputpath,255,"/home/yuchuli/demodata/lk1.rd5");
  112. #endif
  113. if(strncmp(gstr_inputpath , "",255) == 0)
  114. {
  115. std::cout<<"Please use -i set input file path."<<std::endl;
  116. print_useage();
  117. return 0;
  118. }
  119. char strout[1000];
  120. snprintf(strout,1000,"Input File Path: %s",gstr_inputpath);
  121. std::cout<<strout<<std::endl;
  122. if(strncmp(gstr_outputpath , "",255) == 0)
  123. {
  124. std::cout<<"Please use -o set output file path."<<std::endl;
  125. print_useage();
  126. return 0;
  127. }
  128. snprintf(strout,1000,"Output File Path: %s",gstr_outputpath);
  129. std::cout<<strout<<std::endl;
  130. std::string strinputname = GetFileName(gstr_inputpath);
  131. std::string stroutputname = GetFileName(gstr_outputpath);
  132. std::cout<<"input name: "<<strinputname<<std::endl;
  133. std::string strcmd = "xodr2rd5.exe ";
  134. strcmd = strcmd + "-i "+strinputname + " -o "+stroutputname;
  135. std::cout<<"cmd: "<<strcmd<<std::endl;
  136. std::shared_ptr<char> pstr_ptr = nullptr;
  137. int ninputsize = 0;
  138. QFile xFile;
  139. xFile.setFileName(gstr_inputpath);
  140. if(xFile.open(QIODevice::ReadOnly))
  141. {
  142. QByteArray ba= xFile.readAll();
  143. ninputsize = ba.size();
  144. if(ninputsize > 0)
  145. {
  146. pstr_ptr = std::shared_ptr<char>(new char[ninputsize]);
  147. memcpy(pstr_ptr.get(),ba.data(),static_cast<size_t>(ninputsize));
  148. }
  149. xFile.close();
  150. }
  151. else
  152. {
  153. std::cout<<"FATAL Errror. Can't Open input File. Please Check Input File."<<std::endl;
  154. return -1;
  155. }
  156. if(ninputsize == 0)
  157. {
  158. std::cout<<"FATAL Error. Input File is empty."<<std::endl;
  159. return -2;
  160. }
  161. std::shared_ptr<char> pout_ptr;
  162. int noutputsize = 0;
  163. int nres;
  164. carmakercvt * pcvt = new carmakercvt();
  165. nres = pcvt->GetRes(strinputname,stroutputname,strcmd,pstr_ptr,ninputsize,pout_ptr,noutputsize);
  166. if(nres != 1)
  167. {
  168. std::cout<<" GetRes Fail.Fail Code is: "<<nres<<std::endl;
  169. if(nres == -1)
  170. {
  171. std::cout<<" Convert Fail. Please Check Input File"<<std::endl;
  172. }
  173. if(nres == -2)
  174. {
  175. std::cout<<" NO Carmaker Service."<<std::endl;
  176. }
  177. if(nres == -3)
  178. {
  179. std::cout<<" Server Can't Connect."<<std::endl;
  180. }
  181. return -3;
  182. }
  183. if(noutputsize == 0)
  184. {
  185. std::cout<<" Get File size is 0,please check"<<std::endl;
  186. return -4;
  187. }
  188. QFile xFileOut;
  189. xFileOut.setFileName(gstr_outputpath);
  190. if(xFileOut.open(QIODevice::ReadWrite))
  191. {
  192. xFileOut.write(pout_ptr.get(),noutputsize);
  193. xFileOut.close();
  194. }
  195. else
  196. {
  197. std::cout<<" Can't Output data to File. "<<" Output Path: "<<gstr_outputpath<<std::endl;
  198. return -5;
  199. }
  200. return 0;
  201. return a.exec();
  202. }