From 1696577be8aefc290b2748398690430c6c8eab78 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 29 Mar 2024 19:15:16 +0100 Subject: [PATCH 1/3] added `StdCharBufStream` which reads from a buffer --- simplecpp.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/simplecpp.cpp b/simplecpp.cpp index 1de36a52..61521641 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -377,6 +377,42 @@ class StdIStream : public simplecpp::TokenList::Stream { std::istream &istr; }; +class StdCharBufStream : public simplecpp::TokenList::Stream { +public: + // cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members + StdCharBufStream(const unsigned char* str, std::size_t size) + : str(str) + , size(size) + , pos(0) + , lastStatus(0) + { + init(); + } + + virtual int get() OVERRIDE { + if (pos >= size) + return lastStatus = EOF; + return str[pos++]; + } + virtual int peek() OVERRIDE { + if (pos >= size) + return lastStatus = EOF; + return str[pos]; + } + virtual void unget() OVERRIDE { + --pos; + } + virtual bool good() OVERRIDE { + return lastStatus != EOF; + } + +private: + const unsigned char *str; + const std::size_t size; + std::size_t pos; + int lastStatus; +}; + class FileStream : public simplecpp::TokenList::Stream { public: // cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members From 1cb98c854f24dbe08131c90617074a8d3bdf8ed1 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 29 Mar 2024 19:15:28 +0100 Subject: [PATCH 2/3] use `StdCharBufStream` in `Macro` --- simplecpp.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 61521641..3b0a077b 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1483,8 +1483,7 @@ namespace simplecpp { Macro(const std::string &name, const std::string &value, std::vector &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(false) { const std::string def(name + ' ' + value); - std::istringstream istr(def); - StdIStream stream(istr); + StdCharBufStream stream(reinterpret_cast(def.data()), def.size()); tokenListDefine.readfile(stream); if (!parseDefine(tokenListDefine.cfront())) throw std::runtime_error("bad macro syntax. macroname=" + name + " value=" + value); From 1895980b9c0d7551a6c22542f68f7bd615f23c45 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 29 Mar 2024 19:16:29 +0100 Subject: [PATCH 3/3] added `TokenList` constructors which take a buffer --- simplecpp.cpp | 14 ++++++++++++++ simplecpp.h | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/simplecpp.cpp b/simplecpp.cpp index 3b0a077b..91ba5984 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -478,6 +478,20 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector &fi readfile(stream,filename,outputList); } +simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector &filenames, const std::string &filename, OutputList *outputList) + : frontToken(nullptr), backToken(nullptr), files(filenames) +{ + StdCharBufStream stream(data, size); + readfile(stream,filename,outputList); +} + +simplecpp::TokenList::TokenList(const char* data, std::size_t size, std::vector &filenames, const std::string &filename, OutputList *outputList) + : frontToken(nullptr), backToken(nullptr), files(filenames) +{ + StdCharBufStream stream(reinterpret_cast(data), size); + readfile(stream,filename,outputList); +} + simplecpp::TokenList::TokenList(const std::string &filename, std::vector &filenames, OutputList *outputList) : frontToken(nullptr), backToken(nullptr), files(filenames) { diff --git a/simplecpp.h b/simplecpp.h index 87238378..50639457 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -198,6 +198,10 @@ namespace simplecpp { explicit TokenList(std::vector &filenames); /** generates a token list from the given std::istream parameter */ TokenList(std::istream &istr, std::vector &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr); + /** generates a token list from the given buffer */ + TokenList(const unsigned char* data, std::size_t size, std::vector &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr); + /** generates a token list from the given buffer */ + TokenList(const char* data, std::size_t size, std::vector &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr); /** generates a token list from the given filename parameter */ TokenList(const std::string &filename, std::vector &filenames, OutputList *outputList = nullptr); TokenList(const TokenList &other);