added: support for UTF-8 (files utf8.h utf8.cpp)
they can be even used without the rest library
as only a library for converting between wide characters and UTF-8
changed: everywhere we use std::wstring instead of std::string
changed: Generator and Functions are templates now
they take a stream type
renamed: Info to FunInfo and it is a template too
taking a stream type
now you can use other kind of streams with the library
previous was only std::ostringstream
git-svn-id: svn://ttmath.org/publicrep/ezc/trunk@326 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
1 parent
b5ea2514e2
commit
8f94937ed1
15 files changed
+3805
-1997
No files matched your search
@@ -36,334 +36,12 @@
|
||||
*/
|
||||
|
||||
|
||||
#ifndef headerfileezc
|
||||
#define headerfileezc
|
||||
#ifndef headerfile_ezc_ezc
|
||||
#define headerfile_ezc_ezc
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
namespace Ezc
|
||||
{
|
||||
|
||||
|
||||
class Pattern
|
||||
{
|
||||
public:
|
||||
|
||||
Pattern();
|
||||
|
||||
void ParseFile(const std::string & file_name);
|
||||
void ParseFile(const char * file_name);
|
||||
void ParseString(const std::string & str);
|
||||
void ParseString(const char * str);
|
||||
|
||||
// first we're trying to read a file from directory dir
|
||||
// if there is no such a file there then we try read from dir2
|
||||
// (the second dir2 can be empty - it will not be used)
|
||||
void Directory(const char * dir, const char * dir2 = 0);
|
||||
void Directory(const std::string & dir);
|
||||
void Directory(const std::string & dir, const std::string & dir2);
|
||||
|
||||
void AllowInclude(bool allow);
|
||||
void DeleteWhiteTextItems(bool del);
|
||||
|
||||
void SetCommentary(const char * com_start, const char * com_stop);
|
||||
void SetCommentary(const std::string & com_start, const std::string & com_stop);
|
||||
|
||||
void SetIncludeMax(int include_max);
|
||||
|
||||
void Clear();
|
||||
|
||||
|
||||
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
|
||||
{
|
||||
enum Type
|
||||
{
|
||||
item_none, item_container, item_text, item_normal, item_is, item_isno,
|
||||
item_if, item_ifno, item_ifany, item_ifone, item_ifanyno, item_ifoneno, item_ifindex,
|
||||
item_for, item_else, item_end, item_err, item_include, item_comment, item_def
|
||||
};
|
||||
|
||||
struct Function
|
||||
{
|
||||
std::string name; // function name
|
||||
std::vector<std::string> params; // function parameters
|
||||
};
|
||||
|
||||
Type type;
|
||||
std::string text; // used in: item_text, item_include (as a file name)
|
||||
std::vector<Item*> item_tab; // !! zamienic na 'items'?
|
||||
std::vector<Function> functions;
|
||||
|
||||
Item();
|
||||
Item(const Item & i);
|
||||
Item & operator=(const Item & i);
|
||||
void CopyItemTable(const Item & i);
|
||||
~Item();
|
||||
|
||||
Item * AddItem(const Item * porg = 0);
|
||||
Item * AddItem(const Item & porg);
|
||||
void ClearItems();
|
||||
Type LastItemType();
|
||||
void DeleteLastItem();
|
||||
void Clear();
|
||||
};
|
||||
|
||||
Item item_root;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
const char * itext;
|
||||
|
||||
// allowing include tag
|
||||
// default: true
|
||||
bool allow_include;
|
||||
|
||||
// if true all text-items which have only white characters (with new lines as well)
|
||||
// will be deleted - useful in *.txt templates
|
||||
// this not actually delete the whole item but only the string
|
||||
// the item will be present with an empty string
|
||||
// default: false
|
||||
bool delete_white_text_items;
|
||||
|
||||
// first we're trying to read a file from 'directory'
|
||||
// if there is no such a file there then we try read from 'directory2'
|
||||
// we read from these directories only if they are not empty
|
||||
std::string directory, directory2;
|
||||
|
||||
int include_level, include_level_max;
|
||||
|
||||
|
||||
|
||||
std::string commentary_start, commentary_stop;
|
||||
|
||||
|
||||
|
||||
|
||||
std::string ReadFile(const std::string & name);
|
||||
std::string ReadFile(const char * name);
|
||||
bool HasFileAtBeginning(const char * path, const char * file);
|
||||
bool IsFileCorrect(const char * name);
|
||||
bool ReadFileFromDir(const std::string & dir, const char * name, std::string & result);
|
||||
|
||||
int ReadCharInText();
|
||||
void SkipWhite();
|
||||
void CheckWhiteAndDelete(std::string & s);
|
||||
|
||||
bool IsNameChar(int c);
|
||||
bool IsDigit(int c);
|
||||
bool IsPositiveNumber(const std::string & str);
|
||||
|
||||
bool ReadName(std::string & name);
|
||||
bool ReadString(std::string & str);
|
||||
bool ReadFunction(Item::Function & function);
|
||||
bool ReadParams(Item::Function & function);
|
||||
bool ReadFunctions(Item & item);
|
||||
|
||||
void ReadDirectiveIf(Item & item);
|
||||
void ReadDirectiveIfno(Item & item);
|
||||
void ReadDirectiveIfany(Item & item);
|
||||
void ReadDirectiveIfone(Item & item);
|
||||
void ReadDirectiveIfanyno(Item & item);
|
||||
void ReadDirectiveIfoneno(Item & item);
|
||||
void ReadDirectiveIs(Item & item);
|
||||
void ReadDirectiveIsno(Item & item);
|
||||
void ReadDirectiveIfindex(Item & item);
|
||||
void ReadDirectiveFor(Item & item);
|
||||
void ReadDirectiveComment(Item & item);
|
||||
void ReadDirectiveInclude(Item & item);
|
||||
void ReadDirectiveDef(Item & item);
|
||||
void ReadDirectiveNormal(const std::string & name, Item & item);
|
||||
|
||||
void CreateTreeReadItemDirectiveCheckEnding(Item & item);
|
||||
void CreateTreeReadItemDirective(Item & item);
|
||||
void CreateTreeReadItemText(Item & item);
|
||||
bool CreateTreeReadItem(Item & item);
|
||||
void CreateTreeReadIf(Item & item);
|
||||
void CreateTreeReadFor(Item & item);
|
||||
void CreateTree(Item & item);
|
||||
void CreateTreeReadInclude(Item & item);
|
||||
void CreateTreeReadIncludeSkipAllowFlag(Item & item);
|
||||
|
||||
}; // class Pattern
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
struct Info
|
||||
{
|
||||
// output stream
|
||||
std::ostringstream & out;
|
||||
|
||||
// table of parameters
|
||||
std::vector<std::string> & params;
|
||||
|
||||
// the first parameter
|
||||
// you can always use it even if there is not any parameters
|
||||
// in such a way the reference points to an empty valid string
|
||||
const std::string & par;
|
||||
|
||||
// this is set by Generator
|
||||
// normally is 0
|
||||
// in a [for] statement it indicates the number of the current iteration (the first is 0)
|
||||
int iter;
|
||||
|
||||
// return value from a user function (default false if not set directly by the user function)
|
||||
// for a variable it is set to true if the variable is not empty
|
||||
bool res;
|
||||
|
||||
// arguments: output_stream, table_of_parameters, the_first_parameter
|
||||
Info(std::ostringstream & o, std::vector<std::string> & pars, const std::string & p);
|
||||
void Clear();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// functions or variables
|
||||
class Functions
|
||||
{
|
||||
public:
|
||||
|
||||
typedef void (*UserFunction)(Info &);
|
||||
enum Type { function, variable };
|
||||
|
||||
struct Function
|
||||
{
|
||||
Type type;
|
||||
|
||||
UserFunction user_function; // used when type is 'function'
|
||||
std::string variable; // used when type is 'variable'
|
||||
|
||||
int iter;
|
||||
bool is_for; // true if is used by a [for] statement
|
||||
bool is_running; // true if this function is currently executed
|
||||
|
||||
Function();
|
||||
};
|
||||
|
||||
void Insert(const std::string & key, UserFunction ufunction); // inserting a function
|
||||
void Insert(const std::string & key, const char * var); // inserting a variable
|
||||
void Insert(const std::string & key, const std::string & var); // inserting a variable
|
||||
|
||||
bool Find(const std::string & key, Function ** fun);
|
||||
void Clear();
|
||||
|
||||
|
||||
private:
|
||||
typedef std::map<std::string, Function> FunctionsTable;
|
||||
FunctionsTable functions_tab;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Generator
|
||||
{
|
||||
public:
|
||||
|
||||
Generator();
|
||||
Generator(std::ostringstream & o, Pattern & p, Functions & f);
|
||||
|
||||
void Set(std::ostringstream & o, Pattern & p, Functions & f);
|
||||
void Set(std::ostringstream & o);
|
||||
void Set(Pattern & p);
|
||||
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:
|
||||
|
||||
std::ostringstream * output_stream;
|
||||
Pattern * pattern;
|
||||
Functions * functions;
|
||||
|
||||
bool break_generating;
|
||||
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;
|
||||
|
||||
bool Find(const std::string & key, Functions::Function ** function);
|
||||
|
||||
void Call(Functions::Function * function, Info & info);
|
||||
bool Call(const std::string & name, Info & info, Functions::Function ** pfun = 0);
|
||||
bool Call(Pattern::Item::Function & function, bool * info_res = 0, Functions::Function ** pfun = 0);
|
||||
|
||||
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);
|
||||
void MakeTextIfno(Pattern::Item & item);
|
||||
void MakeTextIfany(Pattern::Item & item);
|
||||
void MakeTextIfone(Pattern::Item & item);
|
||||
void MakeTextIfanyno(Pattern::Item & item);
|
||||
void MakeTextIfoneno(Pattern::Item & item);
|
||||
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);
|
||||
void MakeTextIsno(Pattern::Item & item);
|
||||
void MakeTextDefine(Pattern::Item & item);
|
||||
void MakeText(Pattern::Item & item);
|
||||
|
||||
}; // class Generator
|
||||
|
||||
} // namespace Ezc
|
||||
#include "utf8.h"
|
||||
#include "generator.h"
|
||||
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user