This repository has been archived by the owner on May 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
property.cpp
668 lines (609 loc) · 18.9 KB
/
property.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//------------------------------------------------------------------------------
/*! \file property.cpp
* \version BehavePlus3
* \author Copyright (C) 2002-2004 by Collin D. Bevins. All rights reserved.
*
* \brief Generic property system used to define and access the plethora of
* properties used by BehavePlus.
*/
// Custom include files
#include "appmessage.h"
#include "apptranslator.h"
#include "property.h"
// Qt include files
#include <qstringlist.h>
// Standard include files
#include <ctype.h>
#include <stdio.h>
#include <iostream>
//------------------------------------------------------------------------------
/*! \brief Property constructor.
*
* \param type Property type
* \param value Text representation of the property value
* \param releaseFrom Effective beginning this release
* \param m_releaseThru Effective through this release
*/
Property::Property( PropertyType type, const QString &value,
int releaseFrom, int releaseThru ) :
m_type(type),
m_value(value),
m_releaseFrom(releaseFrom),
m_releaseThru(releaseThru)
{
return;
}
//------------------------------------------------------------------------------
/*! \brief Determines whether or not the Property is part of the specified
* release.
*
* \param release Release number (5 digits).
*
* \return TRUE if the Property is part of the specified release.
*/
bool Property::isCurrent( int release )
{
return( release >= m_releaseFrom && release <= m_releaseThru );
}
//------------------------------------------------------------------------------
/*! \brief PropertyDict constructor.
*/
PropertyDict::PropertyDict( int size, bool caseSensitive ) :
QDict<Property>( size, caseSensitive )
{
setAutoDelete(true);
return;
}
//------------------------------------------------------------------------------
/*! \brief Tests and inserts the property into the dictionary.
*
* \return TRUE on success, FALSE on failure.
*/
bool PropertyDict::add( const QString &name, Property::PropertyType type,
const QString &value, int releaseFrom, int releaseThru )
{
// Create the property property
Property *property = new Property( type, "", releaseFrom, releaseThru );
checkmem( __FILE__, __LINE__, property, "Property property", 1 );
// Add it to the dictionary
insert( name, property );
// Test and store its value
update( name, value ) ;
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Returns the current Boolean value of property named \a name.
*/
bool PropertyDict::boolean( const QString &name ) const
{
Property *property = get( name, Property::Boolean );
if ( ! property )
{
return( false );
}
else if ( property->m_value == "true" )
{
return( true );
}
else if ( property->m_value == "false" )
{
return( false );
}
// This code block should never be executed!
// (unless the value string is internally corrupted)
QString msg("");
translate( msg, "Property:badBooleanStore", name, property->m_value );
error( msg );
return( false );
}
//------------------------------------------------------------------------------
/*! \brief Sets the Boolean property named \a name to \a value.
*
* \retval TRUE if property exists and is of correct type.
* \retval FALSE if property doesn't exist or is of the wrong type.
*/
bool PropertyDict::boolean( const QString &name, bool value )
{
Property *property = get( name, Property::Boolean );
if ( ! property )
{
return( false );
}
property->m_value = ( value )
? "true"
: "false" ;
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Returns the current Color value of property named \a name.
*/
QString PropertyDict::color( const QString &name ) const
{
Property *property = get( name, Property::Color );
if ( ! property )
{
return( QString( "yellow" ) );
}
return( property->m_value );
}
//------------------------------------------------------------------------------
/*! \brief Sets the Color property named \a name to \a value.
*
* \retval TRUE if property exists and is of correct type.
* \retval FALSE if property doesn't exist or is of the wrong type.
*/
bool PropertyDict::color( const QString &name, const QString &value )
{
Property *property = get( name, Property::Color );
if ( ! property )
{
return( false );
}
property->m_value = value;
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Determines if the property named \a name exists int he dictionary.
*
* \return TRUE if \a name exists, FALSE if \a name not found.
*/
bool PropertyDict::exists( const QString &name ) const
{
Property *property = find( name );
return( ( property != 0 )
? true
: false );
}
//------------------------------------------------------------------------------
/*! \brief Finds the property with \a name, ensures its of the requested type,
* and returns a pointer to it.
*
* \retval TRUE if \a name exists and is os \a type.
* \retval FALSE if \a name not found or is not of \a type.
*/
Property *PropertyDict::get( const QString &name, Property::PropertyType type )
const
{
Property *property = find( name );
if ( ! property )
{
QString msg("");
translate( msg, "Property:notFound", name );
error( msg );
return( 0 );
}
else if ( property->m_type != type )
{
QString msg("");
translate( msg, "Property:wrongType", name,
QString( "%1" ).arg( property->m_type ),
QString( "%1" ).arg( type ) );
error( msg );
return( 0 );
}
return( property );
}
//------------------------------------------------------------------------------
/*! \brief Returns the current integer value of property named \a name.
*/
int PropertyDict::integer( const QString &name ) const
{
Property *property = get( name, Property::Integer );
if ( ! property )
{
return( 0 );
}
bool ok;
int i = property->m_value.toInt( &ok, 10 );
if ( ok )
{
return( i );
}
// This code block should never be executed!
// (unless the value string is internally corrupted)
QString msg("");
translate( msg, "Property:badIntegerStore", name, property->m_value );
error( msg );
return( 0 );
}
//------------------------------------------------------------------------------
/*! \brief Sets the Integer property named \a name to \a value.
*
* \retval TRUE if property exists and is of correct type.
* \retval FALSE if property doesn't exist or is of the wrong type.
*/
bool PropertyDict::integer( const QString &name, int value )
{
Property *property = get( name, Property::Integer );
if ( ! property )
{
return( 0 );
}
property->m_value = QString( "%1" ).arg( value );
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Determines whether or not the Property is part of the specified
* release.
*
* \param release Release number (5 digits).
*
* \return TRUE if the Property is part of the specified release.
*/
bool PropertyDict::isCurrent( const QString &name, int release ) const
{
Property *property = find( name );
if ( property )
{
return( release >= property->m_releaseFrom
&& release <= property->m_releaseThru );
}
return( false );
}
//------------------------------------------------------------------------------
/*! \brief Returns the current real value of property named \a name.
*/
double PropertyDict::real( const QString &name ) const
{
Property *property = get( name, Property::Real );
if ( ! property )
{
return( 0. );
}
bool ok;
double d = property->m_value.toDouble( &ok );
if ( ok )
{
return( d );
}
// This code block should never be executed!
// (unless the value string is internally corrupted)
QString msg("");
translate( msg, "Property:badRealStore", name, property->m_value );
error( msg );
return( 0. );
}
//------------------------------------------------------------------------------
/*! \brief Sets the Real property named \a name to \a value.
*
* \retval TRUE if property exists and is of correct type.
* \retval FALSE if property doesn't exist or is of the wrong type.
*/
bool PropertyDict::real( const QString &name, double value )
{
Property *property = get( name, Property::Real );
if ( ! property )
{
return( false );
}
property->m_value = QString( "%1" ).arg( value );
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Returns the current String value of property named \a name.
*/
QString PropertyDict::string( const QString &name ) const
{
Property *property = get( name, Property::String );
if ( ! property )
{
return( QString( "" ) );
}
return( property->m_value );
}
//------------------------------------------------------------------------------
/*! \brief Sets the String property named \a name to \a value.
*
* \retval TRUE if property exists and is of correct type.
* \retval FALSE if property doesn't exist or is of the wrong type.
*/
bool PropertyDict::string( const QString &name, const QString &value )
{
Property *property = get( name, Property::String );
if ( ! property )
{
return( false );
}
property->m_value = value;
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Returns the PropertyType of property \a name.
*/
Property::PropertyType PropertyDict::type( const QString &name ) const
{
Property *property = find( name );
if ( ! property )
{
QString msg("");
translate( msg, "Property:notFound", name );
error( msg );
return( Property::None );
}
return( property->m_type );
}
//------------------------------------------------------------------------------
/*! \brief Returns the QString value of property \a name.
*/
QString PropertyDict::value( const QString &name ) const
{
Property *property = find( name );
if ( ! property )
{
QString msg("");
translate( msg, "Property:notFound", name );
error( msg );
return( QString( "" ) );
}
return( property->m_value );
}
//------------------------------------------------------------------------------
/*! \brief Updates an existing Property \a name with \a value.
*
* \retval TRUE if property exists and is of correct type.
* \retval FALSE if property doesn't exist or is of the wrong type.
*/
bool PropertyDict::update( const QString &name, const QString &value )
{
Property *property = find( name );
if ( ! property )
{
QString msg("");
translate( msg, "Property:notFound", name );
error( msg );
return( false );
}
if ( property->m_type == Property::Boolean )
{
if ( value != "true" && value != "false" )
{
QString msg("");
translate( msg, "Property:badBooleanAssign", name, value );
error( msg );
return( false );
}
}
else if ( property->m_type == Property::Integer )
{
bool ok;
value.toInt( &ok, 10 );
if ( ! ok )
{
QString msg("");
translate( msg, "Property:badIntegerAssign", name, value );
error( msg );
return( false );
}
}
else if ( property->m_type == Property::Real )
{
bool ok;
value.toDouble( &ok );
if ( ! ok )
{
QString msg("");
translate( msg, "Property:badRealAssign", name, value );
error( msg );
return( false );
}
}
property->m_value = value;
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Reads properties from the file into the property dictionary.
* The file must be in the same XML format as produced by
* PropertyDict::write().
*
* \return TRUE on success, FALSE if unable to open the file.
*/
bool PropertyDict::readXmlFile( const QString &fileName )
{
PropertyParser *handler = new PropertyParser( this, fileName );
checkmem( __FILE__, __LINE__, handler, "PropertyParser handler", 1 );
QFile xmlFile( fileName );
QXmlInputSource source( &xmlFile );
QXmlSimpleReader reader;
reader.setContentHandler( handler );
reader.setErrorHandler( handler );
bool result = reader.parse( &source );
delete handler;
return( result );
}
//------------------------------------------------------------------------------
/*! \brief Writes the current property dictionary contents in sorted order
* to the \a fileName in XML format.
*
* \param fileName Name of the file to be (over)written.
* \param release Only write properties with this release or lower.
*
* \return TRUE on success, FALSE if unable to open the file.
*/
bool PropertyDict::writeXmlFile( const QString &fileName,
const QString &elementName, int release )
{
// Atempt to open the property file
FILE *fptr;
if ( ! ( fptr = fopen( fileName.latin1(), "w" ) ) )
{
QString text("");
translate( text, "Property:WriteXmlFile:NoOpen", fileName );
error( text );
return( false );
}
// Write the header, body, and footer.
xmlWriteHeader( fptr, elementName, "property", release );
writeXmlFile( fptr, release );
xmlWriteFooter( fptr, elementName );
fclose( fptr );
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Writes the current property dictionary contents in sorted order
* to the file stream. Assumes the XML header has already been written by the
* caller, and the XML footer will be added by the caller.
*
* \param fptr Output file stream pointer.
* \param release Only write properties with this release or lower.
*
* \return TRUE on success, FALSE if unable to open the file.
*/
bool PropertyDict::writeXmlFile( FILE *fptr, int release )
{
// Write out all properties to a string list for sorting
QStringList propList;
QDictIterator<Property> it( *this );
Property *p;
QString xml("");
while( it.current() )
{
p = (Property *) it.current();
if ( p->isCurrent( release ) )
{
if ( p->m_value.isNull()
|| p->m_value.isEmpty() )
{
xml = "";
}
else
{
xml = p->m_value;
xmlEscape( xml );
}
propList.append(
QString( " <property name=\"%1\" value=\"%2\" />" )
.arg( it.currentKey() ).arg( xml ) );
}
++it;
}
// Sort the list and write it to the file
propList.sort();
for ( QStringList::Iterator sit=propList.begin();
sit != propList.end();
++sit )
{
fprintf( fptr, "%s\n", (*sit).latin1() );
}
return( true );
}
//------------------------------------------------------------------------------
/*! \brief PropertyParser constructor.
*
* \param propDict Ptr to parent PropertyDict.
* \param fileName Name of the Property definition document to be read.
* \param validate If TRUE, extra validation is performed.
* \param debug If TRUE, input procesing is echoed to stdout.
*/
PropertyParser::PropertyParser( PropertyDict *propDict, const QString &fileName,
bool validate, bool debug ) :
XmlParser( fileName, validate, debug ),
m_propDict(propDict)
{
return;
}
//------------------------------------------------------------------------------
/*! \brief Handles the <property> element.
*
* This uses error message translation since a translation dictionary should
* be available.
*
* \return TRUE on success, FALSE one error.
*/
bool PropertyParser::handleProperty( const QString &elementName,
const QXmlAttributes& attribute )
{
int id;
QString name, value;
// "name" attribute is required
if ( ( id = attribute.index( "name" ) ) < 0 )
{
trError( "PropertyParser:missingName", elementName, "name" );
return( false );
}
name = attribute.value( id );
// "value" attribute is required
if ( ( id = attribute.index( "value" ) ) < 0 )
{
trError( "PropertyParser:missingAttribute", elementName, name, "value" );
return( false );
}
if ( ( value = attribute.value( id ) ) == "(null)" )
{
value = "";
}
// Find this property in the local Property directory and update it
Property *property;
if ( ( property = m_propDict->find( name ) ) > 0 )
{
if ( ! m_propDict->update( name, value ) )
{
trError( "PropertyParser:badValue",
elementName, name, "value", value );
return( false );
}
}
// Report unknown <property> names here.
else
{
trError( "PropertyParser:badValue", elementName, name, "name", name );
return( false );
}
return( true );
}
//------------------------------------------------------------------------------
/*! \brief Start-of-element callback. This does most the work.
*
* \param namespaceUrl Not used.
* \param localName Not used.
* \param elementName
* \param attribute
*/
bool PropertyParser::startElement( const QString & /* namespaceUrl */,
const QString & /* localName */, const QString& elementName,
const QXmlAttributes& attribute )
{
// If debugging, print each element and its attributes as encountered.
m_indent += " ";
if ( m_debug )
{
std::cout << m_indent << "<" << elementName;
for ( int id = 0;
id < attribute.length();
id++ )
{
std::cout << " " << attribute.localName(id) << "=\""
<< attribute.value(id) << "\"";
}
std::cout << " >" << std::endl;
}
// Skip all elements until <BehavePlus> is found.
if ( ! m_elements )
{
if ( elementName == "BehavePlus" )
{
push( elementName );
return( true );
}
trError( "PropertyParser:unknownDocument" );
return( false );
}
// <property> elements
if ( elementName == "property" )
{
push( elementName );
if ( ! handleProperty( elementName, attribute ) )
{
return( false );
}
}
// Ignore all other tags
else
{
trError( "PropertyParser:unknownElement", elementName );
return( false );
}
return( true );
}
//------------------------------------------------------------------------------
// End of property.cpp
//------------------------------------------------------------------------------