preprocessor: Better warning when illegal character found

This commit is contained in:
Daniel Marjamäki 2008-11-17 18:42:58 +00:00
parent 2018c25d20
commit 1cd9496039
1 changed files with 16 additions and 7 deletions

View File

@ -41,20 +41,23 @@ void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::str
// Get filedata from stream.. // Get filedata from stream..
bool ignoreSpace = true; bool ignoreSpace = true;
int lineno = 1;
std::ostringstream code; std::ostringstream code;
for (char ch = (char)istr.get(); istr.good(); ch = (char)istr.get()) for (char ch = (char)istr.get(); istr.good(); ch = (char)istr.get())
{ {
if ( ch < 0 ) if ( ch < 0 )
{ {
// Bad content.. // Bad content..
errout << "[" << filename << "] Bad character found: " << int((unsigned char)ch) << std::endl; errout << "[" << filename << ":" << lineno << "] Bad character found: " << int((unsigned char)ch) << std::endl;
result.clear(); result.clear();
return; return;
} }
if ( ch == '\n' )
++lineno;
// Replace assorted special chars with spaces.. // Replace assorted special chars with spaces..
if ( ch < 0 )
ch = ' ';
if ( (ch != '\n') && (isspace(ch) || iscntrl(ch)) ) if ( (ch != '\n') && (isspace(ch) || iscntrl(ch)) )
ch = ' '; ch = ' ';
@ -73,6 +76,7 @@ void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::str
while (istr.good() && ch!='\n') while (istr.good() && ch!='\n')
ch = (char)istr.get(); ch = (char)istr.get();
code << "\n"; code << "\n";
++lineno;
} }
else if ( chNext == '*' ) else if ( chNext == '*' )
@ -83,12 +87,17 @@ void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::str
chPrev = ch; chPrev = ch;
ch = (char)istr.get(); ch = (char)istr.get();
if (ch == '\n') if (ch == '\n')
{
code << "\n"; code << "\n";
++lineno;
}
} }
} }
else else
{ {
if ( chNext == '\n' )
++lineno;
code << std::string(1,ch) << std::string(1,chNext); code << std::string(1,ch) << std::string(1,chNext);
} }
} }