[PR]

水無瀬の部屋 > Programming > sample > sendsend > mkcmdln.cpp
最終更新日: 2007/03/21

   1: //*********************************************************
   2: // プロジェクト: Send to 'SendTo'
   3: //   ファイル名: mkcmdln.cpp
   4: //*********************************************************
   5: #include "mkcmdln.h"
   6: 
   7: 
   8: //---------------------------------------------------------
   9: // テスト関数 の 宣言
  10: //---------------------------------------------------------
  11: DECLARE_TESTPROC( test_mkcmdln );
  12: 
  13: 
  14: //---------------------------------------------------------
  15: // ファイルスコープ関数 の 宣言
  16: //---------------------------------------------------------
  17: static bool exist_param( int argc, char **argv, const char *param );
  18: static size_t replace_param( char *buf, size_t bufsize, int argc, char **argv, char **argv_raw, const char *a_param, const char *r_param );
  19: 
  20: 
  21: //*********************************************************
  22: // commandline_replace_param
  23: //*********************************************************
  24: char *
  25: commandline_replace_param
  26: 	(
  27: 		const char *cmdline, 
  28: 		const char *a_param,
  29: 		const char *r_param
  30: 	)
  31: {
  32: 	CALLONCE_TESTPROC( test_mkcmdln ); // [テスト]
  33: 
  34: 	// パラメタの仮定
  35: //	ASSERT( IsValidWindow( hWnd ) );
  36: 
  37: 	int argc;
  38: 	char **argv = argcargv( &argc, cmdline, FLG_ARGCARGV_MSDEFAULT );
  39: 	if ( argv ) 
  40: 	{
  41: 		int argc_raw;
  42: 		char **argv_raw = argcargv( &argc_raw, cmdline, 0 );
  43: 		if ( argv_raw ) 
  44: 		{
  45: 			ASSERT( argc == argc_raw );
  46: 
  47: 			if ( (a_param && !exist_param( argc, argv, a_param ) )
  48: 			  || (r_param &&  exist_param( argc, argv, r_param ) ) )
  49: 			{
  50: 				const size_t bufsize = replace_param( null, 0, argc, argv, argv_raw, a_param, r_param );
  51: 				char *buf = (char *)malloc( bufsize * sizeof( *buf ) );
  52: 				if ( buf )
  53: 				{
  54: 					VERIFY( bufsize == replace_param( buf, bufsize, argc, argv, argv_raw, a_param, r_param ) );
  55: 
  56: 					free_argcargv( argc_raw, argv_raw );
  57: 					free_argcargv( argc, argv );
  58: 					return buf;
  59: 				}
  60: 			}
  61: 			free_argcargv( argc_raw, argv_raw );
  62: 		}
  63: 		free_argcargv( argc, argv );
  64: 	}
  65: 
  66: 	return null;
  67: }//commandline_replace_param
  68: 
  69: 
  70: //******************************************************************************************************************
  71: //
  72: //******************************************************************************************************************
  73: //*********************************************************
  74: // exist_param
  75: //*********************************************************
  76: static
  77: bool
  78: exist_param
  79: 	(
  80: 		      int    argc,
  81: 		      char **argv,
  82: 		const char  *param
  83: 	)
  84: {
  85: 	// パラメタの仮定
  86: 	ASSERT( IsValidArgcArgv( argc, argv ) );
  87: 
  88: 	{for( int i = 0; i < argc; ++i )
  89: 	{
  90: 		if ( streqli( param, argv[ i ] ) )
  91: 		{
  92: 			return true;
  93: 		}
  94: 	}}
  95: 
  96: 	return false;
  97: }//exist_param
  98: 
  99: //*********************************************************
 100: // replace_param
 101: //*********************************************************
 102: static
 103: size_t
 104: replace_param
 105: 	(
 106: 		      char    *buf,
 107: 			  size_t   bufsize,
 108: 		      int      argc,
 109: 		      char   **argv,
 110: 		      char   **argv_raw,
 111: 		const char    *a_param,
 112: 		const char    *r_param
 113: 	)
 114: {
 115: 	// パラメタの仮定
 116: 	ASSERT(  buf || (!buf && (0 == bufsize)) );
 117: 	ASSERT( IsValidArgcArgv( argc, argv ) );
 118: 	ASSERT( IsValidArgcArgv( argc, argv_raw ) );
 119: 
 120: 	//
 121: 	size_t count = 0;
 122: 	
 123: 	// pgname
 124: 	if ( argv_raw[ 0 ] )
 125: 	{
 126: 		count += strputs( buf, bufsize, count, argv_raw[ 0 ] );
 127: 	}
 128: 
 129: 	// a_param
 130: 	if ( a_param )
 131: 	{
 132: 		count += strputs( buf, bufsize, count, " " );
 133: 		count += strputs( buf, bufsize, count, a_param );
 134: 	}
 135: 
 136: 	//
 137: 	{for( int i = 1; i < argc; ++i )
 138: 	{
 139: 		ASSERT( !a_param || !streqli( a_param, argv[ i ] ) );
 140: 
 141: 		if ( !r_param || !streqli( r_param, argv[ i ] ) )
 142: 		{
 143: 			count += strputs( buf, bufsize, count, " " );
 144: 			count += strputs( buf, bufsize, count, argv_raw[ i ] );
 145: 		}
 146: 	}}
 147: 
 148: 	//
 149: 	if (  buf && (count < bufsize) ) { buf[ count ] = '\0'; }
 150: 	if ( !buf || (count < bufsize) ) { ++count; }
 151: 
 152: 	ASSERT( !buf || IsValidStringPtr( buf ) );
 153: 	ASSERT( !buf
 154: 		|| (count   == replace_param( null, 0, argc, argv, argv_raw, a_param, r_param ))
 155: 		|| (bufsize <= replace_param( null, 0, argc, argv, argv_raw, a_param, r_param )) );
 156: 	return count;
 157: }//replace_param
 158: 
 159: 
 160: //******************************************************************************************************************
 161: // TEST
 162: //******************************************************************************************************************
 163: 
 164: 
 165: #ifdef _DEBUG // デバッグ時のみ
 166: 
 167: 
 168: //*********************************************************
 169: // test_cmdline()
 170: //*********************************************************
 171: DEFINE_TESTPROC( test_mkcmdln )
 172: {
 173: 	//---------------------------------------------------------
 174: 	// 定数 の テスト
 175: 	//---------------------------------------------------------
 176: 
 177: 	//---------------------------------------------------------
 178: 	// ファイルスコープ関数 の テスト
 179: 	//---------------------------------------------------------
 180: 
 181: 	//---------------------------------------------------------
 182: 	// 公開関数 の テスト
 183: 	//---------------------------------------------------------
 184: 
 185: 	{
 186: 		const struct 
 187: 			{
 188: 				const char *cmdline; 
 189: 				const char *a_param;
 190: 				const char *r_param;
 191: 				const char *result;
 192: 			}
 193: 		testcase[] =
 194: 			{
 195: #define MAKE_TESTCASE( cmdline, a_param, r_param, result ) { cmdline, a_param, r_param, result }
 196: 				MAKE_TESTCASE( "a  b  c",    "d",  null, "a d b c" ),
 197: 				MAKE_TESTCASE( "a  b  c  d", null, "b",  "a c d" ),
 198: 				MAKE_TESTCASE( "a  b  c  d", null, "c",  "a b d" ),
 199: 				MAKE_TESTCASE( "a  b  c  d", null, "d",  "a b c" ),
 200: 				MAKE_TESTCASE( "a b",        null, null, null ),
 201: #undef MAKE_TESTCASE
 202: 			};
 203: 		{for( int i = 0; i < numof( testcase ); ++i )
 204: 		{
 205: 			char *result = commandline_replace_param( testcase[i].cmdline, testcase[i].a_param, testcase[i].r_param );
 206: 			VERIFY( (!result && !testcase[i].result) || streql( result, testcase[i].result ) );
 207: 			if ( result ){ free( result ); }
 208: 		}}
 209: 	}
 210: 
 211: }//test_mkcmdln
 212: 
 213: 
 214: #endif // #ifdef _DEBUG
 215: 
 216: 
 217: //** end **

参照:


Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬の部屋 > sample > sendsend > mkcmdln.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/sendsend/mkcmdln_cpp.shtml
『新妻LOVELY×CATION』を応援しています!