CoreUtils/ZString.h

269 lines
5.5 KiB
C
Raw Normal View History

2021-07-23 11:44:36 -07:00
#ifndef __ZString_h__
#define __ZString_h__
2022-01-24 14:03:58 -08:00
#include <string.h>
#include <string>
#include <ostream>
#include <vector>
2021-07-23 11:44:36 -07:00
namespace coreutils {
///
/// Use the ZString object when advanced parsing algorithms are required to simplify
2022-01-24 14:03:58 -08:00
/// parsing. ZString is not a backing store and requires that any referenced buffer
2021-07-23 11:44:36 -07:00
/// or objects are retained throughout the life of the object. Additionally, ZString
/// is non-destructive to its underlying references.
///
/// ZString provides several methods to reference and parse the content. First method
2022-01-24 14:03:58 -08:00
/// is split(ZString delimiter) which will return a vector of ZString which contains
2021-07-23 11:44:36 -07:00
/// references to the parts of the string split by the provided delimiter. The delimiter
/// portion is omitted from the results.
///
2022-01-24 14:03:58 -08:00
/// Second method involves a cursor which is a pointer to data contained within the
2021-07-23 11:44:36 -07:00
/// ZString and is used to point to the current parsing point. Several methods are
2022-01-24 14:03:58 -08:00
/// provided to advance through the parsing based upon content and rule checks.
2021-07-23 11:44:36 -07:00
///
class ZString {
public:
///
2022-03-15 20:07:43 -07:00
/// Consructor providing an empty ZString.
2021-07-23 11:44:36 -07:00
///
2021-08-11 21:37:48 -07:00
ZString();
2021-07-23 11:44:36 -07:00
///
///
///
2022-01-24 14:03:58 -08:00
2021-07-23 11:44:36 -07:00
ZString(const char *data);
2022-01-24 14:03:58 -08:00
2021-07-23 11:44:36 -07:00
///
/// Consructor providing the initial setup of the ZString object.
///
ZString(char *data, size_t length);
2021-08-11 21:37:48 -07:00
ZString(const char *data, size_t length);
2021-07-23 11:44:36 -07:00
///
/// Consructor providing a copy of a ZString.
///
2021-08-11 21:37:48 -07:00
ZString(const ZString &zstring);
2021-07-23 11:44:36 -07:00
2022-01-24 14:03:58 -08:00
///
/// Consructor from a string.
///
ZString(std::string string);
2021-07-23 11:44:36 -07:00
///
/// A friend method used to write the value of the ZString from the cursor
/// point to an ostream using the << syntax.
///
friend std::ostream& operator<<(std::ostream& os, const ZString& zstring);
2021-07-31 11:15:07 -07:00
friend std::ostream& operator<<(std::ostream& os, const std::string& string);
friend std::ostream& operator+(std::ostream& os, const ZString& zstring);
2022-01-24 14:03:58 -08:00
bool operator<(const ZString &valuex) const;
bool operator>(const ZString &valuex) const;
bool operator==(const ZString &valuex) const;
2021-07-23 11:44:36 -07:00
///
/// Return a vector of ZString objects which contain the parts established
2021-08-07 11:47:01 -07:00
/// by using the split method and passing a delimiter value.
2021-07-23 11:44:36 -07:00
///
std::vector<ZString> & getList();
///
2021-08-07 11:47:01 -07:00
/// Return the integer value of the ZString from the cursor to the first
/// non-numeric character.
2021-07-23 11:44:36 -07:00
///
2021-08-07 11:47:01 -07:00
2021-07-23 11:44:36 -07:00
int asInteger();
2021-08-07 11:47:01 -07:00
2021-07-23 11:44:36 -07:00
///
/// Return the value of the ZString from the cursor to the end.
///
std::string str();
///
/// Return the current value of the ZString as a string of
/// specified length.
///
std::string str(int len);
///
///
///
2021-08-11 21:37:48 -07:00
std::vector<ZString> &split(ZString &delimiter, size_t maxSize = 0);
2021-07-23 11:44:36 -07:00
2021-07-31 11:15:07 -07:00
///
///
///
2021-08-11 21:37:48 -07:00
std::vector<ZString> &split(std::string delimiter, size_t maxSize = 0);
2021-07-31 11:15:07 -07:00
2021-07-23 11:44:36 -07:00
///
/// Use getTokenInclude with a list of character to allow the forwarding
2022-01-24 14:03:58 -08:00
/// of the cursor through the data until a character that is not in the
/// include string is not encountered in the ZString data.
2021-07-23 11:44:36 -07:00
///
2021-08-11 21:37:48 -07:00
ZString getTokenInclude(const char *include);
2021-07-23 11:44:36 -07:00
///
///
///
2021-08-11 21:37:48 -07:00
ZString getTokenExclude(const char *exclude);
ZString getTokenExclude(std::string exclude);
2021-07-23 11:44:36 -07:00
///
/// Use the [] operator to retrieve values delimited by the split method.
///
2021-08-11 21:37:48 -07:00
ZString &operator[](int index);
2021-07-23 11:44:36 -07:00
///
/// Return true if the ZString cursor is at the end of the data.
///
bool eod();
///
///
///
2022-03-15 20:07:43 -07:00
bool startsWith(const char *value);
///
///
///
2021-07-23 11:44:36 -07:00
bool equals(const char *value);
2021-07-23 11:44:36 -07:00
///
///
///
bool equals(char *value);
///
///
///
2021-08-11 21:37:48 -07:00
bool equals(ZString &zstring);
2021-07-31 11:15:07 -07:00
///
///
///
2021-08-11 21:37:48 -07:00
bool equals(std::string &string);
2021-07-31 11:15:07 -07:00
2021-07-23 11:44:36 -07:00
///
/// Advance the cursor the length of the provided value if the value at
/// the cursor position is equal to the value provided.
///
2021-08-26 22:57:47 -07:00
bool ifNext(const char *value);
2021-07-23 11:44:36 -07:00
2022-03-15 20:07:43 -07:00
bool ifNext(ZString &value);
2022-01-24 14:03:58 -08:00
int ifEqualsCount(ZString &comparator);
2021-07-23 11:44:36 -07:00
///
/// Advance the cursor through the ZString for each whitespace character
/// encountered.
///
int skipWhitespace();
///
///
///
ZString goeol();
///
/// Return a block of data as a ZString from the cursor location for
2022-01-24 14:03:58 -08:00
/// the number of bytes specified by size.
2021-07-23 11:44:36 -07:00
///
ZString readBlock(size_t size);
///
/// Return a pointer to the beginning of the ZString data.
///
2022-01-24 14:03:58 -08:00
char* getData();
2021-07-23 11:44:36 -07:00
2022-03-15 20:07:43 -07:00
///
///
///
char* getCursor();
2021-07-23 11:44:36 -07:00
///
2022-01-24 14:03:58 -08:00
/// Return the length of the ZString.
2021-07-23 11:44:36 -07:00
///
size_t getLength();
2022-01-24 14:03:58 -08:00
2021-07-23 11:44:36 -07:00
///
/// Set this ZString to have the same data space as the ZString pointer
/// passed into the method.
///
2022-01-24 14:03:58 -08:00
2021-07-23 11:44:36 -07:00
void setZString(ZString zstring);
2022-01-24 14:03:58 -08:00
///
///
///
2022-03-15 20:07:43 -07:00
ZString getParsed();
///
///
///
2022-01-24 14:03:58 -08:00
void reset();
///
///
///
char charAt(int index);
///
///
///
bool ifCRLF();
2022-03-15 20:07:43 -07:00
void nextChar();
// protected:
2021-07-23 11:44:36 -07:00
char *data;
char *cursor;
2022-03-15 20:07:43 -07:00
size_t length;
2021-07-23 11:44:36 -07:00
2022-03-15 20:07:43 -07:00
private:
std::vector<ZString> list;
2022-01-24 14:03:58 -08:00
bool isCharacter(char ch, const char *string);
2021-07-23 11:44:36 -07:00
};
}
#endif