xodr2rd5main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <QCoreApplication>
  2. #include <QProcess>
  3. #include "stdarg.h"
  4. #include "road.h"
  5. #include "roadbuilder.h"
  6. #include <iostream>
  7. #include <getopt.h>
  8. #include <iostream>
  9. #include "rd5route.h"
  10. static char gstr_inputpath[256];
  11. static char gstr_outputpath[256];
  12. static char gstr_fromroad[256];
  13. static char gstr_fromlane[256];
  14. static char gstr_toroad[256];
  15. static char gstr_tolane[256];
  16. /**
  17. * @brief print_useage
  18. */
  19. void print_useage()
  20. {
  21. std::cout<<" -i --input $xodrfile : set input file path. eq. -i d:/lk.xodr"<<std::endl;
  22. std::cout<<" -o --output $outputfile : set output file. eq. -o d:/test.rd5"<<std::endl;
  23. std::cout<<" -h --help print help"<<std::endl;
  24. }
  25. int GetOptLong(int argc, char *argv[]) {
  26. int nRtn = 0;
  27. int opt; // getopt_long() 的返回值
  28. int digit_optind = 0; // 设置短参数类型及是否需要参数
  29. (void)digit_optind;
  30. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  31. // 第几个元素的描述,即是long_opts的下标值
  32. int option_index = 0;
  33. // 设置短参数类型及是否需要参数
  34. const char *optstring = "i:o:h";
  35. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  36. /*
  37. struct option {
  38. const char * name; // 参数的名称
  39. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  40. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  41. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  42. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  43. };
  44. 其中:
  45. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  46. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  47. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  48. */
  49. static struct option long_options[] = {
  50. {"input", required_argument, NULL, 'i'},
  51. {"output", required_argument, NULL, 'o'},
  52. {"help", no_argument, NULL, 'h'},
  53. // {"optarg", optional_argument, NULL, 'o'},
  54. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  55. };
  56. while ( (opt = getopt_long(argc,
  57. argv,
  58. optstring,
  59. long_options,
  60. &option_index)) != -1) {
  61. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  62. // printf("optarg = %s\n", optarg); // 参数内容
  63. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  64. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  65. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  66. // printf("\n");
  67. switch(opt)
  68. {
  69. case 'i':
  70. strncpy(gstr_inputpath,optarg,255);
  71. break;
  72. case 'o':
  73. strncpy(gstr_outputpath,optarg,255);
  74. break;
  75. case 'h':
  76. print_useage();
  77. nRtn = 1; //because use -h
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. return nRtn;
  84. }
  85. int
  86. ipgErrorExit(tRoad *road)
  87. {
  88. int nErrMsg;
  89. tRoadMessageList msgList;
  90. nErrMsg = RoadGetMessageList(road, RMT_Error, &msgList);
  91. while (--nErrMsg>=0) fprintf(stderr, "%s\n", msgList.msg[nErrMsg].msg);
  92. RoadDeleteMessageList(road,&msgList);
  93. RoadClearMessageBuffer(road, RMT_Error);
  94. return ROAD_Error;
  95. }
  96. void
  97. ipgWarnMsg(tRoad *road)
  98. {
  99. int nWarn;
  100. tRoadMessageList msgList;
  101. nWarn = RoadGetMessageList(road, RMT_Warn, &msgList);
  102. while (--nWarn>=0) fprintf(stderr, "%s\n", msgList.msg[nWarn].msg);
  103. RoadDeleteMessageList(road,&msgList);
  104. RoadClearMessageBuffer(road, RMT_Warn);
  105. }
  106. #include <QFileInfo>
  107. #include "infofile.h"
  108. int main(int argc, char *argv[])
  109. {
  110. QCoreApplication a(argc, argv);
  111. // cInfoFile xInfo;
  112. // tErrorMsg *error;
  113. // xInfo.Read(&error,"D://ConstructionSite");
  114. // char * pval;
  115. // xInfo.Get(&pval,"Vehicle");
  116. snprintf(gstr_fromroad,256,"1");
  117. snprintf(gstr_toroad,256,"3");
  118. snprintf(gstr_fromlane,256,"-1");
  119. snprintf(gstr_tolane,256,"-1");
  120. int nRtn = GetOptLong(argc,argv);
  121. if(nRtn == 1) //show help,so exit.
  122. {
  123. return 0;
  124. }
  125. #ifdef TESTC
  126. snprintf(gstr_inputpath,255,"D:/lk.xlsx");
  127. snprintf(gstr_outputpath,255,"D:/lk.rd5");
  128. #endif
  129. if(strncmp(gstr_inputpath , "",255) == 0)
  130. {
  131. std::cout<<"Please use -i set input file path."<<std::endl;
  132. print_useage();
  133. return 0;
  134. }
  135. char strout[1000];
  136. snprintf(strout,1000,"Input File Path: %s",gstr_inputpath);
  137. std::cout<<strout<<std::endl;
  138. if(strncmp(gstr_outputpath , "",255) == 0)
  139. {
  140. std::cout<<"Please use -o set output file path."<<std::endl;
  141. print_useage();
  142. return 0;
  143. }
  144. snprintf(strout,1000,"Output File Path: %s",gstr_outputpath);
  145. std::cout<<strout<<std::endl;
  146. tRoad * pRoad;
  147. pRoad = RoadNew();
  148. if(pRoad == NULL)
  149. {
  150. std::cout<<" Road New Fail."<<std::endl;
  151. return -1;
  152. }
  153. else
  154. {
  155. std::cout<<" Road New Successfully."<<std::endl;
  156. }
  157. // createRoadFile(pRoad,"D:/z.rd5");
  158. int nrtn = RoadReadOpenDRIVE(pRoad,gstr_inputpath);
  159. if(nrtn<0)
  160. {
  161. ipgErrorExit(pRoad);
  162. return -1;
  163. }
  164. RoadWriteFile(pRoad,gstr_outputpath,NULL);
  165. return 0;
  166. return a.exec();
  167. }