summaryrefslogtreecommitdiffstats
path: root/mpeglib/example/yaf/yafcore/parser.cpp
blob: f9e8fdaab81305a43760f5bb8e0c1f5ef4ecb050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
  Checks if a valid Command Line is avaiable
  Copyright (C) 1998  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "parser.h"


Parser::Parser() {
  commandLine=new CommandLine();
  parseString=new Buffer(200);
}


Parser::~Parser() {
  delete commandLine;
  delete parseString;
}

void Parser::setParseString(char* parseString){
  (this->parseString)->setData(parseString);
  // clear everything in commandline
  commandLine->clear();
}

void Parser::parse() {
  int commandCounter=0;
  
  parse(parseString->getData(),&commandCounter);
  commandLine->setCommandCount(commandCounter);
}


void Parser::parse(char* strStart,int* nCommand){
  char* doppelPkt;
  char* current;
  char* space;

  if (strlen(strStart) == 0) return;

  doppelPkt=strchr(strStart,':');
  if (doppelPkt == NULL) return;
  (*doppelPkt)='\0';
  commandLine->setIdentifier(*nCommand,strStart);
  strStart=++doppelPkt;
  current=strStart;

  // Now two possibilities:
  // a command with Msg : means the rest ist the value
  // a command different from Msg means: further processing

  if (strcmp("Msg",commandLine->getIdentifier(*nCommand)) == 0) {
    commandLine->setValue((*nCommand),current);
    (*nCommand)++;
    return;
  }

  // Now two possibilities:
  // a command with "Ret:" means string until the the ")" is the value
  // a command different from "Ret:" means: the string until the first space
  //                                        ist the value
  if (strcmp("Ret",commandLine->getIdentifier(*nCommand)) == 0) {
    char* seperator;
    seperator=strchr(current,')');
    if (seperator == NULL) {
      commandLine->setValue((*nCommand),current);
      (*nCommand)++;
      return;
    } else {
      (*seperator)='\0';
      current++;
      commandLine->setValue((*nCommand),current);
      (*nCommand)++;    
      seperator++;  
      if (strlen(seperator) == 0) return;
      parse(++seperator,&(*nCommand));
      return;
    }
  }
 
  space=strchr(current,' ');
  if (space == NULL) {
    commandLine->setValue((*nCommand),current);
    (*nCommand)++;
    return;
  }    
  (*space)='\0';
  commandLine->setValue((*nCommand),current);
  parse(++space,&(++(*nCommand)));
}


  

int Parser::isOK(){
  // a Commandline ist valid when it tqcontains :

  // * 2 * (identifer/value)
  // * first identifier ist "Command"
  // * second identifier is "Msg"
  

  // or:
  // 3 identifier
  // first: "Command"
  // second "Ret"
  // third: "Msg"

  if (commandLine->getCommandCount() == 2){
    if (strcmp("Command",commandLine->getIdentifier(0)) != 0) return 0;
    if (strcmp("Msg",commandLine->getIdentifier(1)) != 0) return 0;
    return 1;
  }

  if (commandLine->getCommandCount() == 3){
    if (strcmp("Command",commandLine->getIdentifier(0)) != 0) return 0;
    if (strcmp("Ret",commandLine->getIdentifier(1)) != 0) return 0;
    if (strcmp("Msg",commandLine->getIdentifier(2)) != 0) return 0;
    return 1;
  }
 
  return 0;
}
  
CommandLine* Parser::getCommandLine(){
  return commandLine;
}