Skip to content

Commit b3e6b7d

Browse files
committed
added simplecpp::TokenList::Stream implementation "FileStream" which uses C I/O functions
1 parent 5839988 commit b3e6b7d

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
@@ -357,6 +357,40 @@ class StdIStream : public simplecpp::TokenList::Stream {
357357
std::istream &istr;
358358
};
359359

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

362396
simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
@@ -366,6 +400,13 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
366400
readfile(stream,filename,outputList);
367401
}
368402

403+
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
404+
: frontToken(nullptr), backToken(nullptr), files(filenames)
405+
{
406+
FileStream stream(filename);
407+
readfile(stream,filename,outputList);
408+
}
409+
369410
simplecpp::TokenList::TokenList(const TokenList &other) : frontToken(nullptr), backToken(nullptr), files(other.files)
370411
{
371412
*this = other;

simplecpp.h

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

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

0 commit comments

Comments
 (0)