diff --git a/headers/private/storage/sniffer/CharStream.h b/headers/private/storage/sniffer/CharStream.h new file mode 100644 index 0000000000..124901d4f5 --- /dev/null +++ b/headers/private/storage/sniffer/CharStream.h @@ -0,0 +1,56 @@ +//---------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +//--------------------------------------------------------------------- +/*! + \file sniffer/CharStream.h + Character stream class +*/ +#ifndef _SNIFFER_CHAR_STREAM_H +#define _SNIFFER_CHAR_STREAM_H + +#include + +#include + +namespace BPrivate { +namespace Storage { +namespace Sniffer { + +//! Manages a stream of characters +/*! CharStream is used by the scanner portion of the parser, which is implemented + in TokenStream::SetTo(). + + It's also used by BPrivate::TRoster while parsing through the the + roster's RosterSettings file. +*/ +class CharStream { +public: + CharStream(const std::string &string); + CharStream(); + virtual ~CharStream(); + + status_t SetTo(const std::string &string); + void Unset(); + status_t InitCheck() const; + bool IsEmpty() const; + size_t Pos() const; + const std::string& String() const; + + char Get(); + void Unget(); + +private: + std::string fString; + size_t fPos; + status_t fCStatus; + + CharStream(const CharStream &ref); + CharStream& operator=(const CharStream &ref); +}; + +}; // namespace Sniffer +}; // namespace Storage +}; // namespace BPrivate + +#endif // _SNIFFER_CHAR_STREAM_H diff --git a/headers/private/storage/sniffer/Parser.h b/headers/private/storage/sniffer/Parser.h index 9fd8b6d9cf..2e2e7d86a4 100644 --- a/headers/private/storage/sniffer/Parser.h +++ b/headers/private/storage/sniffer/Parser.h @@ -10,6 +10,7 @@ #define _SNIFFER_PARSER_H #include +#include #include #include #include @@ -39,35 +40,6 @@ status_t parse(const char *rule, Rule *result, BString *parseError = NULL); // Classes used internally by the parser //------------------------------------------------------------------------------ -//! Manages a stream of characters -/*! CharStream is used by the scanner portion of the parser, which is implemented - in TokenStream::SetTo(). -*/ -class CharStream { -public: - CharStream(const std::string &string); - CharStream(); - ~CharStream(); - - status_t SetTo(const std::string &string); - void Unset(); - status_t InitCheck() const; - bool IsEmpty() const; - size_t Pos() const; - const std::string& String() const; - - char Get(); - void Unget(); - -private: - std::string fString; - size_t fPos; - status_t fCStatus; - - CharStream(const CharStream &ref); - CharStream& operator=(const CharStream &ref); -}; - //! Types of tokens typedef enum TokenType { EmptyToken, diff --git a/src/kits/storage/sniffer/CharStream.cpp b/src/kits/storage/sniffer/CharStream.cpp new file mode 100644 index 0000000000..eb916fc8d6 --- /dev/null +++ b/src/kits/storage/sniffer/CharStream.cpp @@ -0,0 +1,135 @@ +#include "sniffer/CharStream.h" + +#include + +using namespace BPrivate::Storage::Sniffer; + +//------------------------------------------------------------------------------ +// CharStream +//------------------------------------------------------------------------------ + +/*! \brief Creates a new, initialized character stream + + \param string The character string to be streamed +*/ +CharStream::CharStream(const std::string &string) + : fString(string) + , fPos(0) + , fCStatus(B_OK) +{ +} + +/*! \brief Creates a new, unitialized character stream + + Call SetTo() to initialize the stream. +*/ +CharStream::CharStream() + : fString("") + , fPos(0) + , fCStatus(B_NO_INIT) +{ +} + +/*! \brief Destroys the character stream +*/ +CharStream::~CharStream() { + Unset(); +} + +/*! \brief Reinitializes the character stream to the given string + + The stream position is reset to the beginning of the stream. + + \param string The new character string to be streamed + \return Returns \c B_OK +*/ +status_t +CharStream::SetTo(const std::string &string) { + fString = string; + fPos = 0; + fCStatus = B_OK; + return fCStatus; +} + +/*! \brief Unitializes the stream +*/ +void +CharStream::Unset() { + fString = ""; + fPos = 0; + fCStatus = B_NO_INIT; +} + +/*! \brief Returns the current status of the stream + + \return + - \c B_OK: Ready and initialized + - \c B_NO_INIT: Unitialized +*/ +status_t +CharStream::InitCheck() const { + return fCStatus; +} + +/*! \brief Returns \c true if there are no more characters in the stream. + + If the stream is unitialized, \c true is also returned. +*/ +bool +CharStream::IsEmpty() const { + return fPos >= fString.length(); +} + +/*! \brief Returns the current offset of the stream into the original string. + + If the stream is unitialized, zero is returned. +*/ +size_t +CharStream::Pos() const { + return fPos; +} + +/*! \brief Returns the entire string being streamed. +*/ +const std::string& +CharStream::String() const { + return fString; +} + +/*! Returns the next character in the stream. + + Also increments the position in the stream. Call Unget() to + undo this operation. + + Throws a BPrivate::Storage::Sniffer::Err exception if the stream is + unitialized. If the end of the stream has been reached, the position + marker is still incremented, but an end of text char (\c 0x03) is + returned. +*/ +char +CharStream::Get() { + if (fCStatus != B_OK) + throw new Err("Sniffer parser error: CharStream::Get() called on uninitialized CharStream object", -1); + if (fPos < fString.length()) + return fString[fPos++]; + else { + fPos++; // Increment fPos to keep Unget()s consistent + return 0x3; // Return End-Of-Text char + } +} + +/*! Shifts the stream position back one character. + + Throws a BPrivate::Storage::Sniffer::Err exception if the stream is + unitialized or there are no more characters to unget. +*/ +void +CharStream::Unget() { + if (fCStatus != B_OK) + throw new Err("Sniffer parser error: CharStream::Unget() called on uninitialized CharStream object", -1); + if (fPos > 0) + fPos--; + else + throw new Err("Sniffer parser error: CharStream::Unget() called at beginning of character stream", -1); +} + diff --git a/src/kits/storage/sniffer/Parser.cpp b/src/kits/storage/sniffer/Parser.cpp index aad813ba51..6370a6d6f5 100644 --- a/src/kits/storage/sniffer/Parser.cpp +++ b/src/kits/storage/sniffer/Parser.cpp @@ -60,85 +60,6 @@ BPrivate::Storage::Sniffer::parse(const char *rule, Rule *result, BString *parse return parser.Parse(rule, result, parseError); } -//------------------------------------------------------------------------------ -// CharStream -//------------------------------------------------------------------------------ - -CharStream::CharStream(const std::string &string) - : fString(string) - , fPos(0) - , fCStatus(B_OK) -{ -} - -CharStream::CharStream() - : fString("") - , fPos(0) - , fCStatus(B_NO_INIT) -{ -} - -CharStream::~CharStream() { - Unset(); -} - -status_t -CharStream::SetTo(const std::string &string) { - fString = string; - fPos = 0; - fCStatus = B_OK; - return fCStatus; -} - -void -CharStream::Unset() { - fString = ""; - fPos = 0; - fCStatus = B_NO_INIT; -} - -status_t -CharStream::InitCheck() const { - return fCStatus; -} - -bool -CharStream::IsEmpty() const { - return fPos >= fString.length(); -} - -size_t -CharStream::Pos() const { - return fPos; -} - -const std::string& -CharStream::String() const { - return fString; -} - -char -CharStream::Get() { - if (fCStatus != B_OK) - throw new Err("Sniffer parser error: CharStream::Get() called on uninitialized CharStream object", -1); - if (fPos < fString.length()) - return fString[fPos++]; - else { - fPos++; // Increment fPos to keep Unget()s consistent - return 0x3; // Return End-Of-Text char - } -} - -void -CharStream::Unget() { - if (fCStatus != B_OK) - throw new Err("Sniffer parser error: CharStream::Unget() called on uninitialized CharStream object", -1); - if (fPos > 0) - fPos--; - else - throw new Err("Sniffer parser error: CharStream::Unget() called at beginning of character stream", -1); -} - //------------------------------------------------------------------------------ // Token //------------------------------------------------------------------------------