work save

This commit is contained in:
royqh1979@gmail.com 2021-11-13 11:16:05 +08:00
parent c7748bf5b2
commit 3668378d7f
2 changed files with 30 additions and 30 deletions

View File

@ -2,8 +2,6 @@
#include <QList> #include <QList>
static GDBMIResultParser::ParseValue EMPTY_PARSE_VALUE;
GDBMIResultParser::GDBMIResultParser() GDBMIResultParser::GDBMIResultParser()
{ {
mResultTypes.insert("bkpt",GDBMIResultType::Breakpoint); mResultTypes.insert("bkpt",GDBMIResultType::Breakpoint);
@ -33,22 +31,23 @@ bool GDBMIResultParser::parse(const QByteArray &record, GDBMIResultType &type, P
return true; return true;
} }
bool GDBMIResultParser::parseNameAndValue(char *&p, QByteArray &name, ParseValue &value) bool GDBMIResultParser::parseNameAndValue(const char *&p, QByteArray &name, ParseValue &value)
{ {
skipSpaces(p); skipSpaces(p);
char* nameStart =p; const char* nameStart =p;
while (*p!=0 && isNameChar(*p)) { while (*p!=0 && isNameChar(*p)) {
p++; p++;
} }
if (*p==0) if (*p==0)
return false; return false;
name = QByteArray(nameStart,p-nameStart);
skipSpaces(p); skipSpaces(p);
if (*p!='=') if (*p!='=')
return false; return false;
return parseValue(p,value); return parseValue(p,value);
} }
bool GDBMIResultParser::parseValue(char *&p, ParseValue &value) bool GDBMIResultParser::parseValue(const char *&p, ParseValue &value)
{ {
skipSpaces(p); skipSpaces(p);
bool result; bool result;
@ -82,12 +81,12 @@ bool GDBMIResultParser::parseValue(char *&p, ParseValue &value)
return true; return true;
} }
bool GDBMIResultParser::parseStringValue(char *&p, QByteArray& stringValue) bool GDBMIResultParser::parseStringValue(const char *&p, QByteArray& stringValue)
{ {
if (*p!='"') if (*p!='"')
return false; return false;
p++; p++;
char* valueStart = p; const char* valueStart = p;
while (*p!=0) { while (*p!=0) {
if (*p == '"') { if (*p == '"') {
break; break;
@ -105,7 +104,7 @@ bool GDBMIResultParser::parseStringValue(char *&p, QByteArray& stringValue)
return false; return false;
} }
bool GDBMIResultParser::parseObject(char *&p, ParseObject &obj) bool GDBMIResultParser::parseObject(const char *&p, ParseObject &obj)
{ {
if (*p!='{') if (*p!='{')
return false; return false;
@ -137,12 +136,11 @@ bool GDBMIResultParser::parseObject(char *&p, ParseObject &obj)
return false; return false;
} }
bool GDBMIResultParser::parseArray(char *&p, ParseValue &value) bool GDBMIResultParser::parseArray(const char *&p, QList<GDBMIResultParser::ParseObject> &array)
{ {
if (*p!='[') if (*p!='[')
return false; return false;
p++; p++;
QList<ParseObject> array;
if (*p!=']') { if (*p!=']') {
while (*p!=0) { while (*p!=0) {
skipSpaces(p); skipSpaces(p);
@ -163,7 +161,6 @@ bool GDBMIResultParser::parseArray(char *&p, ParseValue &value)
} }
} }
if (*p==']') { if (*p==']') {
value = array;
p++; //skip ']' p++; //skip ']'
return true; return true;
} }
@ -191,7 +188,7 @@ bool GDBMIResultParser::isSpaceChar(char ch)
return false; return false;
} }
void GDBMIResultParser::skipSpaces(char *&p) void GDBMIResultParser::skipSpaces(const char *&p)
{ {
while (*p!=0 && isSpaceChar(*p)) while (*p!=0 && isSpaceChar(*p))
p++; p++;
@ -262,11 +259,11 @@ GDBMIResultParser::ParseValue &GDBMIResultParser::ParseValue::operator=(const QL
} }
const GDBMIResultParser::ParseValue &GDBMIResultParser::ParseObject::operator[](const QByteArray &name) const const GDBMIResultParser::ParseValue GDBMIResultParser::ParseObject::operator[](const QByteArray &name) const
{ {
if (mProps.contains(name)) if (mProps.contains(name))
return mProps[name]; return mProps[name];
return EMPTY_PARSE_VALUE; return ParseValue();
} }
GDBMIResultParser::ParseObject &GDBMIResultParser::ParseObject::operator=(const ParseObject &object) GDBMIResultParser::ParseObject &GDBMIResultParser::ParseObject::operator=(const ParseObject &object)

View File

@ -2,8 +2,9 @@
#define GDBMIRESULTPARSER_H #define GDBMIRESULTPARSER_H
#include <QByteArray> #include <QByteArray>
#include <QHash>
#include <QList> #include <QList>
#include <QVector> #include <memory>
enum class GDBMIResultType { enum class GDBMIResultType {
@ -23,6 +24,7 @@ enum class GDBMIResultType {
class GDBMIResultParser class GDBMIResultParser
{ {
public:
enum class ParseValueType { enum class ParseValueType {
Value, Value,
Object, Object,
@ -30,7 +32,16 @@ class GDBMIResultParser
NotAssigned NotAssigned
}; };
class ParseObject; class ParseValue;
class ParseObject {
public:
const ParseValue operator[](const QByteArray& name) const;
ParseValue& operator[](const QByteArray& name);
ParseObject& operator=(const ParseObject& object);
private:
QHash<QByteArray, ParseValue> mProps;
};
class ParseValue { class ParseValue {
public: public:
@ -56,24 +67,16 @@ class GDBMIResultParser
using PParseValue = std::shared_ptr<ParseValue>; using PParseValue = std::shared_ptr<ParseValue>;
class ParseObject {
const ParseValue& operator[](const QByteArray& name) const;
ParseValue& operator[](const QByteArray& name);
ParseObject& operator=(const ParseObject& object);
private:
QHash<QByteArray, ParseValue> mProps;
};
public: public:
GDBMIResultParser(); GDBMIResultParser();
bool parse(const QByteArray& record, GDBMIResultType& type, ParseValue& value); bool parse(const QByteArray& record, GDBMIResultType& type, ParseValue& value);
private: private:
bool parseNameAndValue(char* &p,QByteArray& name, ParseValue& value); bool parseNameAndValue(const char *&p,QByteArray& name, ParseValue& value);
bool parseValue(char* &p, ParseValue& value); bool parseValue(const char* &p, ParseValue& value);
bool parseStringValue(char*&p, QByteArray& stringValue); bool parseStringValue(const char*&p, QByteArray& stringValue);
bool parseObject(char*&p, ParseObject& obj); bool parseObject(const char*&p, ParseObject& obj);
bool parseArray(char*&p, QList<ParseObject>& array); bool parseArray(const char*&p, QList<ParseObject>& array);
void skipSpaces(char* &p); void skipSpaces(const char* &p);
bool isNameChar(char ch); bool isNameChar(char ch);
bool isSpaceChar(char ch); bool isSpaceChar(char ch);
private: private: