Skip to content

Commit

Permalink
keep lastCh intact when using FileStream::peek()
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Apr 20, 2022
1 parent 1f8d856 commit 1e8ee57
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,31 +382,36 @@ class FileStream : public simplecpp::TokenList::Stream {
}

virtual int get() {
lastCh = fgetc(file);
lastStatus = lastCh = fgetc(file);
return lastCh;
}
virtual int peek() {
const int ch = get();
unget();
// keep lastCh intact
const int ch = fgetc(file);
unget_internal(ch);
return ch;
}
virtual void unget() {
unget_internal(lastCh);
}
virtual bool good() {
return lastStatus != EOF;
}

private:
void unget_internal(int ch) {
if (isUtf16) {
// TODO: use ungetc() as well
// UTF-16 has subsequent unget() calls
fseek(file, -1, SEEK_CUR);
}
else
ungetc(lastCh, file);

}
virtual bool good() {
return lastCh != EOF;
ungetc(ch, file);
}

private:
FILE *file;
int lastCh;
int lastStatus;
};

simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(filenames) {}
Expand Down

0 comments on commit 1e8ee57

Please sign in to comment.