main.cpp 7.7 KB

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