Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Don't cast fgetc()'s return value to a char. #7

Merged
merged 1 commit into from Nov 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 12 additions & 13 deletions config_parser.cc
Expand Up @@ -70,7 +70,7 @@ bool ConfigParser::Parse(SettingsMap* settings,
bool in_comment = false;

while (!stream_->AtEOF()) {
char ch = stream_->GetChar();
int ch = stream_->GetChar();

if (ch == '#') {
in_comment = true;
Expand Down Expand Up @@ -138,7 +138,7 @@ bool ConfigParser::CharStream::AtEOF() {
return (!have_buffered_char_ && AtEOFImpl());
}

char ConfigParser::CharStream::GetChar() {
int ConfigParser::CharStream::GetChar() {
assert(initialized_);

prev_at_line_end_ = at_line_end_;
Expand All @@ -147,7 +147,7 @@ char ConfigParser::CharStream::GetChar() {
at_line_end_ = false;
}

char ch = 0;
int ch = 0;
if (have_buffered_char_) {
have_buffered_char_ = false;
ch = buffered_char_;
Expand All @@ -161,7 +161,7 @@ char ConfigParser::CharStream::GetChar() {
return ch;
}

void ConfigParser::CharStream::UngetChar(char ch) {
void ConfigParser::CharStream::UngetChar(int ch) {
if (prev_at_line_end_)
line_num_--;
at_line_end_ = prev_at_line_end_;
Expand Down Expand Up @@ -202,10 +202,9 @@ bool ConfigParser::FileCharStream::AtEOFImpl() {
return ch == EOF;
}

char ConfigParser::FileCharStream::GetCharImpl() {
int ConfigParser::FileCharStream::GetCharImpl() {
assert(file_);
int ch = fgetc(file_);
return ch;
return fgetc(file_);
}

ConfigParser::StringCharStream::StringCharStream(const string& data)
Expand All @@ -217,7 +216,7 @@ bool ConfigParser::StringCharStream::AtEOFImpl() {
return pos_ == data_.size();
}

char ConfigParser::StringCharStream::GetCharImpl() {
int ConfigParser::StringCharStream::GetCharImpl() {
return data_.at(pos_++);
}

Expand All @@ -230,7 +229,7 @@ bool ConfigParser::ReadSettingName(string* name_out) {
if (stream_->AtEOF())
break;

char ch = stream_->GetChar();
int ch = stream_->GetChar();
if (isspace(ch) || ch == '#') {
stream_->UngetChar(ch);
break;
Expand Down Expand Up @@ -287,7 +286,7 @@ bool ConfigParser::ReadValue(Setting** setting_ptr) {
return false;
}

char ch = stream_->GetChar();
int ch = stream_->GetChar();
stream_->UngetChar(ch);

if ((ch >= '0' && ch <= '9') || ch == '-') {
Expand Down Expand Up @@ -322,7 +321,7 @@ bool ConfigParser::ReadInteger(int32_t* int_out) {
if (stream_->AtEOF())
break;

char ch = stream_->GetChar();
int ch = stream_->GetChar();
if (isspace(ch) || ch == '#') {
stream_->UngetChar(ch);
break;
Expand Down Expand Up @@ -383,7 +382,7 @@ bool ConfigParser::ReadString(string* str_out) {
return false;
}

char ch = stream_->GetChar();
int ch = stream_->GetChar();
if (ch == '\n') {
SetErrorF("Got newline mid-string");
return false;
Expand Down Expand Up @@ -440,7 +439,7 @@ bool ConfigParser::ReadColor(uint16_t* red_out,
return false;
}

char ch = stream_->GetChar();
int ch = stream_->GetChar();
if (ch == '\n') {
SetErrorF("Got newline mid-color");
return false;
Expand Down
12 changes: 6 additions & 6 deletions config_parser.h
Expand Up @@ -70,16 +70,16 @@ class ConfigParser {
bool AtEOF();

// Get the next character in the stream.
char GetChar();
int GetChar();

// Push a previously-read character back onto the stream.
// At most one character can be buffered.
void UngetChar(char ch);
void UngetChar(int ch);

private:
virtual bool InitImpl(std::string* error_out) { return true; }
virtual bool AtEOFImpl() = 0;
virtual char GetCharImpl() = 0;
virtual int GetCharImpl() = 0;

// Has Init() been called?
bool initialized_;
Expand All @@ -88,7 +88,7 @@ class ConfigParser {
bool have_buffered_char_;

// The character returned by UngetChar().
char buffered_char_;
int buffered_char_;

// Are we currently at the end of the line?
bool at_line_end_;
Expand All @@ -110,7 +110,7 @@ class ConfigParser {
private:
bool InitImpl(std::string* error_out);
bool AtEOFImpl();
char GetCharImpl();
int GetCharImpl();

std::string filename_;
FILE* file_;
Expand All @@ -125,7 +125,7 @@ class ConfigParser {

private:
bool AtEOFImpl();
char GetCharImpl();
int GetCharImpl();

std::string data_;
size_t pos_;
Expand Down