]>
Commit | Line | Data |
---|---|---|
cecfc5e7 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: No names yet. | |
3 | // Purpose: Contrib. demo | |
4 | // Author: Aleksandras Gluchovas | |
5 | // Modified by: | |
6 | // Created: 22/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Aleskandars Gluchovas | |
8ad74db3 | 9 | // Licence: wxWindows licence |
cecfc5e7 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // For compilers that support precompilation, includes "wx/wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
d5939a20 | 23 | #ifndef __DARWIN__ |
ad053960 GD |
24 | # include <malloc.h> |
25 | #endif | |
cecfc5e7 VZ |
26 | #include <string.h> |
27 | #include <memory.h> | |
28 | ||
29 | #include <stdio.h> // import sprintf() (for doubles) | |
30 | #include <stdlib.h> // import atoi() (for integers) | |
31 | ||
32 | #include "scriptbinder.h" | |
33 | ||
34 | // helper functions | |
35 | ||
36 | static size_t log2(size_t nr) | |
37 | { | |
8ad74db3 WS |
38 | size_t tmp = 0; |
39 | while (nr >= 2 ) | |
40 | { | |
41 | nr /= 2; | |
42 | ++tmp; | |
43 | } | |
44 | ||
45 | return tmp; | |
cecfc5e7 VZ |
46 | } |
47 | ||
48 | /***** Implementation for class ScriptStream *****/ | |
49 | ||
50 | ScriptStream::ScriptStream() | |
2af95167 WS |
51 | : m_pBuf(0), |
52 | m_Size(0), | |
53 | m_Capacity(0) | |
cecfc5e7 VZ |
54 | {} |
55 | ||
56 | ScriptStream::~ScriptStream() | |
57 | { | |
2af95167 | 58 | if ( m_pBuf ) delete m_pBuf; |
cecfc5e7 VZ |
59 | } |
60 | ||
61 | void ScriptStream::WriteBytes( const void* srcBuf, size_t count ) | |
62 | { | |
8ad74db3 | 63 | if ( !count ) return; |
cecfc5e7 | 64 | |
8ad74db3 | 65 | // increase the capacity if necessary |
2af95167 | 66 | if ( m_Size + count > m_Capacity ) |
8ad74db3 | 67 | { |
2af95167 WS |
68 | m_Capacity = |
69 | ( 0x2 << (log2( m_Size + count ) + 1 ) ); | |
cecfc5e7 | 70 | |
2af95167 | 71 | if ( m_Capacity < 128 ) m_Capacity = 128; |
cecfc5e7 | 72 | |
2af95167 | 73 | char* oldBuf = m_pBuf; |
cecfc5e7 | 74 | |
2af95167 | 75 | m_pBuf = new char[m_Capacity]; |
cecfc5e7 | 76 | |
8ad74db3 WS |
77 | if ( oldBuf ) |
78 | { | |
2af95167 | 79 | memcpy( m_pBuf, oldBuf, m_Size ); |
8ad74db3 WS |
80 | delete oldBuf; |
81 | } | |
82 | } | |
cecfc5e7 | 83 | |
8ad74db3 | 84 | // append new data |
2af95167 | 85 | memcpy( &m_pBuf[m_Size], srcBuf, count ); |
cecfc5e7 | 86 | |
2af95167 | 87 | m_Size += count; |
cecfc5e7 VZ |
88 | } |
89 | ||
90 | ScriptStream& ScriptStream::operator<<( const char* str ) | |
91 | { | |
8ad74db3 | 92 | WriteBytes( str, strlen( str ) ); |
cecfc5e7 | 93 | |
8ad74db3 | 94 | return *this; |
cecfc5e7 VZ |
95 | } |
96 | ||
2af95167 | 97 | ScriptStream& ScriptStream::operator<<( const wxString& str ) |
cecfc5e7 | 98 | { |
8ad74db3 WS |
99 | if ( str.length() < 512 ) |
100 | { | |
101 | char buf[512]; | |
102 | size_t len = str.length(); | |
cecfc5e7 | 103 | |
8ad74db3 WS |
104 | for( size_t i = 0; i != len; ++i ) |
105 | buf[i] = str[i]; | |
cecfc5e7 | 106 | |
8ad74db3 WS |
107 | WriteBytes( buf, len ); |
108 | } | |
109 | else | |
110 | WriteBytes( str.c_str(), str.length() ); | |
cecfc5e7 | 111 | |
8ad74db3 | 112 | return *this; |
cecfc5e7 VZ |
113 | } |
114 | ||
115 | ScriptStream& ScriptStream::operator<<( char ch ) | |
116 | { | |
8ad74db3 | 117 | WriteBytes( &ch, 1 ); |
cecfc5e7 | 118 | |
8ad74db3 | 119 | return *this; |
cecfc5e7 VZ |
120 | } |
121 | ||
122 | void ScriptStream::endl() | |
123 | { | |
8ad74db3 WS |
124 | char ch = '\n'; |
125 | WriteBytes( &ch, 1 ); | |
cecfc5e7 VZ |
126 | } |
127 | ||
128 | /***** Implementation for class ScriptTemplate *****/ | |
129 | ||
2af95167 | 130 | ScriptTemplate::ScriptTemplate( const wxString& templateText ) |
cecfc5e7 | 131 | { |
2af95167 | 132 | wxString tmp = templateText; |
cecfc5e7 | 133 | |
2af95167 | 134 | m_TText = (char*)malloc( tmp.length() + 1 ); |
cecfc5e7 | 135 | |
2af95167 | 136 | strcpy( m_TText, tmp.c_str() ); |
cecfc5e7 VZ |
137 | } |
138 | ||
139 | ScriptTemplate::~ScriptTemplate() | |
140 | { | |
2af95167 | 141 | for( size_t i = 0; i != m_Vars.size(); ++i ) |
cecfc5e7 | 142 | |
2af95167 | 143 | delete m_Vars[i]; |
cecfc5e7 | 144 | |
2af95167 | 145 | free( m_TText ); |
cecfc5e7 VZ |
146 | } |
147 | ||
148 | bool ScriptTemplate::HasVar( const char* name ) | |
149 | { | |
2af95167 | 150 | for( size_t i = 0; i != m_Vars.size(); ++i ) |
cecfc5e7 | 151 | |
2af95167 | 152 | if ( strcmp( m_Vars[i]->m_Name, name ) == 0 ) |
cecfc5e7 | 153 | |
8ad74db3 | 154 | return 1; |
cecfc5e7 | 155 | |
8ad74db3 | 156 | return 0; |
cecfc5e7 VZ |
157 | } |
158 | ||
159 | void ScriptTemplate::AddStringVar ( const char* name, int ofs ) | |
160 | { | |
2af95167 | 161 | m_Vars.push_back( new TVarInfo( name, ofs, TVAR_STRING ) ); |
cecfc5e7 VZ |
162 | } |
163 | ||
164 | void ScriptTemplate::AddIntegerVar( const char* name, int ofs ) | |
165 | { | |
2af95167 | 166 | m_Vars.push_back( new TVarInfo( name, ofs, TVAR_INTEGER ) ); |
cecfc5e7 VZ |
167 | } |
168 | ||
169 | void ScriptTemplate::AddDoubleVar ( const char* name, int ofs ) | |
170 | { | |
2af95167 | 171 | m_Vars.push_back( new TVarInfo( name, ofs, TVAR_DOUBLE ) ); |
cecfc5e7 VZ |
172 | } |
173 | ||
174 | void ScriptTemplate::AddObjectRefArray( const char* name, | |
8ad74db3 WS |
175 | int ofsRefToFirstObj, |
176 | int ofsObjSizeInt, | |
177 | int ofsObjRefTempl | |
178 | ) | |
cecfc5e7 | 179 | { |
8ad74db3 | 180 | TArrayInfo* pInfo = new TArrayInfo( name ); |
cecfc5e7 | 181 | |
2af95167 | 182 | m_Vars.push_back( pInfo ); |
cecfc5e7 | 183 | |
2af95167 WS |
184 | pInfo->m_RefOfs = ofsRefToFirstObj; |
185 | pInfo->m_SizeIntOfs = ofsObjSizeInt; | |
186 | pInfo->m_ObjRefTemplOfs = ofsObjRefTempl; | |
cecfc5e7 VZ |
187 | } |
188 | ||
8ad74db3 WS |
189 | inline void ScriptTemplate::PrintVar( TVarInfo* pInfo, |
190 | void* dataObj, | |
191 | ScriptStream& stm ) | |
cecfc5e7 | 192 | { |
8ad74db3 WS |
193 | char buf[128]; |
194 | ||
821d644d | 195 | switch ( pInfo->m_Type ) |
8ad74db3 WS |
196 | { |
197 | case TVAR_INTEGER : | |
198 | { | |
2af95167 | 199 | sprintf(buf, "%d",*( (int*) ((char*)dataObj + pInfo->m_Ofs) ) ); |
8bc17f14 | 200 | |
8ad74db3 WS |
201 | stm.WriteBytes( buf, strlen(buf ) ); |
202 | break; | |
203 | } | |
204 | ||
205 | case TVAR_STRING : | |
206 | { | |
2af95167 | 207 | wxString& str = *( (wxString*) ((char*)dataObj+pInfo->m_Ofs) ); |
8ad74db3 WS |
208 | |
209 | const char* cs = str.c_str(); | |
cecfc5e7 | 210 | #ifdef DEBUG_WEIRED_OFFSETS |
8ad74db3 WS |
211 | cout << "DBG:: cs address is " << (int)cs << endl; |
212 | cout << "DBG:: str address is " << (int)(&str) << endl; | |
213 | cout << "DBG:: dataObj points to " << (int)dataObj << endl; | |
2af95167 WS |
214 | cout << "DBG:: pInfo->m_Ofs value is " << (int)pInfo->m_Ofs << endl; |
215 | cout << "DBG:: d+pInfo->m_Ofs is " << (int)((char*)dataObj + pInfo->m_Ofs) << endl; | |
8bc17f14 | 216 | cout << "DBG:: pInfo->m_Name is " << pInfo->m_Name << endl; |
821d644d | 217 | cout << "DBG:: pInfo->m_Type is " << pInfo->m_Type << endl; |
8ad74db3 WS |
218 | cout << "DBG:: end of dump. " << endl; |
219 | ||
220 | cout << "DBG:: cs value is " << endl << cs << endl; | |
cecfc5e7 | 221 | #endif |
8ad74db3 WS |
222 | stm.WriteBytes( cs, strlen(cs) ); |
223 | break; | |
224 | } | |
225 | ||
226 | case TVAR_DOUBLE : | |
227 | { | |
228 | sprintf( buf, "%f", | |
2af95167 | 229 | *( (double*)( (char*)dataObj+pInfo->m_Ofs) ) ); |
8ad74db3 WS |
230 | |
231 | stm.WriteBytes( buf, strlen(buf ) ); | |
232 | break; | |
233 | } | |
234 | ||
235 | case TVAR_REF_ARRAY : | |
236 | { | |
237 | TArrayInfo& info = *((TArrayInfo*)pInfo); | |
238 | ||
2af95167 | 239 | int sz = *((int*) ( (char*)dataObj+info.m_SizeIntOfs )); |
8ad74db3 WS |
240 | if ( !sz ) |
241 | { | |
242 | // DBG:: | |
243 | int u = 0; | |
244 | ++u; | |
245 | break; | |
246 | } | |
247 | ||
2af95167 | 248 | int* array = *((int**)( (char*)dataObj+info.m_RefOfs )); |
8bc17f14 | 249 | |
8ad74db3 WS |
250 | ScriptTemplate* pRefTempl; |
251 | ||
252 | for( int i = 0; i != sz; ++i ) | |
253 | { | |
254 | pRefTempl = | |
2af95167 | 255 | *((ScriptTemplate**)((char*)(array[i])+info.m_ObjRefTemplOfs)); |
8ad74db3 WS |
256 | |
257 | pRefTempl->PrintScript( (void*)array[i], stm ); | |
258 | } | |
259 | ||
260 | break; | |
261 | } | |
262 | ||
263 | default : break; | |
264 | } | |
cecfc5e7 VZ |
265 | } |
266 | ||
267 | void ScriptTemplate::PrintScript( void* dataObj, ScriptStream& stm ) | |
268 | { | |
2af95167 | 269 | char* cur = m_TText; |
cecfc5e7 | 270 | |
8ad74db3 WS |
271 | // template parsing loop |
272 | do | |
273 | { | |
274 | char* start = cur; | |
cecfc5e7 | 275 | |
8ad74db3 | 276 | while( *cur != '\0' && *cur != '$' ) ++cur; |
cecfc5e7 | 277 | |
8ad74db3 WS |
278 | // flush text collected between variables |
279 | stm.WriteBytes( start, cur - start ); | |
cecfc5e7 | 280 | |
8ad74db3 | 281 | if ( *cur == '\0' ) break; |
cecfc5e7 | 282 | |
8ad74db3 | 283 | cur += 2; // skip to the name of the var |
cecfc5e7 | 284 | |
8ad74db3 | 285 | start = cur; |
cecfc5e7 | 286 | |
8ad74db3 | 287 | while( *cur != ')' ) ++cur; |
cecfc5e7 | 288 | |
8ad74db3 | 289 | // put terminating zero temorarely |
cecfc5e7 | 290 | |
8ad74db3 | 291 | *cur = '\0'; |
cecfc5e7 | 292 | |
8ad74db3 | 293 | // look up variable |
cecfc5e7 | 294 | |
2af95167 | 295 | size_t sz = m_Vars.size(); |
8ad74db3 | 296 | // bool found = false; |
cecfc5e7 | 297 | |
8ad74db3 WS |
298 | for( size_t i = 0; i != sz; ++i ) |
299 | { | |
2af95167 | 300 | if ( strcmp( m_Vars[i]->m_Name, start ) == 0 ) |
8ad74db3 | 301 | { |
2af95167 | 302 | PrintVar( m_Vars[i], dataObj, stm ); |
cecfc5e7 | 303 | |
8ad74db3 WS |
304 | *cur = ')'; // remove terminating zero |
305 | ++cur; | |
306 | // found = 1; | |
307 | break; | |
308 | } | |
309 | } | |
cecfc5e7 | 310 | |
8ad74db3 WS |
311 | // variable referred by template script is not |
312 | // registered to this tempalte object | |
313 | // ASSERT( found ); | |
cecfc5e7 | 314 | |
8ad74db3 | 315 | } while(1); |
cecfc5e7 VZ |
316 | } |
317 | ||
318 | /***** implementation for class ScriptSection *****/ | |
319 | ||
2af95167 | 320 | int ScriptSection::m_IdCounter = 0; |
cecfc5e7 | 321 | |
c826f371 WS |
322 | ScriptSection::ScriptSection( const wxString& name, |
323 | const wxString& body, | |
8ad74db3 WS |
324 | ScriptTemplate* pSectionTemplate, |
325 | ScriptTemplate* pReferenceTemplate, | |
326 | bool autoHide, | |
327 | bool sorted | |
328 | ) | |
2af95167 | 329 | : m_pParent ( NULL ), |
cecfc5e7 | 330 | |
2af95167 WS |
331 | m_Name ( name ), |
332 | m_Body ( body ), | |
cecfc5e7 | 333 | |
2af95167 WS |
334 | m_AutoHide ( autoHide ), |
335 | m_SortOn ( sorted ), | |
cecfc5e7 | 336 | |
2af95167 WS |
337 | m_pSectTempl( pSectionTemplate ), |
338 | m_pRefTempl ( pReferenceTemplate ), | |
cecfc5e7 | 339 | |
2af95167 WS |
340 | m_RefCount( 0 ), |
341 | m_ArrSize( 0 ) | |
cecfc5e7 | 342 | { |
8ad74db3 | 343 | // generate GUID |
cecfc5e7 | 344 | |
c826f371 | 345 | wxChar buf[32]; |
2af95167 WS |
346 | wxSprintf( buf, _T("%d"), ++m_IdCounter ); |
347 | m_Id = buf; | |
cecfc5e7 VZ |
348 | } |
349 | ||
350 | ScriptSection::~ScriptSection() | |
351 | { | |
2af95167 | 352 | SectListT lst = m_Subsections; |
cecfc5e7 | 353 | |
2af95167 | 354 | while( m_Subsections.size() ) |
cecfc5e7 | 355 | |
2af95167 | 356 | m_Subsections[0]->RemoveRef(); |
cecfc5e7 | 357 | |
2af95167 | 358 | for( size_t i = 0; i != m_References.size(); ++i ) |
cecfc5e7 | 359 | |
2af95167 | 360 | m_References[i]->RemoveRef(); |
cecfc5e7 VZ |
361 | } |
362 | ||
363 | void ScriptSection::AddRef() | |
364 | { | |
2af95167 | 365 | ++m_RefCount; |
cecfc5e7 VZ |
366 | } |
367 | ||
368 | void ScriptSection::RemoveRef() | |
369 | { | |
2af95167 | 370 | if ( !m_RefCount || !(--m_RefCount) ) |
8ad74db3 | 371 | { |
2af95167 | 372 | if (m_pParent) |
8ad74db3 WS |
373 | { |
374 | // remove ourselves from parent's list | |
375 | ||
2af95167 | 376 | SectListT& lst = m_pParent->m_Subsections; |
8ad74db3 WS |
377 | for( size_t i = 0; i != lst.size(); ++i ) |
378 | ||
379 | if ( lst[i] == this ) | |
380 | { | |
381 | lst.erase( &lst[i] ); | |
382 | break; | |
383 | } | |
384 | } | |
385 | ||
386 | delete this; | |
387 | } | |
cecfc5e7 VZ |
388 | } |
389 | ||
390 | ScriptSection* ScriptSection::GetSubsection( const char* name ) | |
391 | { | |
8ad74db3 WS |
392 | // FOR NOW:: fixed section name length |
393 | char buf[128]; | |
cecfc5e7 | 394 | |
8ad74db3 | 395 | size_t cur = 0; |
cecfc5e7 | 396 | |
8ad74db3 WS |
397 | while( name[cur] && name[cur] != '/' ) |
398 | { | |
399 | buf[cur] = name[cur]; | |
400 | ++cur; | |
401 | } | |
cecfc5e7 | 402 | |
8ad74db3 | 403 | // ASSERT( cur < sizeof(buf) ); |
cecfc5e7 | 404 | |
8ad74db3 | 405 | buf[cur] = '\0'; |
cecfc5e7 | 406 | |
2af95167 | 407 | size_t sz = m_Subsections.size(); |
cecfc5e7 | 408 | |
8ad74db3 WS |
409 | for( size_t i = 0; i != sz; ++i ) |
410 | { | |
411 | // DBG:: | |
2af95167 | 412 | //ScriptSection& sect = *m_Subsections[i]; |
cecfc5e7 | 413 | |
2af95167 | 414 | if ( m_Subsections[i]->m_Name == buf ) |
8ad74db3 WS |
415 | { |
416 | if ( name[cur] == '/' ) | |
cecfc5e7 | 417 | |
8ad74db3 | 418 | // search recursivelly |
2af95167 | 419 | return m_Subsections[i]->GetSubsection( &name[cur+1] ); |
8ad74db3 | 420 | else |
2af95167 | 421 | return m_Subsections[i]; |
8ad74db3 WS |
422 | } |
423 | } | |
cecfc5e7 | 424 | |
8ad74db3 | 425 | return 0; |
cecfc5e7 VZ |
426 | } |
427 | ||
428 | void ScriptSection::AddSection( ScriptSection* pSection, | |
8ad74db3 WS |
429 | bool addToReferencesToo |
430 | ) | |
cecfc5e7 | 431 | { |
2af95167 | 432 | m_Subsections.push_back( pSection ); |
cecfc5e7 | 433 | |
8ad74db3 | 434 | pSection->AddRef(); |
cecfc5e7 | 435 | |
8ad74db3 | 436 | // can add section to multiple containers |
2af95167 | 437 | // ASSERT( pSection->m_pParent == 0 ); |
cecfc5e7 | 438 | |
2af95167 | 439 | pSection->m_pParent = this; |
cecfc5e7 | 440 | |
8ad74db3 | 441 | if ( addToReferencesToo ) |
cecfc5e7 | 442 | |
8ad74db3 | 443 | AddReference( pSection ); |
cecfc5e7 VZ |
444 | } |
445 | ||
446 | void ScriptSection::AddReference( ScriptSection* pReferredSection ) | |
447 | { | |
2af95167 | 448 | m_References.push_back( pReferredSection ); |
cecfc5e7 | 449 | |
8ad74db3 | 450 | pReferredSection->AddRef(); |
cecfc5e7 | 451 | |
8ad74db3 | 452 | // set up mandatory fields used by ScriptTemplate |
2af95167 WS |
453 | m_ArrSize = m_References.size(); |
454 | if ( m_ArrSize ) | |
455 | m_RefFirst = (void*)&m_References[0]; | |
cecfc5e7 VZ |
456 | } |
457 | ||
458 | SectListT& ScriptSection::GetSubsections() | |
459 | { | |
2af95167 | 460 | return m_Subsections; |
cecfc5e7 VZ |
461 | } |
462 | ||
463 | // static method: | |
464 | void ScriptSection::RegisterTemplate( ScriptTemplate& sectionTempalte ) | |
465 | { | |
8ad74db3 WS |
466 | int nameOfs, bodyOfs, idOfs, |
467 | arrRefOfs, arrSizeOfs, refTemplOfs; | |
cecfc5e7 | 468 | |
8ad74db3 | 469 | // obtaining offsets of member vars |
8bc17f14 WS |
470 | |
471 | GET_VAR_OFS( ScriptSection, m_Name, &nameOfs ) | |
2af95167 WS |
472 | GET_VAR_OFS( ScriptSection, m_Body, &bodyOfs ) |
473 | GET_VAR_OFS( ScriptSection, m_Id, &idOfs ) | |
474 | GET_VAR_OFS( ScriptSection, m_RefFirst,&arrRefOfs ) | |
475 | GET_VAR_OFS( ScriptSection, m_ArrSize, &arrSizeOfs ) | |
cecfc5e7 | 476 | |
2af95167 | 477 | GET_VAR_OFS( ScriptSection, m_pRefTempl, &refTemplOfs ) |
cecfc5e7 | 478 | |
8ad74db3 | 479 | // registering member variables with given script template |
cecfc5e7 | 480 | |
8ad74db3 WS |
481 | sectionTempalte.AddStringVar( "NAME", nameOfs ); |
482 | sectionTempalte.AddStringVar( "BODY", bodyOfs ); | |
483 | sectionTempalte.AddStringVar( "ID", idOfs ); | |
cecfc5e7 | 484 | |
8ad74db3 WS |
485 | sectionTempalte.AddObjectRefArray( "REFLIST", |
486 | arrRefOfs, arrSizeOfs, refTemplOfs ); | |
cecfc5e7 VZ |
487 | } |
488 | ||
489 | void ScriptSection::Print( ScriptStream& stm ) | |
490 | { | |
8ad74db3 | 491 | // TBD:: sorting |
cecfc5e7 | 492 | |
8ad74db3 | 493 | // print out this content first |
2af95167 | 494 | if ( m_pSectTempl ) |
cecfc5e7 | 495 | |
2af95167 | 496 | m_pSectTempl->PrintScript( this, stm ); |
cecfc5e7 | 497 | |
8ad74db3 | 498 | // attach contents subsections at the end of this content |
cecfc5e7 | 499 | |
2af95167 | 500 | for( size_t i = 0; i != m_Subsections.size(); ++i ) |
cecfc5e7 | 501 | |
2af95167 | 502 | m_Subsections[i]->Print( stm ); |
cecfc5e7 VZ |
503 | } |
504 | ||
505 | void ScriptSection::DoRemoveEmptySections(int& nRemoved, SectListT& removedLst) | |
506 | { | |
2af95167 | 507 | for( size_t i = 0; i != m_Subsections.size(); ++i ) |
8ad74db3 | 508 | { |
2af95167 | 509 | ScriptSection& sect = *m_Subsections[i]; |
8ad74db3 WS |
510 | |
511 | sect.DoRemoveEmptySections( nRemoved, removedLst ); | |
512 | ||
2af95167 | 513 | if (sect.m_AutoHide ) |
8bc17f14 | 514 | |
2af95167 | 515 | if ( sect.m_References.size() == 0 ) |
8ad74db3 WS |
516 | { |
517 | bool found = false; | |
518 | for( size_t k = 0; k != removedLst.size(); ++k ) | |
519 | ||
520 | if ( removedLst[k] == § ) | |
521 | { | |
522 | found = 1; | |
523 | break; | |
524 | } | |
8bc17f14 | 525 | |
8ad74db3 WS |
526 | if ( !found ) |
527 | { | |
528 | removedLst.push_back( § ); | |
529 | ++nRemoved; | |
530 | ||
531 | delete § | |
532 | --i; | |
533 | } | |
534 | } | |
535 | } | |
cecfc5e7 VZ |
536 | } |
537 | ||
538 | void ScriptSection::DoRemoveDeadLinks( SectListT& removedLst) | |
539 | { | |
8ad74db3 WS |
540 | size_t dsz = removedLst.size(); |
541 | ||
2af95167 | 542 | for( size_t i = 0; i != m_Subsections.size(); ++i ) |
8ad74db3 | 543 | { |
2af95167 | 544 | m_Subsections[i]->DoRemoveDeadLinks( removedLst ); |
8ad74db3 WS |
545 | } |
546 | ||
2af95167 | 547 | for( size_t n = 0; n != m_References.size(); ++n ) |
8ad74db3 WS |
548 | { |
549 | for( size_t k = 0; k != dsz; ++k ) | |
550 | ||
2af95167 | 551 | if ( removedLst[k] == m_References[n] ) |
8ad74db3 | 552 | { |
2af95167 | 553 | m_References.erase( &m_References[n] ); |
8ad74db3 WS |
554 | --n; |
555 | ||
556 | // set up mandatory fields used by ScriptTemplate | |
2af95167 WS |
557 | m_ArrSize = m_References.size(); |
558 | if ( m_ArrSize ) | |
559 | m_RefFirst = (void*)&m_References[0]; | |
8ad74db3 WS |
560 | |
561 | break; | |
562 | } | |
563 | } | |
cecfc5e7 VZ |
564 | } |
565 | ||
566 | ||
567 | void ScriptSection::RemoveEmptySections() | |
568 | { | |
8ad74db3 | 569 | // FIXME:: this is very_very_very slow alg.! +"doesn't work" |
cecfc5e7 | 570 | |
8ad74db3 | 571 | int nRemoved = 0; |
cecfc5e7 | 572 | |
8ad74db3 WS |
573 | do |
574 | { | |
575 | SectListT removedLst; | |
576 | nRemoved = 0; | |
cecfc5e7 | 577 | |
8ad74db3 | 578 | DoRemoveEmptySections( nRemoved, removedLst ); |
cecfc5e7 | 579 | |
8ad74db3 WS |
580 | DoRemoveDeadLinks( removedLst ); |
581 | } | |
582 | while( nRemoved ); | |
cecfc5e7 VZ |
583 | } |
584 | ||
585 | /***** Iimplementation for class DocGeneratorBase *****/ | |
586 | ||
8ad74db3 WS |
587 | bool DocGeneratorBase::SaveDocument( const char* fname, |
588 | const char* fopenOptions, | |
589 | ScriptSection* pFromSection | |
590 | ) | |
cecfc5e7 | 591 | { |
8ad74db3 | 592 | FILE* fp = fopen( fname, fopenOptions ); |
cecfc5e7 | 593 | |
42389ac7 | 594 | if ( !fp ) return 0; |
cecfc5e7 | 595 | |
8ad74db3 | 596 | ScriptStream stm; |
cecfc5e7 | 597 | |
8ad74db3 WS |
598 | // check if derived class agrees about saving it |
599 | if ( !OnSaveDocument( stm ) ) return 0; | |
cecfc5e7 | 600 | |
8ad74db3 | 601 | if ( pFromSection ) |
cecfc5e7 | 602 | |
8ad74db3 WS |
603 | pFromSection->Print( stm ); |
604 | else | |
605 | { | |
606 | ScriptSection* pTopSect = GetTopSection(); | |
607 | // ASSERT( pTopSect ); | |
608 | pTopSect->Print( stm ); | |
609 | } | |
cecfc5e7 | 610 | |
8ad74db3 | 611 | size_t nWrite = fwrite( stm.GetBuf(), 1, stm.GetBufSize(), fp ); |
cecfc5e7 | 612 | |
8ad74db3 | 613 | if ( nWrite != stm.GetBufSize() ) return 0; |
cecfc5e7 | 614 | |
8ad74db3 | 615 | fclose( fp ); |
cecfc5e7 | 616 | |
8ad74db3 | 617 | return 1; |
cecfc5e7 | 618 | |
8ad74db3 | 619 | // that^s it |
cecfc5e7 | 620 | } |