Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CCommandLine.h
- #include<string>
- #include<vector>
- // その他のヘッダファイル
- struct COMMANDLINE{
- std::string path; // この実行ファイルのパス
- std::string name; // この実行ファイルの名前
- std::string dirpath; // この実行ファイルがあるディレクトリパス
- std::vector<std::string> argv; // コマンドライン引数 ( 1番目から開始 )
- int argc; // コマンドライン引数の数
- };
- enum COMMANDLINETYPE{
- COMMANDLINE_SEPARATE_SPACE_TYPE = 0,
- COMMANDLINE_SEPARATE_STRING_TYPE = 1,
- COMMANDLINE_NO_SEPARATE_TYPE = 2
- };
- class CCommandLine{
- public:
- CCommandLine( const COMMANDLINE cmdline, int type = COMMANDLINE_SEPARATE_SPACE_TYPE );
- CCommandLine( int type = COMMANDLINE_SEPARATE_SPACE_TYPE );
- ~CCommandLine();
- bool CheckOption( const std::string option );
- bool GetOption( const std::string option, std::string &result );
- int Size( void ) const;
- std::string At( int n ) const;
- std::string GetThisPath( void ) const;
- std::string GetThisName( void ) const;
- std::string GetThisDirPath( void ) const;
- protected:
- COMMANDLINE GetCommandLineEx( void );
- private:
- COMMANDLINE cmdline;
- int type;
- };
- // 本来は分割している。
- // CCommandLine.cpp
- #include"CCommandLine.h"
- // "wchar_t配列 ( または WCHAR配列 ) を char型配列に変換する" の string/wstring版。
- int WcharToString( const wstring wstr, string &result ){
- const int len = wstr.length();
- char ctemp[(len * 2) + 1];
- wchar_t wtemp[len+1];
- wcscpy( wtemp, wstr.c_str() );
- setlocale( LC_CTYPE, "" );
- wcstombs( ctemp, wtemp, (len * 2) + 1 );
- result = ctemp;
- return len;
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 種 類: コンストラクタ
- *
- * 目 的: コマンドライン引数の処理をする
- *
- * 引 数:
- * const COMMANDLINE cmdline := COMMANDLINE構造体
- * int type := どういう仕分けか ( def: COMMANDLINE_SEPARATE_SPACE_TYPE )
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- * 引数の一つ int type で指定できる定数は
- *
- * COMMANDLINE_SEPARATE_SPACE_TYPE : スペース区切り
- *
- * COMMANDLINE_SEPARATE_STRING_TYPE : "=" や ":" といった文字で区切るタイプ
- *
- * COMMANDLINE_NO_SEPARATE_TYPE : 区切り なしでつながっているタイプ
- *
- * の3つ。
- *
- **************************************************************/
- CCommandLine::CCommandLine( const COMMANDLINE cmdline, int type ){
- this->cmdline = cmdline;
- this->type = type;
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 種 類: コンストラクタ
- *
- * 目 的: コマンドライン引数の処理をする
- *
- * 引 数:
- * int type := どういう仕分けか ( def: COMMANDLINE_SEPARATE_SPACE_TYPE )
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- * 引数の一つ int type で指定できる定数は
- *
- * COMMANDLINE_SEPARATE_SPACE_TYPE : スペース区切り
- *
- * COMMANDLINE_SEPARATE_STRING_TYPE : "=" や ":" といった文字で区切るタイプ
- *
- * COMMANDLINE_NO_SEPARATE_TYPE : 区切り なしでつながっているタイプ
- *
- * の3つ。
- *
- * ※ こちらの方は 自動的にCOMMANDLINE構造体オブジェクトを取得するタイプ。
- *
- **************************************************************/
- CCommandLine::CCommandLine( int type ){
- this->type = type;
- this->cmdline = this->GetCommandLineEx();
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 種 類: デストラクタ
- *
- * 目 的:
- *
- * 引 数:
- *
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- *
- *
- **************************************************************/
- CCommandLine::~CCommandLine(){
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 関 数 名: CheckOption
- *
- * 目 的: 指定のスイッチがコマンドライン引数として渡されているかどうかチェックする
- *
- * 引 数:
- * const std::string option := チェックするスイッチ
- *
- * 戻 り 値:
- * bool
- *
- * 使 用 例:
- * if( CCommandLine.CheckOption( チェックするスイッチ ) == true )...
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- *
- *
- **************************************************************/
- bool CCommandLine::CheckOption( const string option ){
- for( int i = 0; i < cmdline.argv.size(); i++ ){
- if( StrcmpEx( cmdline.argv[i], option, false ) == true ) return true;
- }
- return false;
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 関 数 名: GetOption
- *
- * 目 的: 指定のスイッチがコマンドライン引数として渡されている場合、そのデータを取得する
- *
- * 引 数:
- * const std::string option := チェックするスイッチ
- * std::string &result := 戻り値用string
- *
- * 戻 り 値:
- * bool
- *
- * 使 用 例:
- * if( CCommandLine.GetOption( チェックするスイッチ, 戻り値用string ) == true )...
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- *
- *
- **************************************************************/
- bool CCommandLine::GetOption( const string option, string &result ){
- string temp;
- result.clear();
- // -oabc option.size() + 1
- for( int i = 0; i < cmdline.argv.size(); i++ ){
- if( this->type == COMMANDLINE_SEPARATE_SPACE_TYPE ){
- if( StrcmpEx( cmdline.argv[i], option, false ) == true ){
- if( (i + 1) < cmdline.argv.size() ){
- result = cmdline.argv[i+1];
- return true;
- }
- }
- }else if( this->type == COMMANDLINE_SEPARATE_STRING_TYPE ){
- if( cmdline.argv[i].at(0) == option.at(0) && StrstrEx( cmdline.argv[i], option, false ) == true ){
- temp = cmdline.argv[i];
- result = temp.substr( option.size() + 1 );
- return true;
- }
- }else{
- if( cmdline.argv[i].at(0) == option.at(0) && StrstrEx( cmdline.argv[i], option, false ) == true ){
- temp = cmdline.argv[i];
- result = temp.substr( option.size() );
- return true;
- }
- }
- }
- return false;
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 関 数 名: Size const
- *
- * 目 的: コマンドライン引数の数を返す
- *
- * 引 数:
- * void
- *
- * 戻 り 値:
- * int : ファイルパスを含めたコマンドライン引数の数
- *
- * 使 用 例:
- * int cmd = CCommandLine.Size();
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- *
- *
- **************************************************************/
- int CCommandLine::Size( void ) const{
- return cmdline.argv.size();
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 関 数 名: At const
- *
- * 目 的: コマンドライン引数で指定番号の値を返す
- *
- * 引 数:
- * void
- *
- * 戻 り 値:
- * std::string : 文字列データ
- *
- * 使 用 例:
- * std::string cmd = CCommandLine.At();
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- *
- *
- **************************************************************/
- string CCommandLine::At( int n ) const{
- return cmdline.argv.at(n);
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 関 数 名: GetThisPath const
- *
- * 目 的: この実行ファイルのパスを返す
- *
- * 引 数:
- * void
- *
- * 戻 り 値:
- * std::string : この実行ファイルのパス
- *
- * 使 用 例:
- * std::string path = CCommandLine.GetThisPath();
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- *
- *
- **************************************************************/
- string CCommandLine::GetThisPath( void ) const{
- return cmdline.path;
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 関 数 名: GetThisName const
- *
- * 目 的: この実行ファイルの名前を返す
- *
- * 引 数:
- * void
- *
- * 戻 り 値:
- * std::string : この実行ファイルの名前
- *
- * 使 用 例:
- * std::string path = CCommandLine.GetThisName();
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- *
- *
- **************************************************************/
- string CCommandLine::GetThisName( void ) const{
- return cmdline.name;
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 関 数 名: GetThisDirPath const
- *
- * 目 的: この実行ファイルがあるディレクトリのパスを返す
- *
- * 引 数:
- * void
- *
- * 戻 り 値:
- * std::string : この実行ファイルがあるディレクトリのパス
- *
- * 使 用 例:
- * std::string path = CCommandLine.GetThisDirPath();
- *
- * 参 ・ 引:
- *
- *
- * 備 考:
- *
- *
- **************************************************************/
- string CCommandLine::GetThisDirPath( void ) const{
- return cmdline.dirpath;
- }
- /**************************************************************
- *
- * クラス名: CCommandLine
- *
- * 関 数 名: GetCommandLineEx
- *
- * 目 的: コマンドライン引数を取得する
- *
- * 引 数:
- * void
- *
- * 戻 り 値:
- * COMMANDLINE
- *
- * 使 用 例:
- * COMMANDLINE cmdline = CCommandLine.GetCommandLineEx();
- *
- * 参 ・ 引:
- * 「山内の授業補完のページ/Windowsアプリ/argcとargv - 東邦大学理学部情報科学科 山内のサイト」
- *
- * 備 考:
- *
- *
- **************************************************************/
- COMMANDLINE CCommandLine::GetCommandLineEx( void ){
- WCHAR **wCmdLines;
- string strCmdLine;
- int argc;
- vector<string> temp;
- COMMANDLINE cmdline;
- CSystem::GetThisExePath( cmdline.path );
- CPath::GetFileEx( cmdline.path, cmdline.name );
- CPath::GetDirPath( cmdline.path, cmdline.dirpath );
- wCmdLines = CommandLineToArgvW( GetCommandLineW(), &argc );
- for( int i = 0; i < argc; i++ ){
- strCmdLine = WcharToString( wCmdLines[i] );
- cmdline.argv.push_back( strCmdLine );
- strCmdLine.clear();
- }
- cmdline.argc = cmdline.argv.size();
- return cmdline;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement