189ec8a81STyler Dauwalder#include "sniffer/CharStream.h"
289ec8a81STyler Dauwalder
389ec8a81STyler Dauwalder#include <sniffer/Err.h>
489ec8a81STyler Dauwalder
589ec8a81STyler Dauwalderusing namespace BPrivate::Storage::Sniffer;
689ec8a81STyler Dauwalder
789ec8a81STyler Dauwalder//------------------------------------------------------------------------------
889ec8a81STyler Dauwalder// CharStream
989ec8a81STyler Dauwalder//------------------------------------------------------------------------------
1089ec8a81STyler Dauwalder
1189ec8a81STyler Dauwalder/*! \brief Creates a new, initialized character stream
1289ec8a81STyler Dauwalder
1389ec8a81STyler Dauwalder	\param string The character string to be streamed
1489ec8a81STyler Dauwalder*/
1589ec8a81STyler DauwalderCharStream::CharStream(const std::string &string)
1689ec8a81STyler Dauwalder	: fString(string)
1789ec8a81STyler Dauwalder	, fPos(0)
1889ec8a81STyler Dauwalder	, fCStatus(B_OK)
1989ec8a81STyler Dauwalder{
2089ec8a81STyler Dauwalder}
2189ec8a81STyler Dauwalder
2289ec8a81STyler Dauwalder/*! \brief Creates a new, unitialized character stream
2389ec8a81STyler Dauwalder
2489ec8a81STyler Dauwalder	Call SetTo() to initialize the stream.
2589ec8a81STyler Dauwalder*/
2689ec8a81STyler DauwalderCharStream::CharStream()
2789ec8a81STyler Dauwalder	: fString("")
2889ec8a81STyler Dauwalder	, fPos(0)
2989ec8a81STyler Dauwalder	, fCStatus(B_NO_INIT)
3089ec8a81STyler Dauwalder{
3189ec8a81STyler Dauwalder}
3289ec8a81STyler Dauwalder
3389ec8a81STyler Dauwalder/*! \brief Destroys the character stream
3489ec8a81STyler Dauwalder*/
3589ec8a81STyler DauwalderCharStream::~CharStream() {
3689ec8a81STyler Dauwalder	Unset();
3789ec8a81STyler Dauwalder}
3889ec8a81STyler Dauwalder
3989ec8a81STyler Dauwalder/*! \brief Reinitializes the character stream to the given string
4089ec8a81STyler Dauwalder
4189ec8a81STyler Dauwalder	The stream position is reset to the beginning of the stream.
4289ec8a81STyler Dauwalder
4389ec8a81STyler Dauwalder	\param string The new character string to be streamed
4489ec8a81STyler Dauwalder	\return Returns \c B_OK
4589ec8a81STyler Dauwalder*/
4689ec8a81STyler Dauwalderstatus_t
4789ec8a81STyler DauwalderCharStream::SetTo(const std::string &string) {
4889ec8a81STyler Dauwalder	fString = string;
4989ec8a81STyler Dauwalder	fPos = 0;
5089ec8a81STyler Dauwalder	fCStatus = B_OK;
5189ec8a81STyler Dauwalder	return fCStatus;
5289ec8a81STyler Dauwalder}
5389ec8a81STyler Dauwalder
5489ec8a81STyler Dauwalder/*! \brief Unitializes the stream
5589ec8a81STyler Dauwalder*/
5689ec8a81STyler Dauwaldervoid
5789ec8a81STyler DauwalderCharStream::Unset() {
5889ec8a81STyler Dauwalder	fString = "";
5989ec8a81STyler Dauwalder	fPos = 0;
6089ec8a81STyler Dauwalder	fCStatus = B_NO_INIT;
6189ec8a81STyler Dauwalder}
6289ec8a81STyler Dauwalder
6389ec8a81STyler Dauwalder/*! \brief Returns the current status of the stream
6489ec8a81STyler Dauwalder
6589ec8a81STyler Dauwalder	\return
6689ec8a81STyler Dauwalder	- \c B_OK: Ready and initialized
6789ec8a81STyler Dauwalder	- \c B_NO_INIT: Unitialized
6889ec8a81STyler Dauwalder*/
6989ec8a81STyler Dauwalderstatus_t
7089ec8a81STyler DauwalderCharStream::InitCheck() const {
7189ec8a81STyler Dauwalder	return fCStatus;
7289ec8a81STyler Dauwalder}
7389ec8a81STyler Dauwalder
7489ec8a81STyler Dauwalder/*! \brief Returns \c true if there are no more characters in the stream.
7589ec8a81STyler Dauwalder
7689ec8a81STyler Dauwalder	If the stream is unitialized, \c true is also returned.
7789ec8a81STyler Dauwalder*/
7889ec8a81STyler Dauwalderbool
7989ec8a81STyler DauwalderCharStream::IsEmpty() const {
8089ec8a81STyler Dauwalder	return fPos >= fString.length();
8189ec8a81STyler Dauwalder}
8289ec8a81STyler Dauwalder
8389ec8a81STyler Dauwalder/*! \brief Returns the current offset of the stream into the original string.
8489ec8a81STyler Dauwalder
8589ec8a81STyler Dauwalder	If the stream is unitialized, zero is returned.
8689ec8a81STyler Dauwalder*/
8789ec8a81STyler Dauwaldersize_t
8889ec8a81STyler DauwalderCharStream::Pos() const {
8989ec8a81STyler Dauwalder	return fPos;
9089ec8a81STyler Dauwalder}
9189ec8a81STyler Dauwalder
9289ec8a81STyler Dauwalder/*! \brief Returns the entire string being streamed.
9389ec8a81STyler Dauwalder*/
9489ec8a81STyler Dauwalderconst std::string&
9589ec8a81STyler DauwalderCharStream::String() const {
9689ec8a81STyler Dauwalder	return fString;
9789ec8a81STyler Dauwalder}
9889ec8a81STyler Dauwalder
9989ec8a81STyler Dauwalder/*! Returns the next character in the stream.
10089ec8a81STyler Dauwalder
10189ec8a81STyler Dauwalder	Also increments the position in the stream. Call Unget() to
10289ec8a81STyler Dauwalder	undo this operation.
10389ec8a81STyler Dauwalder
10489ec8a81STyler Dauwalder	Throws a BPrivate::Storage::Sniffer::Err exception if the stream is
10589ec8a81STyler Dauwalder	unitialized. If the end of the stream has been reached, the position
10689ec8a81STyler Dauwalder	marker is still incremented, but an end of text	char (\c 0x03) is
10789ec8a81STyler Dauwalder	returned.
10889ec8a81STyler Dauwalder*/
10989ec8a81STyler Dauwalderchar
11089ec8a81STyler DauwalderCharStream::Get() {
11189ec8a81STyler Dauwalder	if (fCStatus != B_OK)
11289ec8a81STyler Dauwalder		throw new Err("Sniffer parser error: CharStream::Get() called on uninitialized CharStream object", -1);
11389ec8a81STyler Dauwalder	if (fPos < fString.length())
11489ec8a81STyler Dauwalder		return fString[fPos++];
11589ec8a81STyler Dauwalder	else {
11689ec8a81STyler Dauwalder		fPos++;		// Increment fPos to keep Unget()s consistent
11789ec8a81STyler Dauwalder		return 0x3;	// Return End-Of-Text char
11889ec8a81STyler Dauwalder	}
11989ec8a81STyler Dauwalder}
12089ec8a81STyler Dauwalder
12189ec8a81STyler Dauwalder/*! Shifts the stream position back one character.
12289ec8a81STyler Dauwalder
12389ec8a81STyler Dauwalder	Throws a BPrivate::Storage::Sniffer::Err exception if the stream is
12489ec8a81STyler Dauwalder	unitialized or there are no more characters to unget.
12589ec8a81STyler Dauwalder*/
12689ec8a81STyler Dauwaldervoid
12789ec8a81STyler DauwalderCharStream::Unget() {
12889ec8a81STyler Dauwalder	if (fCStatus != B_OK)
12989ec8a81STyler Dauwalder		throw new Err("Sniffer parser error: CharStream::Unget() called on uninitialized CharStream object", -1);
13089ec8a81STyler Dauwalder	if (fPos > 0)
13189ec8a81STyler Dauwalder		fPos--;
13289ec8a81STyler Dauwalder	else
13389ec8a81STyler Dauwalder		throw new Err("Sniffer parser error: CharStream::Unget() called at beginning of character stream", -1);
13489ec8a81STyler Dauwalder}
13589ec8a81STyler Dauwalder