From 3144bd7deb950de0cb80e2215c1545bdf8fc81e9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 6 Jun 2007 18:48:07 +0000 Subject: Except for storing the data somewhere, the TafReader is more or less done. Some more generalizations are in order, then we'll stuff the data somewhere. --- misc/taf | 4 +- src/exceptions.cpp | 1 + src/exceptions.h | 1 + src/tafreader.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/tafreader.h | 6 +++ src/tests/taf.cpp | 9 ++++ 6 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 src/tests/taf.cpp diff --git a/misc/taf b/misc/taf index 5ffcdcf..045b042 100644 --- a/misc/taf +++ b/misc/taf @@ -20,7 +20,7 @@ {: key="," value="yell"} {: key="li" value="lightning"} } - description = """They appear to be rather average looking, not particularly + description = "They appear to be rather average looking, not particularly tall or short, with facial features that are difficult to remember even - seconds after witnessing them.""" + seconds after witnessing them." } diff --git a/src/exceptions.cpp b/src/exceptions.cpp index d9f4e70..50b95c3 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -4,6 +4,7 @@ namespace Bu { subExceptionDef( XmlException ) + subExceptionDef( TafException ) subExceptionDef( FileException ) subExceptionDef( SocketException ) subExceptionDef( ConnectionException ) diff --git a/src/exceptions.h b/src/exceptions.h index f146b73..d5a9d39 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -7,6 +7,7 @@ namespace Bu { subExceptionDecl( XmlException ) + subExceptionDecl( TafException ) subExceptionDecl( FileException ) subExceptionDecl( SocketException ) subExceptionDecl( ConnectionException ) diff --git a/src/tafreader.cpp b/src/tafreader.cpp index 4d10b8d..91aa9f2 100644 --- a/src/tafreader.cpp +++ b/src/tafreader.cpp @@ -1,14 +1,141 @@ -#include "tafreader.h" +#include "bu/tafreader.h" +#include "bu/exceptions.h" +#include "bu/fstring.h" Bu::TafReader::TafReader( Bu::Stream &sIn ) : sIn( sIn ) { + next(); + node(); } Bu::TafReader::~TafReader() { } +void Bu::TafReader::node() +{ + ws(); + if( c != '{' ) + throw Bu::TafException("Expected '{'"); + next(); + ws(); + Bu::FString sName; + for(;;) + { + if( c == ':' ) + break; + else + sName += c; + next(); + } + next(); + printf("Node[%s]:\n", sName.getStr() ); + + nodeContent(); + + if( c != '}' ) + throw TafException("Expected '}'"); + + next(); +} + +void Bu::TafReader::nodeContent() +{ + for(;;) + { + ws(); + if( c == '{' ) + node(); + else if( c == '}' ) + return; + else + nodeProperty(); + } +} + +void Bu::TafReader::nodeProperty() +{ + Bu::FString sName; + for(;;) + { + if( isws() || c == '=' ) + break; + else + sName += c; + next(); + } + ws(); + if( c != '=' ) + { + printf(" %s (true)\n", sName.getStr() ); + return; + } + next(); + ws(); + Bu::FString sValue; + if( c == '"' ) + { + next(); + for(;;) + { + if( c == '\\' ) + { + next(); + if( c == 'x' ) + { + char code[3]={'\0','\0','\0'}; + next(); + code[0] = c; + next(); + code[1] = c; + c = (unsigned char)strtol( code, NULL, 16 ); + } + else if( c == '"' ) + c = '"'; + else + throw TafException("Invalid escape sequence."); + } + else if( c == '"' ) + break; + sValue += c; + next(); + } + next(); + } + else + { + for(;;) + { + if( isws() || c == '}' || c == '{' ) + break; + sValue += c; + next(); + } + } + printf(" %s = %s\n", sName.getStr(), sValue.getStr() ); +} + +FString Bu::TafReader::readStr() +{ +} + +void Bu::TafReader::ws() +{ + for(;;) + { + if( !isws() ) + return; + + next(); + } +} + +bool Bu::TafReader::isws() +{ + return (c == ' ' || c == '\t' || c == '\n' || c == '\r'); +} + void Bu::TafReader::next() { sIn.read( &c, 1 ); diff --git a/src/tafreader.h b/src/tafreader.h index b552f5d..127b571 100644 --- a/src/tafreader.h +++ b/src/tafreader.h @@ -17,7 +17,13 @@ namespace Bu virtual ~TafReader(); private: + void node(); + void nodeContent(); + void nodeProperty(); + void ws(); + bool isws(); void next(); + FString readStr(); char c; Stream &sIn; diff --git a/src/tests/taf.cpp b/src/tests/taf.cpp new file mode 100644 index 0000000..12c653e --- /dev/null +++ b/src/tests/taf.cpp @@ -0,0 +1,9 @@ +#include "bu/tafreader.h" +#include "bu/file.h" + +int main() +{ + Bu::File f("test.taf", "rb"); + Bu::TafReader tr( f ); +} + -- cgit v1.2.3