1 |
/* |
2 |
* This file is a part of EZC -- Easy templating in C++ |
3 |
* and is distributed under the (new) BSD licence. |
4 |
* Author: Tomasz Sowa <t.sowa@ttmath.org> |
5 |
*/ |
6 |
|
7 |
/* |
8 |
* Copyright (c) 2007-2014, Tomasz Sowa |
9 |
* All rights reserved. |
10 |
* |
11 |
* Redistribution and use in source and binary forms, with or without |
12 |
* modification, are permitted provided that the following conditions are met: |
13 |
* |
14 |
* * Redistributions of source code must retain the above copyright notice, |
15 |
* this list of conditions and the following disclaimer. |
16 |
* |
17 |
* * Redistributions in binary form must reproduce the above copyright |
18 |
* notice, this list of conditions and the following disclaimer in the |
19 |
* documentation and/or other materials provided with the distribution. |
20 |
* |
21 |
* * Neither the name Tomasz Sowa nor the names of contributors to this |
22 |
* project may be used to endorse or promote products derived |
23 |
* from this software without specific prior written permission. |
24 |
* |
25 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
26 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
27 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
28 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
29 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
30 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
31 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
32 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
33 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
34 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
35 |
* THE POSSIBILITY OF SUCH DAMAGE. |
36 |
*/ |
37 |
|
38 |
|
39 |
#ifndef headerfile_ezc_pattern |
40 |
#define headerfile_ezc_pattern |
41 |
|
42 |
#include <string> |
43 |
#include "item.h" |
44 |
#include "functions.h" |
45 |
#include "stringconv.h" |
46 |
#include "utf8/utf8.h" |
47 |
|
48 |
#ifdef EZC_USE_WINIX_LOGGER |
49 |
#include "core/log.h" |
50 |
#endif |
51 |
|
52 |
|
53 |
namespace Ezc |
54 |
{ |
55 |
|
56 |
|
57 |
class Pattern |
58 |
{ |
59 |
public: |
60 |
|
61 |
Pattern(); |
62 |
|
63 |
/*! |
64 |
clearing only the tree |
65 |
*/ |
66 |
void Clear(); |
67 |
|
68 |
|
69 |
// first we're trying to read a file from directory dir |
70 |
// if there is no such a file there then we try read from dir2 |
71 |
// (the second dir2 can be empty - it will not be used) |
72 |
void Directory(const char * dir, const char * dir2 = 0); |
73 |
void Directory(const std::string & dir); |
74 |
void Directory(const std::string & dir, const std::string & dir2); |
75 |
|
76 |
void Directory(const wchar_t * dir, const wchar_t * dir2 = 0); |
77 |
void Directory(const std::wstring & dir); |
78 |
void Directory(const std::wstring & dir, const std::wstring & dir2); |
79 |
|
80 |
void ParseFile(const char * file_name); |
81 |
void ParseFile(const std::string & file_name); |
82 |
|
83 |
void ParseFile(const wchar_t * file_name); |
84 |
void ParseFile(const std::wstring & file_name); |
85 |
|
86 |
|
87 |
void ParseString(const char * str); |
88 |
void ParseString(const std::string & str); |
89 |
void ParseString(const wchar_t * str); |
90 |
void ParseString(const std::wstring & str); |
91 |
|
92 |
void AllowInclude(bool allow); |
93 |
void DeleteWhiteTextItems(bool del); |
94 |
void SetIncludeMax(int include_max); |
95 |
|
96 |
// files and strings (only char* and std::string) are treated |
97 |
// as UTF-8 |
98 |
void UTF8(bool utf8); |
99 |
|
100 |
void SetCommentary(const char * com_start, const char * com_stop); |
101 |
void SetCommentary(const std::string & com_start, const std::string & com_stop); |
102 |
void SetCommentary(const wchar_t * com_start, const wchar_t * com_stop); |
103 |
void SetCommentary(const std::wstring & com_start, const std::wstring & com_stop); |
104 |
|
105 |
void CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg = 0); |
106 |
static bool IsWhite(wchar_t c); |
107 |
|
108 |
Item item_root; |
109 |
|
110 |
template<class StreamType> |
111 |
void CacheFunctions(Functions<StreamType> & fun) |
112 |
{ |
113 |
CacheFunctions(item_root, fun); |
114 |
} |
115 |
|
116 |
void ClearCache(); |
117 |
|
118 |
private: |
119 |
|
120 |
// the main pointer to the parsed text |
121 |
const wchar_t * itext; |
122 |
|
123 |
|
124 |
// allowing include tag |
125 |
// default: true |
126 |
bool allow_include; |
127 |
|
128 |
// if true all text-items which have only white characters (with new lines as well) |
129 |
// will be deleted - useful in *.txt templates |
130 |
// this not actually delete the whole item but only the string |
131 |
// the item will be present with an empty string |
132 |
// default: false |
133 |
bool delete_white_text_items; |
134 |
|
135 |
// first we're trying to read a file from 'directory' |
136 |
// if there is no such a file there then we try read from 'directory2' |
137 |
// we read from these directories only if they are not empty |
138 |
std::wstring directory, directory2; |
139 |
|
140 |
|
141 |
bool input_as_utf8; |
142 |
|
143 |
int include_level, include_level_max; |
144 |
|
145 |
|
146 |
|
147 |
std::wstring commentary_start, commentary_stop; |
148 |
|
149 |
|
150 |
// temporary content for ParseString(const char*) method |
151 |
std::wstring string_content; |
152 |
|
153 |
// temporary object for a file name |
154 |
std::wstring file_name; |
155 |
|
156 |
// temporary object for a file name |
157 |
std::string afile_name; |
158 |
|
159 |
// temporary object for a EZC function's parameter |
160 |
std::wstring temp_param; |
161 |
|
162 |
// temporary object for a EZC function |
163 |
Item::Function temp_function; |
164 |
|
165 |
|
166 |
|
167 |
void ReadFile(const std::wstring & name, std::wstring & result); |
168 |
void ReadFile(const wchar_t * name, std::wstring & result); |
169 |
bool HasFileAtBeginning(const wchar_t * path, const wchar_t * file); |
170 |
bool IsFileCorrect(const wchar_t * name); |
171 |
bool ReadFileFromDir(const std::wstring & dir, const wchar_t * name, std::wstring & result); |
172 |
void ReadFile(std::ifstream & file, std::wstring & result); |
173 |
void ReadFileContent(std::ifstream & file, std::wstring & result); |
174 |
|
175 |
int ReadCharInText(); |
176 |
void SkipWhite(); |
177 |
void CheckWhiteAndDelete(std::wstring & s); |
178 |
|
179 |
bool IsNameChar(wchar_t c); |
180 |
bool IsDigit(wchar_t c); |
181 |
bool IsPositiveNumber(const std::wstring & str); |
182 |
|
183 |
bool ReadName(std::wstring & name); |
184 |
bool ReadString(std::wstring & str); |
185 |
bool ReadParams(Item::Function & function); |
186 |
bool ReadFunction(Item::Function & function); |
187 |
bool ReadFunctions(Item & item); |
188 |
|
189 |
void ReadDirectiveIf(Item & item); |
190 |
void ReadDirectiveIfno(Item & item); |
191 |
void ReadDirectiveIfany(Item & item); |
192 |
void ReadDirectiveIfone(Item & item); |
193 |
void ReadDirectiveIfanyno(Item & item); |
194 |
void ReadDirectiveIfoneno(Item & item); |
195 |
void ReadDirectiveIs(Item & item); |
196 |
void ReadDirectiveIsno(Item & item); |
197 |
void ReadDirectiveIfindex(Item & item); |
198 |
void ReadDirectiveFor(Item & item); |
199 |
void ReadDirectiveComment(Item & item); |
200 |
void ReadDirectiveInclude(Item & item); |
201 |
void ReadDirectiveDef(Item & item); |
202 |
void ReadDirectiveFilter(Item & item); |
203 |
void ReadDirectiveEzc(Item & item); |
204 |
void ReadDirectiveNormal(const std::wstring & name, Item & item); |
205 |
|
206 |
void CreateTreeReadItemDirectiveCheckEnding(Item & item); |
207 |
void CreateTreeReadItemDirective(Item & item); |
208 |
void CreateTreeReadItemText(Item & item); |
209 |
bool CreateTreeReadItem(Item & item); |
210 |
void CreateTreeReadIf(Item & item); |
211 |
void CreateTreeReadFor(Item & item); |
212 |
void CreateTree(Item & item); |
213 |
void CreateTreeReadInclude(Item & item); |
214 |
void CreateTreeReadIncludeSkipAllowFlag(Item & item); |
215 |
|
216 |
|
217 |
template<class StreamType> |
218 |
void CacheFunctions(Item & item, Functions<StreamType> & fun) |
219 |
{ |
220 |
typename Functions<StreamType>::Function * ezc_fun; |
221 |
|
222 |
// one exception (if_index is putting its argument on the functions stack) |
223 |
if( item.type != Item::item_ifindex ) |
224 |
{ |
225 |
for(size_t f=0; f < item.functions.size() ; ++f) |
226 |
{ |
227 |
if( fun.Find(item.functions[f].name, &ezc_fun) ) |
228 |
{ |
229 |
item.functions[f].fun_cache = ezc_fun; |
230 |
} |
231 |
else |
232 |
{ |
233 |
item.functions[f].fun_cache = 0; |
234 |
|
235 |
#ifdef EZC_USE_WINIX_LOGGER |
236 |
Winix::log << Winix::log1 << "Ezc: unknown function: " << item.functions[f].name << Winix::logend; |
237 |
#endif |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
for(size_t i=0; i < item.item_tab.size() ; ++i) |
243 |
CacheFunctions(*item.item_tab[i], fun); |
244 |
} |
245 |
|
246 |
|
247 |
void ClearCache(Item & item); |
248 |
|
249 |
}; // class Pattern |
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
} // namespace Ezc |
260 |
|
261 |
|
262 |
#endif |