OSDN Git Service

fix gcc worring
[hos/hos-v4a.git] / cfgrtr / source / analyze.cpp
1 /**
2  *  Hyper Operating System V4 Advance Configurator
3  *
4  * @file  analyze.cpp
5  * @brief  %jp{構文解析クラス}%en{lexer cless}
6  *
7  * Copyright (C) 1998-2014 by Project HOS
8  * http://sourceforge.jp/projects/hos/
9  */
10
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include "defercd.h"
16 #include "analyze.h"
17
18
19
20 // ステートメントをAPI名とパラメーターに分割
21 int CAnalyze::SplitState(char* pszApiName, char* pszParams, const char *pszState)
22 {
23         int iErr;
24
25         // コマンド名の切り出し
26         while ( *pszState != '(' )
27         {
28                 if ( *pszState == '\0' )
29                 {
30                         return CFG_ERR_SYNTAX;
31                 }
32                 *pszApiName++ = *pszState++;
33         }
34         *pszApiName = '\0';
35         pszState++;
36
37         // パラメーター部の切り出し
38         iErr = SearchChar(pszParams, pszState, ')');
39         if ( iErr != CFG_ERR_OK )
40         {
41                 return iErr;
42         }
43
44         // 後続チェック
45         if ( *pszState != '\0' )
46         {
47                 return CFG_ERR_SEMICOLON;
48         }
49
50         return CFG_ERR_OK;
51 }
52
53
54 // 特定文字のまでの切り出し
55 int CAnalyze::SearchChar(char* pszBuf, const char* &pszText, char c)
56 {
57         char cDelimiter = 0;
58         bool blText = false;
59         bool blEsc  = false;
60         int  iErr;
61
62         for ( ; ; )
63         {
64                 // 終端チェック
65                 if ( *pszText == '\0' )
66                 {
67                         iErr = CFG_ERR_SYNTAX;
68                         break;
69                 }
70
71                 // 文字チェック
72                 if ( *pszText == c )
73                 {
74                         pszText++;
75                         iErr = CFG_ERR_OK;
76                         break;
77                 }
78
79                 // '\' の次の文字は無条件にエスケープ
80                 if ( blEsc )
81                 {
82                         *pszBuf++ = *pszText++;
83                         blEsc = false;
84                         continue;
85                 }
86                 blEsc = false;
87
88                 // '\' チェック
89                 if ( *pszBuf == '\\' )
90                 {
91                         *pszBuf++ = *pszText++;
92                         blEsc = true;
93                         continue;
94                 }
95
96                 // 文字列開始チェック
97                 if ( (!blText && *pszText == '\"') || *pszText == '\'' )
98                 {
99                         cDelimiter = *pszText;
100                         *pszBuf++ = *pszText++;
101                         blText = true;
102                         continue;
103                 }
104
105                 // 文字列内部
106                 if ( blText )
107                 {
108                         if ( *pszText == cDelimiter )
109                         {
110                                 *pszBuf++ = *pszText++;
111                                 blText = false;
112                         }
113                         else
114                         {
115                                 *pszBuf++ = *pszText++;
116                         }
117                         continue;
118                 }
119
120                 // '{' チェック
121                 if ( *pszText == '{' )
122                 {
123                         *pszBuf++ = *pszText++;
124                         iErr = SearchChar(pszBuf, pszText, '}');
125                         if ( iErr != CFG_ERR_OK )
126                         {
127                                 iErr = CFG_ERR_BRACE;
128                         }
129                         pszBuf  += strlen(pszBuf);
130                         *pszBuf++ = '}';
131                         continue;
132                 }
133
134                 // '(' チェック
135                 if ( *pszText == '(' )
136                 {
137                         *pszBuf++ = *pszText++;
138                         iErr = SearchChar(pszBuf, pszText, ')');
139                         if ( iErr != CFG_ERR_OK )
140                         {
141                                 iErr = CFG_ERR_PAREN;
142                         }
143                         pszBuf  += strlen(pszBuf);
144                         *pszBuf++ = ')';
145                         continue;
146                 }
147
148                 *pszBuf++ = *pszText++;
149         }
150
151         *pszBuf = '\0';
152
153         return iErr;
154 }
155
156
157 // パラメーターの切り出し
158 int CAnalyze::GetParameter(char* pszBuf, const char* &pszText)
159 {
160         int iErr;
161
162         // コンマまで切り出す
163         iErr = SearchChar(pszBuf, pszText, ',');
164         if ( iErr == CFG_ERR_SYNTAX )
165         {
166                 // 括弧対応OKで終端ならコンマが見つからなくても可
167                 iErr = CFG_ERR_OK;
168         }
169
170         return iErr;
171 }
172
173
174 // 文字列定数を展開する
175 int CAnalyze::DecodeText(char *pszBuf, const char* pszText)
176 {
177         bool blEsc = false;
178
179         if ( *pszText++ != '\"') 
180         {
181                 return CFG_ERR_TEXT;
182         }
183
184         for ( ; ; )
185         {
186                 if ( *pszText == '\0' )
187                 {
188                         return CFG_ERR_TEXT;
189                 }
190
191                 // 前の文字が '\' なら読み飛ばし
192                 if ( blEsc )
193                 {
194                         *pszBuf++ = *pszText++;
195                         blEsc = false;
196                         continue;
197                 }
198
199                 // エスケープ文字チェック
200                 if ( *pszText == '\\' )
201                 {
202                         pszText++;
203                         blEsc = true;
204                         continue;
205                 }
206
207                 // 終端チェック
208                 if ( *pszText == '\"' )
209                 {
210                         pszText++;
211                         break;
212                 }
213
214                 *pszBuf++ = *pszText++;
215         }
216
217         // 完結していなければエラー
218         if ( *pszText != '\0' )
219         {
220                         return CFG_ERR_TEXT;
221         }
222
223         *pszBuf = '\0';
224
225         return CFG_ERR_OK;
226 }
227
228
229 // 前後の空白を削除する
230 void CAnalyze::SpaceCut(char* pszText)
231 {
232         char *pszTmp;
233         int  i = 0;
234
235         pszTmp = pszText;
236
237         // 先頭の空白を読み飛ばす
238         while ( *pszTmp == ' ' )
239         {
240                 pszTmp++;
241         }
242
243         // コピー
244         while ( *pszTmp != '\0' )
245         {
246                 pszText[i++] = *pszTmp++;
247         }
248
249         // 末尾の空白を削除
250         while ( i > 0 && pszText[i - 1] == ' ' )
251         {
252                 i--;
253         }
254
255         pszText[i] = '\0';
256 }
257
258
259 // end of file