MNE-CPP  beta 1.0
commandparser.cpp
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "commandparser.h"
42 #include "commandmanager.h"
43 
44 //*************************************************************************************************************
45 //=============================================================================================================
46 // Qt INCLUDES
47 //=============================================================================================================
48 
49 #include <QDebug>
50 #include <QStringList>
51 #include <QJsonObject>
52 #include <QJsonDocument>
53 
54 
55 //*************************************************************************************************************
56 //=============================================================================================================
57 // USED NAMESPACES
58 //=============================================================================================================
59 
60 using namespace RTCOMMANDLIB;
61 
62 
63 //*************************************************************************************************************
64 //=============================================================================================================
65 // DEFINE MEMBER METHODS
66 //=============================================================================================================
67 
69 : QObject(parent)
70 {
71 }
72 
73 
74 //*************************************************************************************************************
75 
76 bool CommandParser::exists(const QString& p_sCommand)
77 {
78  Subject::t_Observers::Iterator itObservers;
79  for(itObservers = this->observers().begin(); itObservers != this->observers().end(); ++itObservers)
80  {
81  CommandManager* t_pCommandManager = static_cast<CommandManager*> (*itObservers);
82  if(t_pCommandManager->hasCommand(p_sCommand))
83  return true;
84  }
85  return false;
86 }
87 
88 
89 //*************************************************************************************************************
90 
91 bool CommandParser::parse(const QString &p_sInput, QStringList &p_qListCommandsParsed)
92 {
93  if(p_sInput.size() <= 0)
94  return false;
95 
96  p_qListCommandsParsed.clear();
97 
98  //Check if JSON format;
99  bool isJson = false;
100  if(QString::compare(p_sInput.at(0), QString("{")) == 0)
101  isJson = true;
102 
103  if(isJson)
104  {
105  qDebug() << "JSON command recognized";
106 
107  QJsonObject t_jsonObjectCommand;
108  QJsonObject t_jsonObjectParameters;
109  QJsonDocument t_jsonDocument(QJsonDocument::fromJson(p_sInput.toLatin1()));
110 
111  //Switch to command object
112  if(t_jsonDocument.isObject() && t_jsonDocument.object().value(QString("commands")) != QJsonValue::Undefined)
113  t_jsonObjectCommand = t_jsonDocument.object().value(QString("commands")).toObject();
114  else
115  return false;
116 
117  //iterate over commands
118  QJsonObject::Iterator it;
119  QJsonObject::Iterator itParam;
120  for(it = t_jsonObjectCommand.begin(); it != t_jsonObjectCommand.end(); ++it)
121  {
122  //Print Command
123  printf("%s\r\n", it.key().toLatin1().constData());
124 
125  if(exists(it.key()))
126  {
127  RawCommand t_rawCommand(it.key(), true);
128  m_rawCommand = t_rawCommand;
129  t_jsonObjectParameters = it.value().toObject();
130 
131  // push command to processed commands
132  p_qListCommandsParsed.push_back(it.key());
133 
134  //append the parameters
135  for(itParam= t_jsonObjectParameters.begin(); itParam != t_jsonObjectParameters.end(); ++itParam)
136  {
137  printf(" %s", itParam.value().toString().toLatin1().constData());
138  //ToDo do a cross check with the param naming and key
139  m_rawCommand.pValues().append(itParam.value().toString());
140 // qDebug() << itParam.key() << " + " << itParam.value().toString();
141  }
142 
143  //Notify attached command manager
144  notify();
145  }
146  printf("\r\n");
147  }
148  }
149  else
150  {
151  QStringList t_qCommandList = p_sInput.split(" ");
152 
153  //Print command
154  printf("%s\r\n", t_qCommandList[0].toLatin1().constData());
155 
156  if(!exists(t_qCommandList[0]))
157  {
158  printf("\r\n");
159  return false;
160  }
161 
162  RawCommand t_rawCommand(t_qCommandList[0], false);
163  m_rawCommand = t_rawCommand;
164 
165  // push command to processed commands
166  p_qListCommandsParsed.push_back(t_qCommandList[0]);
167 
168  if(t_qCommandList.size() > 1) //Parameter parsing
169  {
170  //Parse Parameters
171  for(qint32 i = 1; i < t_qCommandList.size(); ++i)
172  {
173  printf(" %s", t_qCommandList[i].toLatin1().constData());
174  m_rawCommand.pValues().append(t_qCommandList[i]);
175  }
176  }
177  printf("\r\n");
178  notify();
179  }
180 
181  return true;
182 }
CommandParser(QObject *parent=0)
t_Observers & observers()
Declaration of the CommandParser Class.
bool parse(const QString &p_sInput, QStringList &p_qListCommandsParsed)
QList< QString > & pValues()
Definition: rawcommand.h:193
void notify()
bool hasCommand(const QString &p_sCommand) const
bool exists(const QString &p_sCommand)