diff --git a/src/generator.h b/src/generator.h index 1d417f9..1e42eed 100755 --- a/src/generator.h +++ b/src/generator.h @@ -46,8 +46,11 @@ namespace Ezc { - - +/* +StreamType + we use method write where the content should not be escaped (html escaping) + we use operator<< where the content can be escaped (such as error messages) +*/ template class Generator { @@ -68,6 +71,8 @@ public: // \t will be a tabulator (9) // \s will be a space // \\ will be one '\' + // if the second character is different from those shown above the first slash will be printed + // so "\x" gives "\x" // default: false void RecognizeSpecialChars(bool spec); @@ -81,13 +86,13 @@ public: // default: false void SkipNewLine(bool skip); - + // the main method for generating void Generate(); private: StreamType * output_stream; - Pattern * pattern; + Pattern * pattern; Functions * functions; // temporary error messages @@ -106,66 +111,6 @@ private: const std::wstring empty; - void PutChar(wchar_t z, StreamType & out); - void PutStringGeneric(const wchar_t * start, size_t len, StreamType & out); - - -// !! tu bedzie chyba problem z kompilacja -// kiedy typ strumienia to bedzie std::ostream -// chwilowo zostawiam jak jest - -/* -void PutString(const wchar_t * start, const wchar_t * end, std::ostream & out) -{ - PutStringGeneric(start, end-start, out); -} - - - -void PutString(const wchar_t * start, const wchar_t * end, std::iostream & out) -{ - PutStringGeneric(start, end-start, out); -} - - -void PutString(const wchar_t * start, const wchar_t * end, std::ofstream & out) -{ - PutStringGeneric(start, end-start, out); -} - - -void PutString(const wchar_t * start, const wchar_t * end, std::fstream & out) -{ - PutStringGeneric(start, end-start, out); -} - - -void PutString(const wchar_t * start, const wchar_t * end, std::ostringstream & out) -{ - PutStringGeneric(start, end-start, out); -} - - -void PutString(const wchar_t * start, const wchar_t * end, std::stringstream & out) -{ - PutStringGeneric(start, end-start, out); -} -*/ - - - - void PutString(const std::wstring & str, StreamType & out) - { - PutString(str.c_str(), str.c_str() + str.size(), out); - } - - void PutString(const wchar_t * start, const wchar_t * end, StreamType & out) - { - out.write(start, end-start); - } - - - bool Find(const std::wstring & key, typename Functions::Function ** function); void Call(typename Functions::Function * function, FunInfo & info); @@ -176,6 +121,7 @@ void PutString(const wchar_t * start, const wchar_t * end, std::stringstream & o void CallVariable(typename Functions::Function * function, FunInfo & info); wchar_t CreateSpecialChar(wchar_t c); + const wchar_t * PrintSpecialChar(const wchar_t * start, const wchar_t * end); void PrintSpecialText(const wchar_t * start, const wchar_t * end); void PrintNormalText(const wchar_t * start, const wchar_t * end); void TrimWhite(const wchar_t *& start, const wchar_t *& end); @@ -209,39 +155,6 @@ void PutString(const wchar_t * start, const wchar_t * end, std::stringstream & o -template -void Generator::PutChar(wchar_t z, StreamType & out) -{ -typedef typename StreamType::char_type CharType; - - out << static_cast(z); -} - -/* -template -void Generator::PutString(const wchar_t * start, const wchar_t * end, StreamType & out) -{ - out.write(start, end-start); -} - - -template -void Generator::PutString(const std::wstring & str, StreamType & out) -{ - PutString(str.c_str(), str.c_str() + str.size(), out); -} -*/ - - -template -void Generator::PutStringGeneric(const wchar_t * start, size_t len, StreamType & out) -{ -typedef typename StreamType::char_type CharType; - - for(size_t i=0 ; i(start[i]); -} - @@ -473,30 +386,58 @@ return res; } + +// a special character is precedeed by a slash '\' +// if the special character is unknown the first slash is printing +// so "\t" gives one character of code 9 +// and "\x" gives "\x" +template +const wchar_t * Generator::PrintSpecialChar(const wchar_t * start, const wchar_t * end) +{ +wchar_t special = 0; + + if( *start == '\\' && (start+1) != end ) + special = CreateSpecialChar(*(start+1)); + + if( special ) + { + output_stream->write(&special, 1); + start += 2; + } + else + { + if( !skip_new_line || *start != 10 ) + output_stream->write(start, 1); + + start += 1; + } + +return start; +} + + template void Generator::PrintSpecialText(const wchar_t * start, const wchar_t * end) { -wchar_t special; - while( start != end ) { - special = 0; + const wchar_t * end2 = start; - if( *start == '\\' && (start+1) != end ) - special = CreateSpecialChar(*(start+1)); + // looking for a first new line character or a special char + // (for new line only if skip_new_line is true) + while( end2 != end && *end2 != '\\' && (!skip_new_line || *end2 != 10) ) + end2 += 1; - if( special ) - { - *output_stream << special; - start += 2; - } - else - { - if( !(skip_new_line && *start == 10) ) - *output_stream << *start; + // printing the first part of the text + if( start != end2 ) + output_stream->write(start, end2 - start); - start += 1; - } + start = end2; + + // skipping one or more new line characters or special chars + // (new lines only if skip_new_line is true) + while( start != end && (*start == '\\' || (skip_new_line && *start == 10)) ) + start = PrintSpecialChar(start, end); } } @@ -506,16 +447,29 @@ void Generator::PrintNormalText(const wchar_t * start, const wchar_t { if( skip_new_line ) { - for( ; start != end ; ++start) + while( start != end ) { - if( *start != 10 ) - PutChar(*start, *output_stream); + const wchar_t * end2 = start; + + // looking for a first new line character + while( end2 != end && *end2 != 10 ) + end2 += 1; + + // printing the first part of the text (until the new line) + if( start != end2 ) + output_stream->write(start, end2 - start); + + start = end2; + + // skipping one or more new line characters + while( start != end && *start == 10 ) + start += 1; } } else { if( start != end ) - PutString(start, end, *output_stream); + output_stream->write(start, end-start); } } @@ -569,7 +523,7 @@ template void Generator::CreateMsg(StreamType & o, const wchar_t * type, const wchar_t * arg) { pattern->CreateMsg(temp_msg, type, arg); - PutString(temp_msg, o); + o << temp_msg; temp_msg.clear(); } @@ -630,7 +584,7 @@ void Generator::MakeTextNormal(Item & item) bool called = Call(item.functions[0], 0, &pfun); if( called && pfun->type == Functions::variable ) - PutString(pfun->variable, *output_stream); + (*output_stream) << pfun->variable; }