X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftextparser.cpp;h=78d2f1df6334b32dbf67a7871975deda4dafb0a4;hb=2f79370bffe0bac865dc97c5114dc87c1936fbb4;hp=298b7cac363ebd2b75a712b2ea41d18dd17b3801;hpb=27630d44298cb67e075c166f4421288cc8ca117e;p=libs%2Fdatafile.git diff --git a/source/textparser.cpp b/source/textparser.cpp index 298b7ca..78d2f1d 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -6,6 +6,7 @@ Distributed under the LGPL */ #include +#include #include "input.h" #include "textparser.h" #include "token.h" @@ -266,7 +267,16 @@ Token TextParser::parse_token() if(c=='\\') escape=!escape; else if(c=='"' && !escape) - return Token(Token::STRING, unescape_string(buf)); + { + try + { + return Token(Token::STRING, c_unescape(buf.substr(1, buf.size()-2))); + } + catch(const Exception &e) + { + throw ParseError(format("%s: %s", get_location(), e.what()), src, in.get_line_number()); + } + } else escape=false; break; @@ -297,57 +307,6 @@ bool TextParser::isodigit(int c) return (c>='0' && c<='7'); } -string TextParser::unescape_string(const string &str) -{ - string result; - bool escape=false; - unsigned hexcape=0; - for(string::const_iterator i=str.begin()+1; i!=str.end()-1; ++i) - { - if(escape) - { - if(*i=='n') - result+='\n'; - else if(*i=='t') - result+='\t'; - else if(*i=='\\') - result+='\\'; - else if(*i=='"') - result+='"'; - else if(*i=='x') - hexcape=0x100; - else - throw ParseError(format("%s: Invalid escape sequence '\\%c'", get_location(), *i), src, in.get_line_number()); - escape=false; - } - else if(hexcape) - { - unsigned digit=0; - if(*i>='0' && *i<='9') - digit=*i-'0'; - else if(*i>='a' && *i<='f') - digit=*i-'a'+10; - else if(*i>='A' && *i<='F') - digit=*i-'A'+10; - else - throw ParseError(get_location()+": Invalid hex digit", src, in.get_line_number()); - - hexcape=(hexcape<<4)|digit; - if(hexcape&0x10000) - { - result+=hexcape&0xFF; - hexcape=0; - } - } - else if(*i=='\\') - escape=true; - else - result+=*i; - } - - return result; -} - string TextParser::get_location() { ostringstream ss;