Storage Kit: Streamline CharStream and make it use BString.

This commit is contained in:
Augustin Cavalier
2026-04-07 17:57:19 -04:00
parent 104eaabf01
commit 2168280fac
12 changed files with 118 additions and 215 deletions
@@ -0,0 +1 @@
#include <../private/storage/CharStream.h>
@@ -1 +0,0 @@
#include <../../../private/storage/sniffer/CharStream.h>
+101
View File
@@ -0,0 +1,101 @@
/*
* Copyright 2002-2026, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _CHAR_STREAM_H
#define _CHAR_STREAM_H
#include <String.h>
namespace BPrivate {
//! Manages a stream of characters.
class CharStream {
public:
CharStream(const BString& source)
:
fString(source),
fPos(0)
{
}
inline char Get();
inline void Unget();
inline status_t SetTo(const BString& source);
inline bool IsEmpty() const;
inline int32 Pos() const { return fPos; }
private:
BString fString;
int32 fPos;
};
/*! Returns the next character in the stream.
Also increments the position in the stream. Call Unget() to
undo this operation.
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 (fPos < fString.Length())
return fString[fPos++];
fPos++;
return 0x3;
}
/*! Shifts the stream position back one character. */
void
CharStream::Unget()
{
if (fPos > 0) {
fPos--;
return;
}
throw (status_t)B_BUFFER_OVERFLOW;
}
/*! \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 BString& string)
{
fString = string;
fPos = 0;
return B_OK;
}
/*! \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();
}
} // namespace BPrivate
#endif // _CHAR_STREAM_H
@@ -1,56 +0,0 @@
//----------------------------------------------------------------------
// This software is part of the Haiku distribution and is covered
// by the MIT License.
//---------------------------------------------------------------------
/*!
\file sniffer/CharStream.h
Character stream class
*/
#ifndef _SNIFFER_CHAR_STREAM_H
#define _SNIFFER_CHAR_STREAM_H
#include <SupportDefs.h>
#include <string>
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
-1
View File
@@ -54,7 +54,6 @@ BuildPlatformMergeObjectPIC <libbe_build>storage_kit.o :
TextSnifferAddon.cpp TextSnifferAddon.cpp
# sniffer # sniffer
CharStream.cpp
Err.cpp Err.cpp
DisjList.cpp DisjList.cpp
Pattern.cpp Pattern.cpp
-1
View File
@@ -57,7 +57,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
storage_support.cpp storage_support.cpp
# sniffer # sniffer
CharStream.cpp
Err.cpp Err.cpp
DisjList.cpp DisjList.cpp
Pattern.cpp Pattern.cpp
-134
View File
@@ -1,134 +0,0 @@
#include <sniffer/CharStream.h>
#include "Err.h"
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);
}
+5 -6
View File
@@ -81,7 +81,8 @@ Token::Type() const {
} }
const std::string& const std::string&
Token::String() const { Token::String() const
{
throw new Err("Sniffer scanner error: Token::String() called on non-string token", fPos); throw new Err("Sniffer scanner error: Token::String() called on non-string token", fPos);
} }
@@ -212,7 +213,7 @@ FloatToken::Float() const {
// TokenStream // TokenStream
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
TokenStream::TokenStream(const std::string &string) TokenStream::TokenStream(const BString &string)
: fCStatus(B_NO_INIT) : fCStatus(B_NO_INIT)
, fPos(-1) , fPos(-1)
, fStrLen(-1) , fStrLen(-1)
@@ -232,12 +233,10 @@ TokenStream::~TokenStream() {
} }
status_t status_t
TokenStream::SetTo(const std::string &string) { TokenStream::SetTo(const BString& string) {
Unset(); Unset();
fStrLen = string.length(); fStrLen = string.Length();
CharStream stream(string); CharStream stream(string);
if (stream.InitCheck() != B_OK)
throw new Err("Sniffer scanner error: Unable to intialize character stream", -1);
typedef enum TokenStreamScannerState { typedef enum TokenStreamScannerState {
tsssStart, tsssStart,
+3 -3
View File
@@ -10,7 +10,7 @@
#define _SNIFFER_PARSER_H #define _SNIFFER_PARSER_H
#include <SupportDefs.h> #include <SupportDefs.h>
#include <sniffer/CharStream.h> #include <CharStream.h>
#include <List.h> #include <List.h>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -135,11 +135,11 @@ protected:
*/ */
class TokenStream { class TokenStream {
public: public:
TokenStream(const std::string &string); TokenStream(const BString &string);
TokenStream(); TokenStream();
~TokenStream(); ~TokenStream();
status_t SetTo(const std::string &string); status_t SetTo(const BString &string);
void Unset(); void Unset();
status_t InitCheck() const; status_t InitCheck() const;
@@ -10,7 +10,6 @@
#include "RosterSettingsCharStream.h" #include "RosterSettingsCharStream.h"
#include <StorageDefs.h> #include <StorageDefs.h>
#include <stdio.h> #include <stdio.h>
#include "Debug.h" #include "Debug.h"
@@ -24,19 +23,17 @@ const status_t RosterSettingsCharStream::kComment;
const status_t RosterSettingsCharStream::kUnexpectedState; const status_t RosterSettingsCharStream::kUnexpectedState;
const status_t RosterSettingsCharStream::kStringTooLong; const status_t RosterSettingsCharStream::kStringTooLong;
using namespace BPrivate::Storage::Sniffer;
RosterSettingsCharStream::RosterSettingsCharStream(const BString& string)
RosterSettingsCharStream::RosterSettingsCharStream(const std::string &string)
: :
CharStream(string) BPrivate::CharStream(string)
{ {
} }
RosterSettingsCharStream::RosterSettingsCharStream() RosterSettingsCharStream::RosterSettingsCharStream()
: :
CharStream() BPrivate::CharStream(BString())
{ {
} }
@@ -85,9 +82,7 @@ status_t
RosterSettingsCharStream::GetString(char *result) RosterSettingsCharStream::GetString(char *result)
{ {
status_t error = result ? B_OK : B_BAD_VALUE; status_t error = result ? B_OK : B_BAD_VALUE;
if (!error) if (error != B_OK)
error = InitCheck();
if (error)
return error; return error;
enum RosterSettingsScannerState { enum RosterSettingsScannerState {
@@ -30,7 +30,7 @@
#ifndef _ROSTER_SETTINGS_CHAR_STREAM_H #ifndef _ROSTER_SETTINGS_CHAR_STREAM_H
#define _ROSTER_SETTINGS_CHAR_STREAM_H #define _ROSTER_SETTINGS_CHAR_STREAM_H
#include <sniffer/CharStream.h> #include <CharStream.h>
#include <SupportDefs.h> #include <SupportDefs.h>
#include <string> #include <string>
@@ -50,9 +50,9 @@
used when a valid string containing invalid data is discovered in the middle used when a valid string containing invalid data is discovered in the middle
of a line. of a line.
*/ */
class RosterSettingsCharStream : public BPrivate::Storage::Sniffer::CharStream { class RosterSettingsCharStream : public BPrivate::CharStream {
public: public:
RosterSettingsCharStream(const std::string &string); RosterSettingsCharStream(const BString& string);
RosterSettingsCharStream(); RosterSettingsCharStream();
virtual ~RosterSettingsCharStream(); virtual ~RosterSettingsCharStream();
+1 -1
View File
@@ -1885,7 +1885,7 @@ TRoster::_LoadRosterSettings(const char* path)
} }
if (!error) { if (!error) {
data[size] = 0; data[size] = 0;
error = stream.SetTo(std::string(data)); error = stream.SetTo(BString(data));
} }
delete[] data; delete[] data;