1 /******************************************************************************
2 *
3 * Copyright (C) 2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 //-------------------------------------------------
15
16 type macroitem
17 {
18 ustr value // value
19 uint dynamic // dynamic function
20 }
21
22 type macro< index = ustr >
23 {
24 ushort syschar
25 hash vals of macroitem
26 }
27
28 //-------------------------------------------------
29
30 method macro macro.init
31 {
32 this.syschar = '#'
33 this.vals.ignorecase()
34 return this
35 }
36
37 //-------------------------------------------------
38
39 method uint macro.index( str key )
40 {
41 uint it
42
43 it as this.vals[ key ]
44 if it.dynamic : it.dynamic->func( key, it )
45 return &( it.value )
46 }
47
48 //-------------------------------------------------
49
50 method macro.dynamic( str key, uint dynfunc )
51 {
52 this.vals[ key ].dynamic = dynfunc
53 }
54
55 //-------------------------------------------------
56
57 method uint macro.ismacro( str name )
58 {
59 return this.vals.find( name ) != 0
60 }
61
62 //-------------------------------------------------
63
64 method ustr macro.replace( ustr result, uint level )
65 {
66 uint i
67 ustr val
68
69 if !*result: return result
70
71 while ( i = result.findch( i, this.syschar )) < *result
72 {
73 uint end
74 str key
75
76 end = result.findch( ++i, this.syschar )
77 if end >= *result : break
78 key = str( val.substr( result, i, end - i ))
79 if this.vals.find( key ) && (val = this[ key ]) != result
80 {
81 if level < 10 : this.replace( val, level + 1 )
82 result.replace( i - 1, end - i + 2, val )
83 i += *val - 1
84 }
85 }
86 return result
87 }
88
89 method ustr macro.replace( ustr result )
90 {
91 return this.replace( result, 0 )
92 }
93
94 //-------------------------------------------------
95
96 method str macro.replace( str result )
97 {
98 return result = str( this.replace( ustr( result )))
99 }
100
101 //-------------------------------------------------
102
103 method ustr macro.get( str name, ustr result )
104 {
105 result.clear()
106 if this.vals.find( name ) : this.replace( result = this[ name ] )
107
108 return result
109 }
110
111 //-------------------------------------------------
112
113 method str macro.get( str name, str result )
114 {
115 ustr uval
116
117 return result = str( this.get( name, uval ))
118 }
119
120 //-------------------------------------------------
121
122 method int macro.getint( str name )
123 {
124 ustr stemp
125
126 return int( str( this.get( name, stemp )))
127 }
128
129 //-------------------------------------------------
130
131 method long macro.getlong( str name )
132 {
133 ustr stemp
134
135 return long( str( this.get( name, stemp )))
136 }
137
138 //-------------------------------------------------
139
140 method macro.set( str name, int val )
141 {
142 this[ name ] = ustr( str( val ))
143 }
144
145 //-------------------------------------------------
146
147 method macro.set( str name, str value )
148 {
149 this[ name ] = ustr( value )
150 }
151
152 //-------------------------------------------------
153
154 method macro.setutf8( str name, str value )
155 {
156 this[ name ].fromutf8( value )
157 }
158
159 //-------------------------------------------------
160
161 method macro.set( str name, long val )
162 {
163 this[ name ] = ustr( str( val ))
164 }
165
166 //-------------------------------------------------
167
168 method macro.setchar( uint syschar )
169 {
170 this.syschar = syschar
171 }
172
173 //-------------------------------------------------
174 /*
175 method uint macro.load( str macrolist )
176 {
177 uint i count
178 arr data of str
179
180 macrolist.split( data, 0xA, $SPLIT_NOSYS )
181 fornum i, *data
182 {
183 arr vals of str
184 data[i].split( vals, '=', $SPLIT_NOSYS | $SPLIT_FIRST )
185 if *vals > 1
186 {
187 this[ vals[0]] = vals[1]
188 count++
189 }
190 }
191 return count
192 }
193 */
194 //-------------------------------------------------
195
196 operator macro +=( macro left, macro right )
197 {
198 foreach cur, right.vals.keys
199 {
200 left[ cur ] = right[ cur ]
201 }
202 return left
203 }
204
205 //-------------------------------------------------
206 /*
207 func main<main>
208 {
209 ustr result
210 macro macros
211
212 macros["ve"] = ustr("ве")
213 macros["test"] = ustr("Про#ve#рка")
214 result = ustr("#test# esese #test# 333 #test#?")
215 macros.replace( result )
216 print( "val=\( hex2stru( ustr("#").ptr()->uint ))\n")
217 congetch( str( result ))
218 }
219 */
220