2007-01-26 20:29:36 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of EZC -- Easy templating in C++
|
|
|
|
* and is distributed under the (new) BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@slimaczek.pl>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2008-12-10 02:46:10 +01:00
|
|
|
* Copyright (c) 2007-2008, Tomasz Sowa
|
2007-01-26 20:29:36 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
|
|
|
* project may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-01-27 14:52:18 +01:00
|
|
|
/*
|
2008-12-23 21:08:11 +01:00
|
|
|
* version 0.9.2
|
2007-01-27 14:52:18 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-01-24 21:01:55 +01:00
|
|
|
#ifndef headerfileezc
|
|
|
|
#define headerfileezc
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2007-01-25 22:42:54 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2007-01-24 21:01:55 +01:00
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
namespace Ezc
|
2007-01-24 21:01:55 +01:00
|
|
|
{
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
void CreateMsg(std::ostringstream & o, const char * type, const char * arg = 0);
|
2007-01-26 20:29:36 +01:00
|
|
|
std::string CreateMsg(const char * type, const char * arg = 0);
|
2007-01-27 14:52:18 +01:00
|
|
|
void SplitUnixDirectory(const char * name, std::string & dir, std::string & file);
|
|
|
|
void SplitUnixDirectory(const std::string & name, std::string & dir, std::string & file);
|
2007-01-24 21:01:55 +01:00
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
class Pattern
|
|
|
|
{
|
|
|
|
public:
|
2007-01-27 14:52:18 +01:00
|
|
|
void ParseFile(const std::string & file_name);
|
2007-01-26 20:29:36 +01:00
|
|
|
void ParseFile(const char * file_name);
|
|
|
|
void Directory(const char * d);
|
|
|
|
void Directory(const std::string & d);
|
2007-01-24 21:01:55 +01:00
|
|
|
|
|
|
|
struct Item
|
|
|
|
{
|
2008-12-10 02:46:10 +01:00
|
|
|
// change the name to 'Type'
|
2007-01-24 21:01:55 +01:00
|
|
|
enum ItemType
|
|
|
|
{
|
2008-12-23 21:08:11 +01:00
|
|
|
item_none, item_container, item_text, item_ifany, item_ifno, item_for,
|
2007-01-27 14:52:18 +01:00
|
|
|
item_else, item_end, item_err, item_normal, item_ifindex,
|
2008-12-10 02:46:10 +01:00
|
|
|
item_include, item_is, item_ifone, item_comment, item_def
|
2007-01-24 21:01:55 +01:00
|
|
|
};
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-01-24 21:01:55 +01:00
|
|
|
ItemType type;
|
|
|
|
std::string text;
|
|
|
|
std::vector<Item*> item_table;
|
|
|
|
std::vector<std::string> directives;
|
|
|
|
|
|
|
|
Item();
|
2007-01-26 20:29:36 +01:00
|
|
|
Item(const Item & i);
|
|
|
|
Item & operator=(const Item & i);
|
|
|
|
void CopyItemTable(const Item & i);
|
2007-01-24 21:01:55 +01:00
|
|
|
~Item();
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
Item * AddItem(const Item * porg = 0);
|
|
|
|
Item * AddItem(const Item & porg);
|
|
|
|
void ClearItems();
|
2007-01-24 21:01:55 +01:00
|
|
|
ItemType LastItemType();
|
2007-01-26 20:29:36 +01:00
|
|
|
void DeleteLastItem();
|
|
|
|
void Clear();
|
2007-01-24 21:01:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Item item_root;
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char * itext;
|
|
|
|
std::string directory;
|
|
|
|
|
|
|
|
|
|
|
|
bool CheckFileName(const char * name);
|
|
|
|
std::string ReadFile(const char * name);
|
|
|
|
|
|
|
|
int ReadCharInText();
|
|
|
|
void SkipWhiteCharacters();
|
|
|
|
|
|
|
|
std::string ReadDirective();
|
2008-12-10 02:46:10 +01:00
|
|
|
std::string ReadString(bool skip_first_quote = false);
|
2007-01-26 20:29:36 +01:00
|
|
|
std::string ReadDirectiveOrString();
|
|
|
|
|
|
|
|
void CreateTreeReadItemDirectiveCheckEnding(Item & item);
|
|
|
|
|
|
|
|
void ReadDirectiveIfany(Item & item);
|
2008-12-23 21:08:11 +01:00
|
|
|
void ReadDirectiveIfno(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void ReadDirectiveIfone(Item & item);
|
|
|
|
void ReadDirectiveIs(Item & item);
|
|
|
|
void ReadDirectiveIfindex(Item & item);
|
|
|
|
void ReadDirectiveFor(Item & item);
|
2007-01-27 14:52:18 +01:00
|
|
|
void ReadDirectiveComment(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void ReadDirectiveInclude(Item & item);
|
2008-12-10 02:46:10 +01:00
|
|
|
void ReadDirectiveDef(Item & item);
|
2007-01-27 14:52:18 +01:00
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
}; // Pattern
|
|
|
|
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
struct Info
|
|
|
|
{
|
2008-12-10 02:46:10 +01:00
|
|
|
// this variables you can set in your function
|
|
|
|
std::ostringstream & out;
|
|
|
|
std::string out_string;
|
2007-01-26 20:29:36 +01:00
|
|
|
bool result;
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
// this is set by Generator
|
|
|
|
// normally is 0
|
|
|
|
// in a [for] statement it indicates the number of the current iteration (the first is 0)
|
2007-01-26 20:29:36 +01:00
|
|
|
int iter;
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
Info(std::ostringstream & o);
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
bool IsTrue() const;
|
|
|
|
bool IsFalse() const;
|
2007-01-24 21:01:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Functions
|
2007-01-26 20:29:36 +01:00
|
|
|
{
|
2008-12-10 02:46:10 +01:00
|
|
|
public:
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
typedef void (*UserFunction)(Info &);
|
2008-12-10 02:46:10 +01:00
|
|
|
enum Type { function, variable };
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
struct Function
|
2007-01-26 20:29:36 +01:00
|
|
|
{
|
2008-12-10 02:46:10 +01:00
|
|
|
Type type;
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
UserFunction user_function;
|
2008-12-10 02:46:10 +01:00
|
|
|
std::string variable;
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
int iter;
|
2008-12-10 02:46:10 +01:00
|
|
|
bool is_for; // true if is used by a [for] statement
|
|
|
|
bool is_running; // true if this function (if is) is currently running
|
|
|
|
|
|
|
|
Function();
|
2007-01-26 20:29:36 +01:00
|
|
|
};
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
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_table;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Generator
|
|
|
|
{
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
Generator(std::ostringstream &, Pattern & data, Functions & functions);
|
|
|
|
void Generate();
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
std::ostringstream & output_stream;
|
|
|
|
Pattern & pattern;
|
|
|
|
Functions & functions;
|
|
|
|
|
|
|
|
Info info1, info2;
|
|
|
|
int loop;
|
|
|
|
|
|
|
|
bool Find(const std::string & key, Functions::Function ** function);
|
|
|
|
|
|
|
|
|
|
|
|
bool Call(const std::string & name, Info & info, Functions::Function ** pfun = 0);
|
|
|
|
void Call(Functions::Function * function, Info & info);
|
|
|
|
|
|
|
|
void CallUserFunction(Functions::Function * function, Info & info);
|
|
|
|
void CallVariable(Functions::Function * function, Info & info);
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeTextIf_go(Pattern::Item & item, bool result);
|
2008-12-10 02:46:10 +01:00
|
|
|
bool MakeTextIfindexnumber(Pattern::Item & item, Functions::Function * function, bool & result);
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeTextIfany(Pattern::Item & item);
|
2008-12-23 21:08:11 +01:00
|
|
|
void MakeTextIfno(Pattern::Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeTextIfone(Pattern::Item & item);
|
|
|
|
void MakeTextIfindex(Pattern::Item & item);
|
|
|
|
void MakeTextFor(Pattern::Item & item);
|
|
|
|
void MakeTextContainer(Pattern::Item & item);
|
|
|
|
void MakeTextNormal(Pattern::Item & item);
|
|
|
|
void MakeTextIs(Pattern::Item & item);
|
2008-12-10 02:46:10 +01:00
|
|
|
void MakeTextDefine(Pattern::Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeText(Pattern::Item & item);
|
|
|
|
|
|
|
|
}; // Generator
|
|
|
|
|
|
|
|
} // namespace Ezc
|
|
|
|
|
|
|
|
|
2007-01-24 21:01:55 +01:00
|
|
|
#endif
|