Skip to content

Commit 4ce68d2

Browse files
committed
added simplecpp::TokenList::Stream implementation "FileStream" which uses C I/O functions
1 parent 93b2860 commit 4ce68d2

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
1010
# no need for c++98 compatibility
1111
add_compile_options(-Wno-c++98-compat-pedantic)
1212
# these are not really fixable
13-
add_compile_options(-Wno-exit-time-destructors -Wno-global-constructors)
13+
add_compile_options(-Wno-exit-time-destructors -Wno-global-constructors -Wno-weak-vtables)
1414
# we are not interested in these
1515
add_compile_options(-Wno-multichar -Wno-four-char-constants)
16+
# ignore C++11-specific warning
17+
add_compile_options(-Wno-suggest-override -Wno-suggest-destructor-override)
1618
# TODO: fix these?
1719
add_compile_options(-Wno-padded -Wno-sign-conversion -Wno-implicit-int-conversion -Wno-shorten-64-to-32 -Wno-shadow-field-in-constructor)
1820

main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ int main(int argc, char **argv)
9393
// Perform preprocessing
9494
simplecpp::OutputList outputList;
9595
std::vector<std::string> files;
96-
std::ifstream f(filename);
97-
simplecpp::TokenList rawtokens(f,files,filename,&outputList);
96+
//std::ifstream f(filename);
97+
simplecpp::TokenList rawtokens(files,filename,&outputList);
9898
rawtokens.removeComments();
9999
std::map<std::string, simplecpp::TokenList*> included = simplecpp::load(rawtokens, files, dui, &outputList);
100100
for (std::pair<std::string, simplecpp::TokenList *> i : included)

simplecpp.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,40 @@ class StdIStream : public simplecpp::TokenList::Stream {
360360
std::istream &istr;
361361
};
362362

363+
class FileStream : public simplecpp::TokenList::Stream {
364+
public:
365+
FileStream(const std::string &filename)
366+
: file(fopen(filename.c_str(), "rb"))
367+
{
368+
init();
369+
}
370+
371+
~FileStream() {
372+
fclose(file);
373+
file = nullptr;
374+
}
375+
376+
virtual int get() {
377+
lastCh = fgetc(file);
378+
return lastCh;
379+
}
380+
virtual int peek() {
381+
const int ch = get();
382+
unget();
383+
return ch;
384+
}
385+
virtual void unget() {
386+
ungetc(lastCh, file);
387+
}
388+
virtual bool good() {
389+
return lastCh != EOF;
390+
}
391+
392+
private:
393+
FILE *file;
394+
int lastCh;
395+
};
396+
363397
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(filenames) {}
364398

365399
simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
@@ -369,6 +403,13 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
369403
readfile(stream,filename,outputList);
370404
}
371405

406+
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
407+
: frontToken(nullptr), backToken(nullptr), files(filenames)
408+
{
409+
FileStream stream(filename);
410+
readfile(stream,filename,outputList);
411+
}
412+
372413
simplecpp::TokenList::TokenList(const TokenList &other) : frontToken(nullptr), backToken(nullptr), files(other.files)
373414
{
374415
*this = other;

simplecpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ namespace simplecpp {
197197

198198
explicit TokenList(std::vector<std::string> &filenames);
199199
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
200+
TokenList(std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList = nullptr);
200201
TokenList(const TokenList &other);
201202
#if __cplusplus >= 201103L
202203
TokenList(TokenList &&other);

0 commit comments

Comments
 (0)