1#include "sniffer/CharStream.h"
2
3#include <sniffer/Err.h>
4
5using namespace BPrivate::Storage::Sniffer;
6
7//------------------------------------------------------------------------------
8// CharStream
9//------------------------------------------------------------------------------
10
11/*! \brief Creates a new, initialized character stream
12
13	\param string The character string to be streamed
14*/
15CharStream::CharStream(const std::string &string)
16	: fString(string)
17	, fPos(0)
18	, fCStatus(B_OK)
19{
20}
21
22/*! \brief Creates a new, unitialized character stream
23
24	Call SetTo() to initialize the stream.
25*/
26CharStream::CharStream()
27	: fString("")
28	, fPos(0)
29	, fCStatus(B_NO_INIT)
30{
31}
32
33/*! \brief Destroys the character stream
34*/
35CharStream::~CharStream() {
36	Unset();
37}
38
39/*! \brief Reinitializes the character stream to the given string
40
41	The stream position is reset to the beginning of the stream.
42
43	\param string The new character string to be streamed
44	\return Returns \c B_OK
45*/
46status_t
47CharStream::SetTo(const std::string &string) {
48	fString = string;
49	fPos = 0;
50	fCStatus = B_OK;
51	return fCStatus;
52}
53
54/*! \brief Unitializes the stream
55*/
56void
57CharStream::Unset() {
58	fString = "";
59	fPos = 0;
60	fCStatus = B_NO_INIT;
61}
62
63/*! \brief Returns the current status of the stream
64
65	\return
66	- \c B_OK: Ready and initialized
67	- \c B_NO_INIT: Unitialized
68*/
69status_t
70CharStream::InitCheck() const {
71	return fCStatus;
72}
73
74/*! \brief Returns \c true if there are no more characters in the stream.
75
76	If the stream is unitialized, \c true is also returned.
77*/
78bool
79CharStream::IsEmpty() const {
80	return fPos >= fString.length();
81}
82
83/*! \brief Returns the current offset of the stream into the original string.
84
85	If the stream is unitialized, zero is returned.
86*/
87size_t
88CharStream::Pos() const {
89	return fPos;
90}
91
92/*! \brief Returns the entire string being streamed.
93*/
94const std::string&
95CharStream::String() const {
96	return fString;
97}
98
99/*! Returns the next character in the stream.
100
101	Also increments the position in the stream. Call Unget() to
102	undo this operation.
103
104	Throws a BPrivate::Storage::Sniffer::Err exception if the stream is
105	unitialized. If the end of the stream has been reached, the position
106	marker is still incremented, but an end of text	char (\c 0x03) is
107	returned.
108*/
109char
110CharStream::Get() {
111	if (fCStatus != B_OK)
112		throw new Err("Sniffer parser error: CharStream::Get() called on uninitialized CharStream object", -1);
113	if (fPos < fString.length())
114		return fString[fPos++];
115	else {
116		fPos++;		// Increment fPos to keep Unget()s consistent
117		return 0x3;	// Return End-Of-Text char
118	}
119}
120
121/*! Shifts the stream position back one character.
122
123	Throws a BPrivate::Storage::Sniffer::Err exception if the stream is
124	unitialized or there are no more characters to unget.
125*/
126void
127CharStream::Unget() {
128	if (fCStatus != B_OK)
129		throw new Err("Sniffer parser error: CharStream::Unget() called on uninitialized CharStream object", -1);
130	if (fPos > 0)
131		fPos--;
132	else
133		throw new Err("Sniffer parser error: CharStream::Unget() called at beginning of character stream", -1);
134}
135
136