source\example\autoformat\autoformat.g
1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2006, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project.
5 * http://www.gentee.com
6 *
7 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
8 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
9 * ACCEPTANCE OF THE AGREEMENT.
10 *
11 * ID: formatfile 20.10.06 0.0.A.
12 *
13 * Author: Aleksandr Antypenko ( santy )
14 *
15 * Summary: Function of utility autoformatting Gentee sources
16 *
17 ******************************************************************************/
18
19 include
20 {
21 $"..\..\lib\lex\lex.g"
22 "lexfgentee.g"
23 }
24
25 type puncmult < protected >
26 {
27 uint uMValue
28 uint startPos
29 }
30
31 operator puncmult =( puncmult left right )
32 {
33 left.uMValue = right.uMValue
34 left.startPos = right.startPos
35 return left
36 }
37
38 /******************************************************************************
39 * Function formatfile -
40 *
41 * Parameters:
42 * str namefile - addrees of flinfo structure
43 * int intend - path and pattern for search files
44 * uint lenthline - max length of line
45 * byte bCreateNew - 1- create bak file, 0- overwrite existent file
46 *
47 * Return :
48 * 1 - formatting file
49 *
50 ******************************************************************************/
51 func int formatfile( str namefile, uint indend, uint lenthLine, byte bCreateNew )
52 {
53 str inBuffer, strOutFile, strLine, stemp
54 arrout outArr
55 uint lex, off, i
56 uint igt // The current gtitem
57 uint startPos, sMainValue, countMult = 0
58 byte bNewLine = 0, inBlock = 0, bDot = 0, bDugky = 0
59 stack puncStk of puncmult
60 puncmult pMult
61
62
63 outArr.isize = sizeof( lexitem )
64 if !( fileexist( namefile ) )
65 {
66 print( "File not found \n" )
67 return 0 ;
68 }
69 inBuffer.read( namefile )
70 lex = lex_init( 0, lexfgentee.ptr( ) )
71 gentee_lex( inBuffer -> buf, lex, outArr )
72 //print("------------------------ddd--\n")
73 //
74 off = outArr.data.ptr( )
75 str stemp1
76 fornum i = 0, * outArr
77 {
78 uint li
79
80 li as off -> lexitem
81
82 stemp1.clear( )
83
84 //if (li.ltype != $FG_UNKNOWN)
85 //{
86 //print("type=\( hex2stru("", li.ltype )) pos = \(li.pos) len=\(li.len ) \(hex2stru("", li.value )) \n")
87 //print(stemp.substr( inBuffer, li.pos, li.len)+" \n")
88 if( li.ltype == $FG_NAME )
89 {
90 if( li.value == $KEY_INCLUDE || li.value == $KEY_FUNC || li.value == $KEY_GLOBAL
91 || li.value == $KEY_DEFINE || li.value == $KEY_IMPORT || li.value == $KEY_METHOD
92 || li.value == $KEY_OPERATOR || li.value == $KEY_TYPE || li.value == $KEY_IFDEF )
93 {
94 startPos = * strLine
95 sMainValue = li.value
96 bNewLine = 0
97 strLine.fillspacer( startPos )
98 strLine += stemp.substr( inBuffer, li.pos, li.len )
99 }
100 else
101 {
102 if( bNewLine )
103 {
104 strLine.fillspacer( * strLine + startPos )
105 strLine.fillspacer( * strLine + indend )
106 //strLine+="DDDD"
107 strLine += stemp.substr( inBuffer, li.pos, li.len )
108 bNewLine = 0
109 }
110 else
111 {
112 str sTmp = ""
113 if( bDot ) : sTmp = "" ; bDot = 0
114 else : sTmp = " "
115 if( strLine [ * strLine + startPos - 1 ] == 0x7B )
116 {
117 strLine += "\n"
118 strLine.fillspacer( * strLine + startPos )
119 strLine += sTmp + stemp.substr( inBuffer, li.pos, li.len )
120 strOutFile += strLine
121 strLine.clear( )
122 bNewLine = 1
123 }
124 else
125 {
126 strLine += sTmp + stemp.substr( inBuffer, li.pos, li.len )
127 }
128 //bNewLine =
129 }
130 }
131 }
132 elif( li.ltype == $FG_SPACE || li.ltype == $FG_TAB )
133 {
134 stemp.clear( )
135 off += sizeof( lexitem )
136 continue
137 }
138 elif( li.ltype == $FG_NUMBER || li.ltype == $FG_MACRO )
139 {
140 if( bNewLine ) : bNewLine = 0
141 strLine += " " + stemp.substr( inBuffer, li.pos, li.len )
142 }
143 elif( li.ltype == $FG_OPERCHAR )
144 {
145 str stmp = ""
146 if( li.value == 0x2E ) : stmp = "" ; bDot = 1
147 elif(( li.value == 0x2B2B || li.value == 0x2D2D || li.value == 0x7C7C || li.value == 0x2626 )
148 && bNewLine )
149 {
150 stmp.fillspacer( indend )
151 strLine.fillspacer( * strLine + startPos )
152 bNewLine = 0
153 }
154 elif( li.value == 0x7B25 )
155 {
156 ++ countMult
157 if( countMult > 1 )
158 {
159 puncmult pMultTmp
160 pMultTmp.uMValue = sMainValue
161 pMultTmp.startPos = startPos
162 puncStk.push( ) -> puncmult = pMultTmp
163 startPos = startPos + indend
164 }
165 stmp = " "
166 }
167 else : stmp = " "
168 strLine += stmp + stemp.substr( inBuffer, li.pos, li.len )
169 }
170 elif( li.ltype == $FG_LINECOMMENT || li.ltype == $FG_COMMENT )
171 {
172 str stmp = ""
173 strLine.fillspacer( * strLine + startPos )
174 if( li.ltype == $FG_LINECOMMENT ) : stmp.fillspacer( indend )
175 strLine += stmp + stemp.substr( inBuffer, li.pos, li.len )
176 }
177 elif( li.ltype == $FG_STRING || li.ltype == $FG_MACROSTR )
178 {
179 str sTmp = ""
180 if( bNewLine )
181 {
182 strLine.fillspacer( * strLine + startPos + indend ) ;
183 //sTmp = ?( li.ltype == $FG_MACROSTR, " ", "" )
184 }
185 else : sTmp = ?( li.ltype == $FG_STRING, " ", "" )
186 strLine += sTmp + stemp.substr( inBuffer, li.pos, li.len )
187 }
188 elif( li.ltype == $FG_UNKNOWN )
189 {
190 strLine += " " + stemp.substr( inBuffer, li.pos, li.len )
191 }
192 elif( li.ltype == $FG_SYSCHAR )
193 {
194 if( li.value == 0x28 || li.value == 0x29 || li.value == 0x2C )
195 {
196 if( bNewLine ) : strLine.fillspacer( * strLine )
197 strLine += ?( li.value == 0x29, " ", "" ) + stemp.substr( inBuffer, li.pos, li.len )
198 }
199 elif( li.value == 0x7B )
200 {
201 ++ countMult
202 if( countMult > 1 )
203 {
204 puncmult pMultTmp
205 pMultTmp.uMValue = sMainValue
206 pMultTmp.startPos = startPos
207 puncStk.push( ) -> puncmult = pMultTmp
208 startPos = startPos + indend
209 }
210 bDugky = 1
211 // add to stack
212 if( bNewLine )
213 {
214 strLine.fillspacer( * strLine + startPos )
215 strLine += stemp.substr( inBuffer, li.pos, li.len )
216 //
217 //strOutFile += strLine+"\n"
218 //strLine.clear()
219 bNewLine = 0
220 inBlock = 1 //?( li.value == 0x7B,1,0)
221 }
222 else
223 {
224 strLine += "\n"
225 strLine.fillspacer( * strLine + startPos )
226 strLine += stemp.substr( inBuffer, li.pos, li.len )
227 strOutFile += strLine + "\n"
228 strLine.clear( )
229 inBlock = 1
230 bNewLine = 1
231 }
232 }
233 elif( li.value == 0x7D )
234 {
235 if( bNewLine )
236 {
237 strLine.fillspacer( * strLine + startPos )
238 //print (strLine+"----5 \n")
239 strLine += stemp.substr( inBuffer, li.pos, li.len )
240 strOutFile += strLine + " " ;
241 strLine.clear( )
242 inBlock = 0
243 bNewLine = 0
244 }
245 else
246 {
247 strLine += "\n"
248 strLine.fillspacer( * strLine + startPos )
249 //print (strLine+"----6 \n")
250 strLine += stemp.substr( inBuffer, li.pos, li.len )
251 strOutFile += strLine
252 strLine.clear( )
253 inBlock = 0
254 bNewLine = 1
255 }
256 bDugky = 0
257 if( countMult > 1 )
258 {
259 puncmult pMultTmp
260 pMultTmp = puncStk.top( ) -> puncmult
261 sMainValue = pMultTmp.uMValue
262 startPos = pMultTmp.startPos
263 puncStk.pop( )
264 }
265 -- countMult
266 }
267 else : strLine += " " + stemp.substr( inBuffer, li.pos, li.len )
268
269 }
270 elif( li.ltype == $FG_LINE )
271 {
272 strLine += stemp.substr( inBuffer, li.pos, li.len )
273 strOutFile += strLine
274 //print(strLine+"----111111 \n")
275 bNewLine = 1
276 strLine.clear( )
277 }
278 //}
279 stemp.clear( )
280 off += sizeof( lexitem )
281 }
282 if( bCreateNew )
283 {
284 str snewfile
285 snewfile.fsetext( namefile, "bak" )
286 if !( copyfile( namefile, snewfile ) ) : print( "Error create backup file. \n" ) ; return 0
287 else : print( "Backup file -- " + snewfile + " -- was created.\n" )
288 }
289 strOutFile.write( namefile )
290 //
291 lex_delete( lex )
292 //print("--------------------------\n")
293 //congetch("Press any key...")
294 return 1
295 }
296
297