source: dev/trunk/ab5.0/jenga/src/common/CmdLine.cpp@ 457

Last change on this file since 457 was 457, checked in by dai_9181, 17 years ago

コマンドライン解析モジュールをリファクタリング。

File size: 2.6 KB
Line 
1#include <jenga/include/common/CmdLine.h>
2
3#include <boost/foreach.hpp>
4
5using namespace Jenga::Common;
6
7CmdLines::CmdLines( const std::string &strCmdLine )
8{
9 char temporary[8192];
10 int i = 0;
11 while( i<(int)strCmdLine.size() )
12 {
13 while( strCmdLine[i] == ' ' )
14 {
15 i ++;
16 }
17
18 if( strCmdLine[i] == '\0' )
19 {
20 break;
21 }
22
23 if( strCmdLine[i] == '/' )
24 {
25 i++;
26
27 // コマンド文字列(オプション名)を抽出
28 for( int j = 0; ; j++, i++ )
29 {
30 if( isalpha( strCmdLine[i] ) || isdigit( strCmdLine[i] ) || strCmdLine[i] == '_' )
31 {
32 temporary[j] = strCmdLine[i];
33 }
34 else
35 {
36 temporary[j] = 0;
37 break;
38 }
39 }
40 while( strCmdLine[i] == ' ' ) i++;
41
42 std::string command = temporary;
43 std::string parameter = "";
44
45 if( (int)strCmdLine.size() > i && strCmdLine[i] != '/' )
46 {
47 if( strCmdLine[i] == ':' )
48 {
49 // ':'はコマンドとパラメータの区切り文字として認識する
50 i++;
51 }
52
53 // パラメータを抽出
54 if( strCmdLine[i] == '\"' )
55 {
56 i++;
57 //ダブルクォートの中身を取り出す
58 for( int j=0; ; j++, i++ )
59 {
60 if( strCmdLine[i] == '\"' || strCmdLine[i] == '\0' )
61 {
62 temporary[j] = 0;
63
64 if( strCmdLine[i] == '\"' ) i++;
65 break;
66 }
67 temporary[j] = strCmdLine[i];
68 }
69 }
70 else
71 {
72 //空白またはヌル文字以前を取り出す
73 for( int j=0; ; j++, i++ )
74 {
75 if( strCmdLine[i] == ' ' || strCmdLine[i] == '\0' )
76 {
77 temporary[j] = 0;
78 break;
79 }
80 temporary[j] = strCmdLine[i];
81 }
82 }
83 parameter = temporary;
84 }
85
86 this->push_back( CmdLine( command, parameter ) );
87 }
88 else if( strCmdLine[i] == '\"' )
89 {
90 i++;
91 //ダブルクォートの中身を取り出す
92 for( int j=0; ; j++, i++ )
93 {
94 if( strCmdLine[i] == '\"' || strCmdLine[i] == '\0' )
95 {
96 temporary[j] = 0;
97
98 if( strCmdLine[i] == '\"' ) i++;
99 break;
100 }
101 temporary[j] = strCmdLine[i];
102 }
103
104 this->push_back( CmdLine( "", temporary ) );
105 }
106 else
107 {
108 //空白またはヌル文字以前を取り出す
109 for( int j=0; ; j++, i++ )
110 {
111 if( strCmdLine[i] == ' ' || strCmdLine[i] == '\0' )
112 {
113 temporary[j] = 0;
114 break;
115 }
116 temporary[j] = strCmdLine[i];
117 }
118
119 this->push_back( CmdLine( "", temporary ) );
120 }
121 }
122}
123
124bool CmdLines::IsExist( const std::string &commandString ) const
125{
126 const CmdLines &cmdLines = *this;
127 BOOST_FOREACH( const CmdLine &cmdLine, cmdLines )
128 {
129 if( cmdLine.GetCommand() == commandString )
130 {
131 return true;
132 }
133 }
134 return false;
135}
Note: See TracBrowser for help on using the repository browser.