A template engine for C++ applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

261 lines
7.4 KiB

/*
* This file is a part of EZC -- Easy templating in C++
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2007-2011, Tomasz Sowa
* 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.
*/
#ifndef headerfile_ezc_pattern
#define headerfile_ezc_pattern
#include <string>
#include "item.h"
#include "functions.h"
#include "stringconv.h"
#include "utf8.h"
#ifdef EZC_USE_WINIX_LOGGER
#include "core/log.h"
#endif
namespace Ezc
{
class Pattern
{
public:
Pattern();
/*!
clearing only the tree
*/
void Clear();
// 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 Directory(const wchar_t * dir, const wchar_t * dir2 = 0);
void Directory(const std::wstring & dir);
void Directory(const std::wstring & dir, const std::wstring & dir2);
void ParseFile(const char * file_name);
void ParseFile(const std::string & file_name);
void ParseFile(const wchar_t * file_name);
void ParseFile(const std::wstring & file_name);
void ParseString(const char * str);
void ParseString(const std::string & str);
void ParseString(const wchar_t * str);
void ParseString(const std::wstring & str);
void AllowInclude(bool allow);
void DeleteWhiteTextItems(bool del);
void SetIncludeMax(int include_max);
// files and strings (only char* and std::string) are treated
// as UTF-8
void UTF8(bool utf8);
void SetCommentary(const char * com_start, const char * com_stop);
void SetCommentary(const std::string & com_start, const std::string & com_stop);
void SetCommentary(const wchar_t * com_start, const wchar_t * com_stop);
void SetCommentary(const std::wstring & com_start, const std::wstring & com_stop);
void CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg = 0);
static bool IsWhite(wchar_t c);
Item item_root;
template<class StreamType>
void CacheFunctions(Functions<StreamType> & fun)
{
CacheFunctions(item_root, fun);
}
void ClearCache();
private:
// the main pointer to the parsed text
const wchar_t * 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::wstring directory, directory2;
bool input_as_utf8;
int include_level, include_level_max;
std::wstring commentary_start, commentary_stop;
// temporary content for ParseString(const char*) method
std::wstring string_content;
// temporary object for a file name
std::wstring file_name;
// temporary object for a file name
std::string afile_name;
// temporary object for a EZC function's parameter
std::wstring temp_param;
// temporary object for a EZC function
Item::Function temp_function;
void ReadFile(const std::wstring & name, std::wstring & result);
void ReadFile(const wchar_t * name, std::wstring & result);
bool HasFileAtBeginning(const wchar_t * path, const wchar_t * file);
bool IsFileCorrect(const wchar_t * name);
bool ReadFileFromDir(const std::wstring & dir, const wchar_t * name, std::wstring & result);
void ReadFile(std::ifstream & file, std::wstring & result);
void ReadFileContent(std::ifstream & file, std::wstring & result);
int ReadCharInText();
void SkipWhite();
void CheckWhiteAndDelete(std::wstring & s);
bool IsNameChar(wchar_t c);
bool IsDigit(wchar_t c);
bool IsPositiveNumber(const std::wstring & str);
bool ReadName(std::wstring & name);
bool ReadString(std::wstring & str);
bool ReadParams(Item::Function & function);
bool ReadFunction(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 ReadDirectiveFilter(Item & item);
void ReadDirectiveNormal(const std::wstring & 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);
template<class StreamType>
void CacheFunctions(Item & item, Functions<StreamType> & fun)
{
typename Functions<StreamType>::Function * ezc_fun;
// one exception (if_index is putting its argument on the functions stack)
if( item.type != Item::item_ifindex )
{
for(size_t f=0; f < item.functions.size() ; ++f)
{
if( fun.Find(item.functions[f].name, &ezc_fun) )
{
item.functions[f].fun_cache = ezc_fun;
}
else
{
item.functions[f].fun_cache = 0;
#ifdef EZC_USE_WINIX_LOGGER
log << log1 << "Ezc: unknown function: " << item.functions[f].name << logend;
#endif
}
}
}
for(size_t i=0; i < item.item_tab.size() ; ++i)
CacheFunctions(*item.item_tab[i], fun);
}
void ClearCache(Item & item);
}; // class Pattern
} // namespace Ezc
#endif