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 macro< index = ustr >
17 {
18 ushort syschar
19 hash vals of ustr
20 }
21
22 //-------------------------------------------------
23
24 method macro macro.init
25 {
26 this.syschar = '#'
27 this.vals.ignorecase()
28 return this
29 }
30
31 //-------------------------------------------------
32
33 method uint macro.index( str key )
34 {
35 return &( this.vals[ key ] )
36 }
37
38 //-------------------------------------------------
39
40 method uint macro.ismacro( str name )
41 {
42 return this.vals.find( name ) != 0
43 }
44
45 //-------------------------------------------------
46
47 method ustr macro.replace( ustr result )
48 {
49 uint i
50 ustr val
51
52 while ( i = result.findch( i, this.syschar )) < *result
53 {
54 uint end
55 str key
56
57 end = result.findch( ++i, this.syschar )
58 if end >= *result : break
59 key = str( val.substr( result, i, end - i ))
60 if this.vals.find( key )
61 {
62 this.replace( val = this[ key ] )
63 result.replace( i - 1, end - i + 2, val )
64 i += *val - 1
65 }
66 }
67 return result
68 }
69
70 //-------------------------------------------------
71
72 method str macro.replace( str result )
73 {
74 return result = str( this.replace( ustr( result )))
75 }
76
77 //-------------------------------------------------
78
79 method ustr macro.get( str name, ustr result )
80 {
81 result.clear()
82 if this.vals.find( name ) : this.replace( result = this[ name ] )
83
84 return result
85 }
86
87 //-------------------------------------------------
88
89 method int macro.getint( str name )
90 {
91 ustr stemp
92
93 return int( str( this.get( name, stemp )))
94 }
95
96 //-------------------------------------------------
97
98 method macro.set( str name, int val )
99 {
100 this[ name ] = ustr( str( val ))
101 }
102
103 //-------------------------------------------------
104
105 method macro.set( str name, str value )
106 {
107 this[ name ] = ustr( value )
108 }
109
110 //-------------------------------------------------
111
112 method macro.setchar( uint syschar )
113 {
114 this.syschar = syschar
115 }
116
117 //-------------------------------------------------
118 /*
119 method uint macro.load( str macrolist )
120 {
121 uint i count
122 arr data of str
123
124 macrolist.split( data, 0xA, $SPLIT_NOSYS )
125 fornum i, *data
126 {
127 arr vals of str
128 data[i].split( vals, '=', $SPLIT_NOSYS | $SPLIT_FIRST )
129 if *vals > 1
130 {
131 this[ vals[0]] = vals[1]
132 count++
133 }
134 }
135 return count
136 }
137 */
138 //-------------------------------------------------
139
140 operator macro +=( macro left, macro right )
141 {
142 foreach cur, right.vals.keys
143 {
144 left[ cur ] = right[ cur ]
145 }
146 return left
147 }
148
149 //-------------------------------------------------
150 /*
151 func main<main>
152 {
153 ustr result
154 macro macros
155
156 macros["ve"] = ustr("ве")
157 macros["test"] = ustr("Про#ve#рка")
158 result = ustr("#test# esese #test# 333 #test#?")
159 macros.replace( result )
160 print( "val=\( hex2stru( ustr("#").ptr()->uint ))\n")
161 congetch( str( result ))
162 }
163 */
Edit