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