From 75cda24e0eb81f48649ad97283b657324082dd3b Mon Sep 17 00:00:00 2001 From: strange Date: Tue, 1 Dec 2009 15:53:38 -0700 Subject: [PATCH] Added GDB::StringFollower. StringFollower basically will replace all of the ugly-looking dynamic_cast<>s that are required to get data out of a String instance. --- src/interface/gdb/BreakpointObserver.cpp | 2 ++ src/interface/gdb/StringFollower.cpp | 53 ++++++++++++++++++++++++++++++++ src/interface/gdb/StringFollower.h | 47 ++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/interface/gdb/StringFollower.cpp create mode 100644 src/interface/gdb/StringFollower.h diff --git a/src/interface/gdb/BreakpointObserver.cpp b/src/interface/gdb/BreakpointObserver.cpp index 0fd3241..e0aafd9 100644 --- a/src/interface/gdb/BreakpointObserver.cpp +++ b/src/interface/gdb/BreakpointObserver.cpp @@ -1,3 +1,5 @@ +#include + #include "BreakpointObserver.h" namespace Aesalon { diff --git a/src/interface/gdb/StringFollower.cpp b/src/interface/gdb/StringFollower.cpp new file mode 100644 index 0000000..fda3703 --- /dev/null +++ b/src/interface/gdb/StringFollower.cpp @@ -0,0 +1,53 @@ +#include + +#include "StringFollower.h" + +namespace Aesalon { +namespace Interface { +namespace GDB { + +std::string StringFollower::follow(std::string path) { + std::stringstream stream; + stream << path; + + std::string token; + + while(stream >> token) { + if(token[0] == '[') { /* list element */ + if(token[token.length()-1] != ']') { + throw StringFollowerInvalidPathException("Expected terminating ']' at end of token."); + } + std::istringstream number_stream(token.substr(1, token.length()-2)); + int number; + number_stream >> number; + + data = data.to()->get_element(number); + } + else if(token[0] == '\'') { /* tuple element */ + if(token[token.length()-1] != '\'') { + throw StringFollowerInvalidPathException("Expected terminating \' at end of token."); + } + std::string key = token.substr(1, token.length()-2); + data = data.to()->get_element(key); + } + else if(token[0] == '\"') { /* tuple element */ + if(token[token.length()-1] != '\"') { + throw StringFollowerInvalidPathException("Expected terminating \" at end of token."); + } + std::string key = token.substr(1, token.length()-2); + data = data.to()->get_element(key); + } + else if(token == "lhs") { /* left side of a result */ + data = new ParseString(data.to()->get_name()); + } + else if(token == "rhs") { /* right side of a result */ + data = data.to()->get_value(); + } + } + + return data.to()->get_data(); +} + +} // namespace GDB +} // namespace Interface +} // namespace Aesalon diff --git a/src/interface/gdb/StringFollower.h b/src/interface/gdb/StringFollower.h new file mode 100644 index 0000000..a9f3889 --- /dev/null +++ b/src/interface/gdb/StringFollower.h @@ -0,0 +1,47 @@ +#ifndef AESALON_INTERFACE_GDB_STRING_FOLLOWER_H +#define AESALON_INTERFACE_GDB_STRING_FOLLOWER_H + +#include + +#include "misc/Exception.h" +#include "String.h" + +namespace Aesalon { +namespace Interface { +namespace GDB { + +class StringFollowerException : public Misc::Exception { +public: + StringFollowerException(std::string message) : Misc::Exception(message) {} + virtual ~StringFollowerException() {} +}; + +class StringFollowerInvalidPathException : public StringFollowerException { +public: + StringFollowerInvalidPathException(std::string message) : StringFollowerException(message) {} + virtual ~StringFollowerInvalidPathException() {} +}; + +/** Class to find an element within a String. */ +class StringFollower { +private: + Misc::SmartPointer data; +public: + StringFollower(Misc::SmartPointer string) : data(string->get_data()) {} + virtual ~StringFollower() {} + + /** Syntax used: + [n] nth item in ParseList + 'string' 'string' index in tuple + "string" "string" index in tuple + lhs Left side of a result (e.g. 'reason' in "reason=breakpoint-hit") + rhs Right side of a result (e.g. 'breakpoint-hit' in "reason=breakpoint-hit") + */ + virtual std::string follow(std::string path); +}; + +} // namespace GDB +} // namespace Interface +} // namespace Aesalon + +#endif -- 2.11.4.GIT