水無瀬の部屋 > Programming > sample > atlnk > cmdline.cpp |
---|
1: //*********************************************************
2: // プロジェクト: atlnk - @link.target
3: // ファイル名: cmdline.cpp
4: //*********************************************************
5: #include "cmdline.h"
6:
7:
8: //---------------------------------------------------------
9: // テスト関数 の 宣言
10: //---------------------------------------------------------
11: DECLARE_TESTPROC( test_cmdline );
12:
13:
14: //---------------------------------------------------------
15: // マクロ定数 の 定義
16: //---------------------------------------------------------
17: #define NULL_VERB ""
18:
19:
20: //*********************************************************
21: // make_cmdline
22: //*********************************************************
23: bool
24: make_cmdline
25: (
26: char *buffer,
27: size_t bufsize,
28: const char *verb
29: )
30: {
31: CALLONCE_TESTPROC( test_cmdline ); // [テスト]
32:
33: // パラメタの仮定
34: ASSERT( 0 < bufsize );
35: ASSERT( IsValidStringBufferPtr( buffer, bufsize ) );
36: DESTROY_TEXT_BUFFER( buffer, bufsize ); // [破壊]
37: ASSERT( !verb || IsValidCommandLineString( verb ) );
38:
39: //
40: char myself[ MAX_PATH_BUF ];
41: VERIFY( GetModuleFile( myself, numof( myself ), null ) );
42:
43: //
44: snprintf
45: (
46: buffer,
47: bufsize,
48: "\"%s\"%s%s%s%s \"%s\"",
49: myself,
50: verb ? " /verb:" : "",
51: verb ? "\"" : "",
52: verb ? verb : "",
53: verb ? "\"" : "",
54: "%1"
55: );
56: buffer[ bufsize-1 ] = '\0';
57:
58: ASSERT( IsValidCommandLineString( buffer ) );
59: ASSERT( strlen( buffer ) < bufsize );
60:
61: return true;
62: }//make_cmdline
63:
64: //*********************************************************
65: // get_cmdline_option
66: //*********************************************************
67: bool
68: get_cmdline_option
69: (
70: char *buffer,
71: size_t bufsize,
72: int argc,
73: char **argv
74: )
75: {
76: CALLONCE_TESTPROC( test_cmdline ); // [テスト]
77:
78: // パラメタの仮定
79: ASSERT( 0 < bufsize );
80: ASSERT( IsValidStringBufferPtr( buffer, bufsize ) );
81: DESTROY_TEXT_BUFFER( buffer, bufsize ); // [破壊]
82: ASSERT( IsValidArgcArgv( argc, argv ) );
83: ASSERT( IsPathMyself( argv[ 0 ] ) ); // 自分自身
84:
85: //
86: strcopy( buffer, bufsize, NULL_VERB );
87:
88: //
89: {for( int i = 1; i < argc; ++i )
90: {
91: ASSERT( IsValidCommandLineString( argv[ i ] ) );
92:
93: if ( strhstri( argv[ i ], "/verb:" ) )
94: {
95: const char *verb = jmschr( argv[ i ], ':' );
96:
97: ASSERT( verb && (':' == verb[ 0 ]) );
98: strcopy( buffer, bufsize, verb+1 );
99: }
100: }}
101:
102: ASSERT( IsValidCommandLineString( buffer ) );
103: ASSERT( strlen( buffer ) < bufsize );
104:
105: return true;
106: }//get_cmdline_option
107:
108:
109: //******************************************************************************************************************
110: // TEST
111: //******************************************************************************************************************
112:
113:
114: #ifdef _DEBUG // デバッグ時のみ
115:
116:
117: //*********************************************************
118: // test_cmdline
119: //*********************************************************
120: DEFINE_TESTPROC( test_cmdline )
121: {
122: //---------------------------------------------------------
123: // 定数 の テスト
124: //---------------------------------------------------------
125:
126: // NULL_VERB
127: ASSERT( IsValidCommandLineString( NULL_VERB ) );
128:
129:
130: //---------------------------------------------------------
131: // ファイルスコープ関数 の テスト
132: //---------------------------------------------------------
133:
134: //---------------------------------------------------------
135: // 公開関数 の テスト
136: //---------------------------------------------------------
137: // make_cmdline(), get_cmdline_option()
138: {
139: const struct
140: {
141: const char *verb;
142: const char *argv1;
143: const char *argv2;
144: }
145: testcase[] =
146: {
147: #define MAKE_TESTCASE( verb, argv1, argv2 ) { verb, argv1, argv2 }
148: MAKE_TESTCASE( null, "%1", null ),
149: MAKE_TESTCASE( "", "/verb:", "%1" ),
150: MAKE_TESTCASE( "verb", "/verb:verb", "%1" ),
151: MAKE_TESTCASE( "v e r b", "/verb:v e r b", "%1" ),
152: #undef MAKE_TESTCASE
153: };
154: {for( int i = 0; i < numof( testcase ); ++i )
155: {
156: char cmdline[ MAX_PATH_BUF ];
157: make_cmdline( cmdline, 1, testcase[ i ].verb );
158: ASSERT( '\0' == *cmdline );
159: make_cmdline( cmdline, numof(cmdline), testcase[ i ].verb );
160: ASSERT( IsValidCommandLineString( cmdline ) );
161:
162: int argc;
163: char **argv = argcargv( &argc, cmdline, FLG_ARGCARGV_MSDEFAULT );
164: ASSERT( IsValidArgcArgv( argc, argv ) );
165: ASSERT( IsPathMyself( argv[ 0 ] ) ); // 自分自身
166: ASSERT( (!testcase[i].argv1 && (argc < 2))
167: || ( testcase[i].argv1 && streql( argv[1], testcase[i].argv1 )) );
168: ASSERT( (!testcase[i].argv2 && (argc < 3))
169: || ( testcase[i].argv2 && streql( argv[2], testcase[i].argv2 )) );
170:
171: char verb[ MAX_PATH_BUF ];
172: get_cmdline_option( verb, 1, argc, argv );
173: ASSERT( '\0' == *verb );
174: get_cmdline_option( verb, numof(verb), argc, argv );
175: ASSERT( (!testcase[i].verb && streql( verb, NULL_VERB ))
176: || ( testcase[i].verb && streql( verb, testcase[i].verb )) );
177:
178: free_argcargv( argc, argv );
179: }}
180: }
181:
182: }//test_cmdline
183:
184:
185: #endif // #ifdef _DEBUG
186:
187:
188: //** end **
参照:
水無瀬の部屋 > sample > atlnk > cmdline.cpp |
---|
このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/atlnk/cmdline_cpp.shtml
同人ダウンロード販売|DL.Getchu.com