Skip to content

Commit 1126326

Browse files
committed
StdCharBufStream
1 parent 3960aa4 commit 1126326

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

simplecpp.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
#include <cstring>
2121
#include <ctime>
2222
#include <exception>
23-
#include <fstream> // IWYU pragma: keep
23+
#include <fstream>
2424
#include <iostream>
2525
#include <limits>
2626
#include <list>
2727
#include <map>
2828
#include <set>
29-
#include <sstream> // IWYU pragma: keep
29+
#include <sstream>
3030
#include <stack>
3131
#include <stdexcept>
3232
#include <string>
@@ -377,6 +377,40 @@ 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(-1)
387+
{
388+
init();
389+
}
390+
391+
virtual int get() OVERRIDE {
392+
if (pos >= size)
393+
return EOF;
394+
return str[++pos];
395+
}
396+
virtual int peek() OVERRIDE {
397+
if ((pos+1) >= size)
398+
return EOF;
399+
return str[pos+1];
400+
}
401+
virtual void unget() OVERRIDE {
402+
--pos;
403+
}
404+
virtual bool good() OVERRIDE {
405+
return pos < size;
406+
}
407+
408+
private:
409+
const unsigned char *str;
410+
const int size;
411+
int pos;
412+
};
413+
380414
class FileStream : public simplecpp::TokenList::Stream {
381415
public:
382416
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
@@ -442,6 +476,20 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
442476
readfile(stream,filename,outputList);
443477
}
444478

479+
simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
480+
: frontToken(nullptr), backToken(nullptr), files(filenames)
481+
{
482+
StdCharBufStream stream(data, size);
483+
readfile(stream,filename,outputList);
484+
}
485+
486+
simplecpp::TokenList::TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
487+
: frontToken(nullptr), backToken(nullptr), files(filenames)
488+
{
489+
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(data), size);
490+
readfile(stream,filename,outputList);
491+
}
492+
445493
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList)
446494
: frontToken(nullptr), backToken(nullptr), files(filenames)
447495
{
@@ -1447,8 +1495,7 @@ namespace simplecpp {
14471495

14481496
Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(false) {
14491497
const std::string def(name + ' ' + value);
1450-
std::istringstream istr(def);
1451-
StdIStream stream(istr);
1498+
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(def.data()), def.size());
14521499
tokenListDefine.readfile(stream);
14531500
if (!parseDefine(tokenListDefine.cfront()))
14541501
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
@@ -198,6 +198,10 @@ namespace simplecpp {
198198
explicit TokenList(std::vector<std::string> &filenames);
199199
/** generates a token list from the given std::istream parameter */
200200
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
201+
/** generates a token list from the given buffer */
202+
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
203+
/** generates a token list from the given buffer */
204+
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
201205
/** generates a token list from the given filename parameter */
202206
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
203207
TokenList(const TokenList &other);

0 commit comments

Comments
 (0)