19 #include <ndb_global.h>
22 #include <Properties.hpp>
29 char* gets(
char *
buf,
int bufLen);
30 void push_back(
const char *);
37 ParseInputStream::ParseInputStream(
InputStream & _in,
45 ParseInputStream::gets(
char *
buf,
int bufLen){
47 strncpy(buf, buffer, bufLen);
52 char *t = in.gets(buf, bufLen);
57 ParseInputStream::push_back(
const char * str){
63 ParserImpl::ParserImpl(
const DummyRow * rows,
InputStream & in,
64 bool b_cmd,
bool b_empty,
bool b_iarg)
68 m_breakOnEmpty = b_empty;
69 m_breakOnInvalidArg = b_iarg;
72 ParserImpl::~ParserImpl(){
78 Empty(
const char * str){
81 const int len = strlen(str);
84 for(
int i = 0;
i<len;
i++)
85 if(str[
i] !=
' ' && str[
i] !=
'\t' && str[
i] !=
'\n')
92 Eof(
const char * str) {
return str == 0;}
99 int len = strlen(str);
100 for(len--; str[len] ==
'\n' || str[len] ==
' ' || str[len] ==
'\t'; len--)
104 while(str[pos] ==
' ' || str[pos] ==
'\t')
107 if(str[pos] ==
'\"' && str[len] ==
'\"') {
113 memmove(str, &str[pos], len - pos + 2);
118 split(
char * buf,
char **
name,
char ** value){
120 for (*value=buf; **value; (*value)++) {
121 if (**value ==
':' || **value ==
'=') {
130 * value = (* value + 1);
141 volatile bool * stop)
const
146 bool ownStop =
false;
150 ctx->m_aliasUsed.clear();
152 const unsigned sz =
sizeof(ctx->m_tokenBuffer);
153 ctx->m_currentToken = input.gets(ctx->m_tokenBuffer, sz);
154 if(Eof(ctx->m_currentToken)){
159 int last= strlen(ctx->m_currentToken);
163 if(ctx->m_currentToken[last] !=
'\n'){
165 ctx->m_tokenBuffer[0]=
'\0';
169 if(Empty(ctx->m_currentToken)){
174 trim(ctx->m_currentToken);
175 ctx->m_currentCmd = matchCommand(ctx, ctx->m_currentToken, m_rows);
176 if(ctx->m_currentCmd == 0){
183 bool invalidArgument =
false;
184 ctx->m_currentToken = input.gets(ctx->m_tokenBuffer, sz);
187 !Eof(ctx->m_currentToken) &&
188 !Empty(ctx->m_currentToken)){
189 if(ctx->m_currentToken[0] != 0){
190 trim(ctx->m_currentToken);
191 if(!parseArg(ctx, ctx->m_currentToken, ctx->m_currentCmd + 1, p)){
193 invalidArgument =
true;
197 ctx->m_currentToken = input.gets(ctx->m_tokenBuffer, sz);
203 if(!m_breakOnInvalidArg){
205 tmp = input.gets(buf, sz);
206 }
while((! * stop) && !Eof(tmp) && !Empty(tmp));
217 if(!checkMandatory(ctx, p)){
226 for(
unsigned i = 0;
i<ctx->m_aliasUsed.size();
i++){
229 tmp.
put(
"name", alias->name);
230 tmp.
put(
"realName", alias->realName);
231 p->
put(
"$ALIAS",
i, &tmp);
233 p->
put(
"$ALIAS", ctx->m_aliasUsed.size());
241 ParserImpl::matchCommand(Context* ctx,
const char* buf,
const DummyRow rows[]){
242 const char * name =
buf;
243 const DummyRow * tmp = &rows[0];
244 while(tmp->name != 0 && name != 0){
245 if(strcmp(tmp->name, name) == 0){
246 if(tmp->type == DummyRow::Cmd)
248 if(tmp->type == DummyRow::CmdAlias){
250 ctx->m_aliasUsed.push_back(tmp);
251 name = tmp->realName;
262 ParserImpl::matchArg(Context* ctx,
const char * buf,
const DummyRow rows[]){
263 const char * name =
buf;
264 const DummyRow * tmp = &rows[0];
265 while(tmp->name != 0){
266 const DummyRow::Type t = tmp->type;
267 if(t != DummyRow::Arg && t != DummyRow::ArgAlias && t !=DummyRow::CmdAlias)
269 if(t !=DummyRow::CmdAlias && strcmp(tmp->name, name) == 0){
270 if(tmp->type == DummyRow::Arg){
273 if(tmp->type == DummyRow::ArgAlias){
275 ctx->m_aliasUsed.push_back(tmp);
276 name = tmp->realName;
287 ParserImpl::parseArg(Context * ctx,
289 const DummyRow * rows,
293 if(!split(buf, &name, &value)){
297 const DummyRow * arg = matchArg(ctx, name, rows);
303 switch(arg->argType){
304 case DummyRow::String:
305 if(p->
put(arg->name, value))
310 int c = sscanf(value,
"%u", &i);
315 if(p->
put(arg->name, i))
320 case DummyRow::Properties: {
328 if(p->getPropertiesErrno() == E_PROPERTIES_ELEMENT_ALREADY_EXISTS){
338 ParserImpl::checkMandatory(Context* ctx,
const Properties* props){
339 const DummyRow * tmp = &ctx->m_currentCmd[1];
340 while(tmp->name != 0 && tmp->type == DummyRow::Arg){
344 ctx->m_currentArg = tmp;
355 #include <NdbTap.hpp>
359 char *str, *
name, *value;
362 str = strdup(
"x=c:\\windows");
363 OK(split(str, &name, &value));
364 OK(!strcmp(name,
"x"));
365 OK(!strcmp(value,
"c:\\windows"));