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