Skip to content

Commit d904016

Browse files
authored
made it possible to create a TokenList from a buffer (#347)
* added `StdCharBufStream` which reads from a buffer * use `StdCharBufStream` in `Macro` * added `TokenList` constructors which take a buffer
1 parent 8fcc8de commit d904016

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

simplecpp.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,42 @@ class StdIStream : public simplecpp::TokenList::Stream {
377377
std::istream &istr;
378378
};
379379

380+
class StdCharBufStream : public simplecpp::TokenList::Stream {
381+
public:
382+
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
383+
StdCharBufStream(const unsigned char* str, std::size_t size)
384+
: str(str)
385+
, size(size)
386+
, pos(0)
387+
, lastStatus(0)
388+
{
389+
init();
390+
}
391+
392+
virtual int get() OVERRIDE {
393+
if (pos >= size)
394+
return lastStatus = EOF;
395+
return str[pos++];
396+
}
397+
virtual int peek() OVERRIDE {
398+
if (pos >= size)
399+
return lastStatus = EOF;
400+
return str[pos];
401+
}
402+
virtual void unget() OVERRIDE {
403+
--pos;
404+
}
405+
virtual bool good() OVERRIDE {
406+
return lastStatus != EOF;
407+
}
408+
409+
private:
410+
const unsigned char *str;
411+
const std::size_t size;
412+
std::size_t pos;
413+
int lastStatus;
414+
};
415+
380416
class FileStream : public simplecpp::TokenList::Stream {
381417
public:
382418
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
@@ -442,6 +478,20 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
442478
readfile(stream,filename,outputList);
443479
}
444480

481+
simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
482+
: frontToken(nullptr), backToken(nullptr), files(filenames)
483+
{
484+
StdCharBufStream stream(data, size);
485+
readfile(stream,filename,outputList);
486+
}
487+
488+
simplecpp::TokenList::TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
489+
: frontToken(nullptr), backToken(nullptr), files(filenames)
490+
{
491+
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(data), size);
492+
readfile(stream,filename,outputList);
493+
}
494+
445495
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList)
446496
: frontToken(nullptr), backToken(nullptr), files(filenames)
447497
{
@@ -1447,8 +1497,7 @@ namespace simplecpp {
14471497

14481498
Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(false) {
14491499
const std::string def(name + ' ' + value);
1450-
std::istringstream istr(def);
1451-
StdIStream stream(istr);
1500+
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(def.data()), def.size());
14521501
tokenListDefine.readfile(stream);
14531502
if (!parseDefine(tokenListDefine.cfront()))
14541503
throw std::runtime_error("bad macro syntax. macroname=" + name + " value=" + value);

simplecpp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ namespace simplecpp {
199199
explicit TokenList(std::vector<std::string> &filenames);
200200
/** generates a token list from the given std::istream parameter */
201201
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
202+
/** generates a token list from the given buffer */
203+
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
204+
/** generates a token list from the given buffer */
205+
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
202206
/** generates a token list from the given filename parameter */
203207
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
204208
TokenList(const TokenList &other);

0 commit comments

Comments
 (0)