Advertisement
Guest User

Untitled

a guest
May 8th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.85 KB | None | 0 0
  1. // CCommandLine.h
  2.  
  3. #include<string>
  4. #include<vector>
  5. // その他のヘッダファイル
  6.  
  7. struct COMMANDLINE{
  8. std::string path; // この実行ファイルのパス
  9. std::string name; // この実行ファイルの名前
  10. std::string dirpath; // この実行ファイルがあるディレクトリパス
  11.  
  12. std::vector<std::string> argv; // コマンドライン引数 ( 1番目から開始 )
  13.  
  14. int argc; // コマンドライン引数の数
  15. };
  16.  
  17. enum COMMANDLINETYPE{
  18. COMMANDLINE_SEPARATE_SPACE_TYPE = 0,
  19. COMMANDLINE_SEPARATE_STRING_TYPE = 1,
  20. COMMANDLINE_NO_SEPARATE_TYPE = 2
  21. };
  22.  
  23. class CCommandLine{
  24. public:
  25. CCommandLine( const COMMANDLINE cmdline, int type = COMMANDLINE_SEPARATE_SPACE_TYPE );
  26. CCommandLine( int type = COMMANDLINE_SEPARATE_SPACE_TYPE );
  27. ~CCommandLine();
  28.  
  29. bool CheckOption( const std::string option );
  30. bool GetOption( const std::string option, std::string &result );
  31. int Size( void ) const;
  32. std::string At( int n ) const;
  33. std::string GetThisPath( void ) const;
  34. std::string GetThisName( void ) const;
  35. std::string GetThisDirPath( void ) const;
  36. protected:
  37. COMMANDLINE GetCommandLineEx( void );
  38. private:
  39. COMMANDLINE cmdline;
  40. int type;
  41. };
  42.  
  43.  
  44. // 本来は分割している。
  45.  
  46. // CCommandLine.cpp
  47.  
  48. #include"CCommandLine.h"
  49.  
  50.  
  51. // "wchar_t配列 ( または WCHAR配列 ) を char型配列に変換する" の string/wstring版。
  52. int WcharToString( const wstring wstr, string &result ){
  53. const int len = wstr.length();
  54.  
  55. char ctemp[(len * 2) + 1];
  56. wchar_t wtemp[len+1];
  57.  
  58. wcscpy( wtemp, wstr.c_str() );
  59.  
  60. setlocale( LC_CTYPE, "" );
  61.  
  62. wcstombs( ctemp, wtemp, (len * 2) + 1 );
  63.  
  64. result = ctemp;
  65. return len;
  66. }
  67.  
  68.  
  69.  
  70. /**************************************************************
  71. *
  72. * クラス名: CCommandLine
  73. *
  74. * 種 類: コンストラクタ
  75. *
  76. * 目 的: コマンドライン引数の処理をする
  77. *
  78. * 引 数:
  79. * const COMMANDLINE cmdline := COMMANDLINE構造体
  80. * int type := どういう仕分けか ( def: COMMANDLINE_SEPARATE_SPACE_TYPE )
  81. *
  82. * 参 ・ 引:
  83. *
  84. *
  85. * 備 考:
  86. * 引数の一つ int type で指定できる定数は
  87. *
  88. * COMMANDLINE_SEPARATE_SPACE_TYPE : スペース区切り
  89. *
  90. * COMMANDLINE_SEPARATE_STRING_TYPE : "=" や ":" といった文字で区切るタイプ
  91. *
  92. * COMMANDLINE_NO_SEPARATE_TYPE : 区切り なしでつながっているタイプ
  93. *
  94. * の3つ。
  95. *
  96. **************************************************************/
  97.  
  98. CCommandLine::CCommandLine( const COMMANDLINE cmdline, int type ){
  99. this->cmdline = cmdline;
  100. this->type = type;
  101. }
  102.  
  103.  
  104.  
  105. /**************************************************************
  106. *
  107. * クラス名: CCommandLine
  108. *
  109. * 種 類: コンストラクタ
  110. *
  111. * 目 的: コマンドライン引数の処理をする
  112. *
  113. * 引 数:
  114. * int type := どういう仕分けか ( def: COMMANDLINE_SEPARATE_SPACE_TYPE )
  115. *
  116. * 参 ・ 引:
  117. *
  118. *
  119. * 備 考:
  120. * 引数の一つ int type で指定できる定数は
  121. *
  122. * COMMANDLINE_SEPARATE_SPACE_TYPE : スペース区切り
  123. *
  124. * COMMANDLINE_SEPARATE_STRING_TYPE : "=" や ":" といった文字で区切るタイプ
  125. *
  126. * COMMANDLINE_NO_SEPARATE_TYPE : 区切り なしでつながっているタイプ
  127. *
  128. * の3つ。
  129. *
  130. * ※ こちらの方は 自動的にCOMMANDLINE構造体オブジェクトを取得するタイプ。
  131. *
  132. **************************************************************/
  133.  
  134. CCommandLine::CCommandLine( int type ){
  135. this->type = type;
  136. this->cmdline = this->GetCommandLineEx();
  137. }
  138.  
  139.  
  140.  
  141. /**************************************************************
  142. *
  143. * クラス名: CCommandLine
  144. *
  145. * 種 類: デストラクタ
  146. *
  147. * 目 的:
  148. *
  149. * 引 数:
  150. *
  151. *
  152. * 参 ・ 引:
  153. *
  154. *
  155. * 備 考:
  156. *
  157. *
  158. **************************************************************/
  159.  
  160. CCommandLine::~CCommandLine(){
  161.  
  162. }
  163.  
  164.  
  165.  
  166. /**************************************************************
  167. *
  168. * クラス名: CCommandLine
  169. *
  170. * 関 数 名: CheckOption
  171. *
  172. * 目 的: 指定のスイッチがコマンドライン引数として渡されているかどうかチェックする
  173. *
  174. * 引 数:
  175. * const std::string option := チェックするスイッチ
  176. *
  177. * 戻 り 値:
  178. * bool
  179. *
  180. * 使 用 例:
  181. * if( CCommandLine.CheckOption( チェックするスイッチ ) == true )...
  182. *
  183. * 参 ・ 引:
  184. *
  185. *
  186. * 備 考:
  187. *
  188. *
  189. **************************************************************/
  190.  
  191. bool CCommandLine::CheckOption( const string option ){
  192. for( int i = 0; i < cmdline.argv.size(); i++ ){
  193. if( StrcmpEx( cmdline.argv[i], option, false ) == true ) return true;
  194. }
  195. return false;
  196. }
  197.  
  198.  
  199.  
  200. /**************************************************************
  201. *
  202. * クラス名: CCommandLine
  203. *
  204. * 関 数 名: GetOption
  205. *
  206. * 目 的: 指定のスイッチがコマンドライン引数として渡されている場合、そのデータを取得する
  207. *
  208. * 引 数:
  209. * const std::string option := チェックするスイッチ
  210. * std::string &result := 戻り値用string
  211. *
  212. * 戻 り 値:
  213. * bool
  214. *
  215. * 使 用 例:
  216. * if( CCommandLine.GetOption( チェックするスイッチ, 戻り値用string ) == true )...
  217. *
  218. * 参 ・ 引:
  219. *
  220. *
  221. * 備 考:
  222. *
  223. *
  224. **************************************************************/
  225.  
  226. bool CCommandLine::GetOption( const string option, string &result ){
  227. string temp;
  228.  
  229. result.clear();
  230.  
  231. // -oabc option.size() + 1
  232. for( int i = 0; i < cmdline.argv.size(); i++ ){
  233. if( this->type == COMMANDLINE_SEPARATE_SPACE_TYPE ){
  234. if( StrcmpEx( cmdline.argv[i], option, false ) == true ){
  235. if( (i + 1) < cmdline.argv.size() ){
  236. result = cmdline.argv[i+1];
  237. return true;
  238. }
  239. }
  240. }else if( this->type == COMMANDLINE_SEPARATE_STRING_TYPE ){
  241. if( cmdline.argv[i].at(0) == option.at(0) && StrstrEx( cmdline.argv[i], option, false ) == true ){
  242. temp = cmdline.argv[i];
  243. result = temp.substr( option.size() + 1 );
  244. return true;
  245. }
  246. }else{
  247. if( cmdline.argv[i].at(0) == option.at(0) && StrstrEx( cmdline.argv[i], option, false ) == true ){
  248. temp = cmdline.argv[i];
  249. result = temp.substr( option.size() );
  250. return true;
  251. }
  252. }
  253. }
  254. return false;
  255. }
  256.  
  257.  
  258.  
  259. /**************************************************************
  260. *
  261. * クラス名: CCommandLine
  262. *
  263. * 関 数 名: Size const
  264. *
  265. * 目 的: コマンドライン引数の数を返す
  266. *
  267. * 引 数:
  268. * void
  269. *
  270. * 戻 り 値:
  271. * int : ファイルパスを含めたコマンドライン引数の数
  272. *
  273. * 使 用 例:
  274. * int cmd = CCommandLine.Size();
  275. *
  276. * 参 ・ 引:
  277. *
  278. *
  279. * 備 考:
  280. *
  281. *
  282. **************************************************************/
  283.  
  284. int CCommandLine::Size( void ) const{
  285. return cmdline.argv.size();
  286. }
  287.  
  288.  
  289.  
  290. /**************************************************************
  291. *
  292. * クラス名: CCommandLine
  293. *
  294. * 関 数 名: At const
  295. *
  296. * 目 的: コマンドライン引数で指定番号の値を返す
  297. *
  298. * 引 数:
  299. * void
  300. *
  301. * 戻 り 値:
  302. * std::string : 文字列データ
  303. *
  304. * 使 用 例:
  305. * std::string cmd = CCommandLine.At();
  306. *
  307. * 参 ・ 引:
  308. *
  309. *
  310. * 備 考:
  311. *
  312. *
  313. **************************************************************/
  314.  
  315. string CCommandLine::At( int n ) const{
  316. return cmdline.argv.at(n);
  317. }
  318.  
  319.  
  320.  
  321. /**************************************************************
  322. *
  323. * クラス名: CCommandLine
  324. *
  325. * 関 数 名: GetThisPath const
  326. *
  327. * 目 的: この実行ファイルのパスを返す
  328. *
  329. * 引 数:
  330. * void
  331. *
  332. * 戻 り 値:
  333. * std::string : この実行ファイルのパス
  334. *
  335. * 使 用 例:
  336. * std::string path = CCommandLine.GetThisPath();
  337. *
  338. * 参 ・ 引:
  339. *
  340. *
  341. * 備 考:
  342. *
  343. *
  344. **************************************************************/
  345.  
  346. string CCommandLine::GetThisPath( void ) const{
  347. return cmdline.path;
  348. }
  349.  
  350.  
  351.  
  352. /**************************************************************
  353. *
  354. * クラス名: CCommandLine
  355. *
  356. * 関 数 名: GetThisName const
  357. *
  358. * 目 的: この実行ファイルの名前を返す
  359. *
  360. * 引 数:
  361. * void
  362. *
  363. * 戻 り 値:
  364. * std::string : この実行ファイルの名前
  365. *
  366. * 使 用 例:
  367. * std::string path = CCommandLine.GetThisName();
  368. *
  369. * 参 ・ 引:
  370. *
  371. *
  372. * 備 考:
  373. *
  374. *
  375. **************************************************************/
  376.  
  377. string CCommandLine::GetThisName( void ) const{
  378. return cmdline.name;
  379. }
  380.  
  381.  
  382.  
  383. /**************************************************************
  384. *
  385. * クラス名: CCommandLine
  386. *
  387. * 関 数 名: GetThisDirPath const
  388. *
  389. * 目 的: この実行ファイルがあるディレクトリのパスを返す
  390. *
  391. * 引 数:
  392. * void
  393. *
  394. * 戻 り 値:
  395. * std::string : この実行ファイルがあるディレクトリのパス
  396. *
  397. * 使 用 例:
  398. * std::string path = CCommandLine.GetThisDirPath();
  399. *
  400. * 参 ・ 引:
  401. *
  402. *
  403. * 備 考:
  404. *
  405. *
  406. **************************************************************/
  407.  
  408. string CCommandLine::GetThisDirPath( void ) const{
  409. return cmdline.dirpath;
  410. }
  411.  
  412.  
  413.  
  414. /**************************************************************
  415. *
  416. * クラス名: CCommandLine
  417. *
  418. * 関 数 名: GetCommandLineEx
  419. *
  420. * 目 的: コマンドライン引数を取得する
  421. *
  422. * 引 数:
  423. * void
  424. *
  425. * 戻 り 値:
  426. * COMMANDLINE
  427. *
  428. * 使 用 例:
  429. * COMMANDLINE cmdline = CCommandLine.GetCommandLineEx();
  430. *
  431. * 参 ・ 引:
  432. * 「山内の授業補完のページ/Windowsアプリ/argcとargv - 東邦大学理学部情報科学科 山内のサイト」
  433. *
  434. * 備 考:
  435. *
  436. *
  437. **************************************************************/
  438.  
  439. COMMANDLINE CCommandLine::GetCommandLineEx( void ){
  440.  
  441. WCHAR **wCmdLines;
  442. string strCmdLine;
  443. int argc;
  444.  
  445. vector<string> temp;
  446.  
  447. COMMANDLINE cmdline;
  448.  
  449. CSystem::GetThisExePath( cmdline.path );
  450.  
  451. CPath::GetFileEx( cmdline.path, cmdline.name );
  452.  
  453. CPath::GetDirPath( cmdline.path, cmdline.dirpath );
  454.  
  455. wCmdLines = CommandLineToArgvW( GetCommandLineW(), &argc );
  456.  
  457.  
  458. for( int i = 0; i < argc; i++ ){
  459. strCmdLine = WcharToString( wCmdLines[i] );
  460. cmdline.argv.push_back( strCmdLine );
  461. strCmdLine.clear();
  462. }
  463.  
  464. cmdline.argc = cmdline.argv.size();
  465. return cmdline;
  466. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement