Skip to content

Commit 8cab5d1

Browse files
committed
added simplecpp::TokenList::Stream implementation "FileStream" which uses C I/O functions
1 parent 20b8d81 commit 8cab5d1

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
@@ -8,9 +8,11 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
88
# no need for c++98 compatibility
99
add_compile_options(-Wno-c++98-compat-pedantic)
1010
# these are not really fixable
11-
add_compile_options(-Wno-exit-time-destructors -Wno-global-constructors)
11+
add_compile_options(-Wno-exit-time-destructors -Wno-global-constructors -Wno-weak-vtables)
1212
# we are not interested in these
1313
add_compile_options(-Wno-multichar)
14+
# ignore C++11-specific warning
15+
add_compile_options(-Wno-suggest-override -Wno-suggest-destructor-override)
1416
# TODO: fix these?
1517
add_compile_options(-Wno-padded -Wno-sign-conversion -Wno-implicit-int-conversion -Wno-shorten-64-to-32)
1618

main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ int main(int argc, char **argv)
7272
// Perform preprocessing
7373
simplecpp::OutputList outputList;
7474
std::vector<std::string> files;
75-
std::ifstream f(filename);
76-
simplecpp::TokenList rawtokens(f,files,filename,&outputList);
75+
//std::ifstream f(filename);
76+
simplecpp::TokenList rawtokens(files,filename,&outputList);
7777
rawtokens.removeComments();
7878
std::map<std::string, simplecpp::TokenList*> included = simplecpp::load(rawtokens, files, dui, &outputList);
7979
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
@@ -343,6 +343,40 @@ class StdIStream : public simplecpp::TokenList::Stream {
343343
std::istream &istr;
344344
};
345345

346+
class FileStream : public simplecpp::TokenList::Stream {
347+
public:
348+
FileStream(const std::string &filename)
349+
: file(fopen(filename.c_str(), "rb"))
350+
{
351+
init();
352+
}
353+
354+
~FileStream() {
355+
fclose(file);
356+
file = nullptr;
357+
}
358+
359+
virtual int get() {
360+
lastCh = fgetc(file);
361+
return lastCh;
362+
}
363+
virtual int peek() {
364+
const int ch = get();
365+
unget();
366+
return ch;
367+
}
368+
virtual void unget() {
369+
ungetc(lastCh, file);
370+
}
371+
virtual bool good() {
372+
return lastCh != EOF;
373+
}
374+
375+
private:
376+
FILE *file;
377+
int lastCh;
378+
};
379+
346380
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(filenames) {}
347381

348382
simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
@@ -352,6 +386,13 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
352386
readfile(stream,filename,outputList);
353387
}
354388

389+
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
390+
: frontToken(nullptr), backToken(nullptr), files(filenames)
391+
{
392+
FileStream stream(filename);
393+
readfile(stream,filename,outputList);
394+
}
395+
355396
simplecpp::TokenList::TokenList(const TokenList &other) : frontToken(nullptr), backToken(nullptr), files(other.files)
356397
{
357398
*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)