added to Generator:

void RecognizeSpecialChars(bool spec);
  recognizing some special characters in text patterns (item_text in Patterns)
  \r will be a carriage return (13)
  \n will be a new line (10)
  \t will be a tabulator (9)
  \s will be a space
  \\ will be one '\'
  default: false

void TrimWhite(bool trim);
  trimming white characters (at the beginning and at the end of an item_text)
  (special char \s if enabled is not treated as a white character here)
  default: false 

void SkipNewLine(bool skip);
  skipping new line characters (from the whole string in an item_text)
  but you can use a new line character written as "\n" (if special chars are turn on)
  default: false
						       


git-svn-id: svn://ttmath.org/publicrep/ezc/trunk@305 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2010-09-07 23:52:41 +00:00
parent 376fe414f8
commit b5ea2514e2
2 changed files with 146 additions and 4 deletions

View File

@ -298,7 +298,7 @@ return *(itext++);
}
bool Pattern::IsWhite(int c )
bool Pattern::IsWhite(int c)
{
// 13 (\r) is from a dos file at the end of a line (\r\n)
// 160 is an unbreakable space
@ -324,7 +324,6 @@ size_t i;
if( s.empty() )
return;
//for(i=0 ; i<s.size() && (s[i]==10 || IsWhite(s[i])) ; ++i); // !!! skasowac
for(i=0 ; i<s.size() && IsWhite(s[i]) ; ++i);
if( i == s.size() )
@ -1039,6 +1038,9 @@ Generator::Generator()
max_items = 50000;
max_for_items = 5000;
special_chars = false;
trim_white = false;
skip_new_line = false;
}
@ -1050,6 +1052,9 @@ Generator::Generator(std::ostringstream & o, Pattern & p, Functions & f)
max_items = 50000;
max_for_items = 5000;
special_chars = false;
trim_white = false;
skip_new_line = false;
}
@ -1086,6 +1091,22 @@ void Generator::SetMax(int max_items_, int max_for_items_)
}
void Generator::RecognizeSpecialChars(bool spec)
{
special_chars = spec;
}
void Generator::TrimWhite(bool trim)
{
trim_white = trim;
}
void Generator::SkipNewLine(bool skip)
{
skip_new_line = skip;
}
void Generator::Generate()
{
@ -1195,7 +1216,97 @@ return called;
}
char Generator::CreateSpecialChar(char c)
{
char res = 0;
if( c == 'r' )
res = '\r';
else
if( c == 'n' )
res = '\n';
else
if( c == 't' )
res = '\t';
else
if( c == 's' )
res = ' ';
else
if( c == '\\' )
res = '\\';
return res;
}
void Generator::PrintSpecialText(const char * start, const char * end)
{
char special;
while( start != end )
{
special = 0;
if( *start == '\\' && (start+1) != end )
special = CreateSpecialChar(*(start+1));
if( special )
{
*output_stream << special;
start += 2;
}
else
{
if( !(skip_new_line && *start == 10) )
*output_stream << *start;
start += 1;
}
}
}
void Generator::PrintNormalText(const char * start, const char * end)
{
if( skip_new_line )
{
for( ; start != end ; ++start)
{
if( *start != 10 )
*output_stream << *start;
}
}
else
{
if( start != end )
output_stream->write(start, end-start);
}
}
void Generator::TrimWhite(const char *& start, const char *& end)
{
while( start != end && pattern->IsWhite(*start) )
++start;
while( start != end && pattern->IsWhite(*(end-1)) )
--end;
}
void Generator::MakeItemText(Pattern::Item & item)
{
const char * start = item.text.c_str();
const char * end = item.text.c_str() + item.text.size();
if( trim_white )
TrimWhite(start, end);
if( special_chars )
PrintSpecialText(start, end);
else
PrintNormalText(start, end);
}
void Generator::MakeTextContainer(Pattern::Item & item)
@ -1535,7 +1646,7 @@ void Generator::MakeText(Pattern::Item & item)
return;
}
if ( item.type == Pattern::Item::item_text ) *output_stream << item.text;
if ( item.type == Pattern::Item::item_text ) MakeItemText(item);
else if( item.type == Pattern::Item::item_container ) MakeTextContainer(item);
else if( item.type == Pattern::Item::item_normal ) MakeTextNormal(item);
else if( item.type == Pattern::Item::item_if ) MakeTextIf(item);

View File

@ -84,6 +84,9 @@ public:
void CreateMsg(std::ostringstream & o, const char * type, const char * arg = 0);
std::string CreateMsg(const char * type, const char * arg = 0);
bool IsWhite(int c);
struct Item
{
@ -158,7 +161,6 @@ private:
bool ReadFileFromDir(const std::string & dir, const char * name, std::string & result);
int ReadCharInText();
bool IsWhite(int c);
void SkipWhite();
void CheckWhiteAndDelete(std::string & s);
@ -287,6 +289,26 @@ public:
void Set(Functions & f);
void SetMax(int max_items_, int max_for_items_);
// recognizing some special characters in text patterns (item_text in Patterns)
// \r will be a carriage return (13)
// \n will be a new line (10)
// \t will be a tabulator (9)
// \s will be a space
// \\ will be one '\'
// default: false
void RecognizeSpecialChars(bool spec);
// trimming white characters (at the beginning and at the end of an item_text)
// (special char \s if enabled is not treated as a white character here)
// default: false
void TrimWhite(bool trim);
// skipping new line characters (from the whole string in an item_text)
// but you can use a new line character written as "\n" (if special chars are turn on)
// default: false
void SkipNewLine(bool skip);
void Generate();
private:
@ -299,6 +321,9 @@ private:
int current_item;
int max_items;
int max_for_items;
bool special_chars;
bool trim_white;
bool skip_new_line;
// an empty string for info objects
const std::string empty;
@ -312,6 +337,11 @@ private:
void CallUserFunction(Functions::Function * function, Info & info);
void CallVariable(Functions::Function * function, Info & info);
char CreateSpecialChar(char c);
void PrintSpecialText(const char * start, const char * end);
void PrintNormalText(const char * start, const char * end);
void TrimWhite(const char *& start, const char *& end);
void MakeTextIf_go(Pattern::Item & item, bool result);
bool MakeTextIfindexnumber(Pattern::Item & item, Functions::Function * function, bool & result);
void MakeTextIf(Pattern::Item & item);
@ -323,6 +353,7 @@ private:
void MakeTextIfindex(Pattern::Item & item);
void MakeTextForLoop(Pattern::Item & item, Functions::Function * function);
void MakeTextFor(Pattern::Item & item);
void MakeItemText(Pattern::Item & item);
void MakeTextContainer(Pattern::Item & item);
void MakeTextNormal(Pattern::Item & item);
void MakeTextIs(Pattern::Item & item);