Skip to content

Commit e5c0b7f

Browse files
committed
StdCharBufStream
1 parent a816ad7 commit e5c0b7f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

simplecpp.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,40 @@ class StdStringStream : public simplecpp::TokenList::Stream {
411411
int pos;
412412
};
413413

414+
class StdCharBufStream : public simplecpp::TokenList::Stream {
415+
public:
416+
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
417+
EXPLICIT StdCharBufStream(const char* str, int size)
418+
: str(str)
419+
, size(size)
420+
, pos(-1)
421+
{
422+
init();
423+
}
424+
425+
virtual int get() OVERRIDE {
426+
if (pos >= size)
427+
return EOF;
428+
return str[++pos];
429+
}
430+
virtual int peek() OVERRIDE {
431+
if ((pos+1) >= size)
432+
return EOF;
433+
return str[pos+1];
434+
}
435+
virtual void unget() OVERRIDE {
436+
--pos;
437+
}
438+
virtual bool good() OVERRIDE {
439+
return pos < size;
440+
}
441+
442+
private:
443+
const char *str;
444+
const int size;
445+
int pos;
446+
};
447+
414448
class FileStream : public simplecpp::TokenList::Stream {
415449
public:
416450
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
@@ -483,6 +517,13 @@ simplecpp::TokenList::TokenList(const std::string &str, std::vector<std::string>
483517
readfile(stream,filename,outputList);
484518
}
485519

520+
simplecpp::TokenList::TokenList(const char* str, int size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
521+
: frontToken(nullptr), backToken(nullptr), files(filenames)
522+
{
523+
StdCharBufStream stream(str, size);
524+
readfile(stream,filename,outputList);
525+
}
526+
486527
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList)
487528
: frontToken(nullptr), backToken(nullptr), files(filenames)
488529
{

simplecpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ namespace simplecpp {
200200
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
201201
/** generates a token list from the given std::string parameter */
202202
TokenList(const std::string &str, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
203+
/** generates a token list from the given char buffer parameter */
204+
TokenList(const char* str, int size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
203205
/** generates a token list from the given filename parameter */
204206
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
205207
TokenList(const TokenList &other);

0 commit comments

Comments
 (0)