/
CPersistentAttributes.cpp
819 lines (672 loc) · 23.7 KB
/
CPersistentAttributes.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/************************************************************************************//**
* @file CPersistentAttributes.cpp
* @brief Persistent attributes class
*
* This file contains the implementation of the CPersistentAttribute class,
* which holds attribute data that persists for entities across level
* boundaries, and may also be saved and restored to/from a file.
* @author Ralph Deane
*//*
* Copyright (c) 2001 Ralph Deane; All rights reserved.
****************************************************************************************/
// The One True Include File
#include "RabidFramework.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/* ------------------------------------------------------------------------------------ */
// Default constructor
/* ------------------------------------------------------------------------------------ */
CPersistentAttributes::CPersistentAttributes()
{
theList = NULL; // Fast & easy.
m_nCount = 0; // Nothing here.
return;
}
/* ------------------------------------------------------------------------------------ */
// Default destructor
/* ------------------------------------------------------------------------------------ */
CPersistentAttributes::~CPersistentAttributes()
{
Clear();
return;
}
/* ------------------------------------------------------------------------------------ */
// Clear
//
// Delete all attributes in this object
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Clear()
{
if(theList == NULL)
return RGF_SUCCESS; // Already empty, cool!
PersistAttribute *pTemp = theList;
PersistAttribute *pNextOne = NULL;
while(pTemp != NULL)
{
pNextOne = pTemp->pNext; // Save off pointer to next
delete pTemp->Name; // Gun the name
if(pTemp->UserData)
delete pTemp->UserData;
delete pTemp; // ZAP! It's dead, Jim.
pTemp = pNextOne; // On to the next victim
}
theList = NULL; // Gone forever.
m_nCount = 0; // List out.
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// SetValueLimits
//
// Set LOW and HIGH limits for attribute values
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::SetValueLimits(const char *szTag, int Low, int High)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_NOT_FOUND; // Not found
pAttr->ValueLowLimit = Low;
pAttr->ValueHighLimit = High;
return RGF_SUCCESS; // Limits set
}
// changed QD 12/15/05
/* ------------------------------------------------------------------------------------ */
// SetValueLimits
//
// Set HIGH limit for attribute values
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::SetHighLimit(const char *szTag, int HighLimit)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_NOT_FOUND; // Not found
pAttr->ValueHighLimit = HighLimit;
pAttr->PowerUpLevel++;
return RGF_SUCCESS; // Limits set
}
// end change
/* ------------------------------------------------------------------------------------ */
// Set
//
// Locate the attribute identified by the tag and set its value to the desired value.
// If the attribute doesn't exist, create it and set the value.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Set(const char *szTag, int nValue)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_NOT_FOUND; // Not found
pAttr->Value = nValue; // Set it, and forget it
ClampValue(pAttr); // Clamp the values to limits
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// SetIf
//
// Locate the attribute identified by the tag. If it doesn't exist, return
// ..an eror. If it DOES exist, perform the test indicated by 'nHow'
// ..and if the result is TRUE, set the attributes value to the desired value.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::SetIf(const char *szTag, int nHow, int nCompareValue, int nValue)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_NOT_FOUND; // Not found
if(LocalCompare(pAttr, nHow, nCompareValue))
pAttr->Value = nValue; // Set it, and forget it
ClampValue(pAttr); // Clamp the values to limits
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// Modify
//
// Locate the attribute identified by the tag. If it doesn't exist, return
// ..an error. If it DOES exist, add 'nValue' to the attribute value.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Modify(const char *szTag, int nValue)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_NOT_FOUND; // No such attribute
pAttr->Value += nValue; // MAke the mod
// changed RF064
pAttr->ModifyAmt = nValue;
// end change RF064
ClampValue(pAttr); // Clamp the values to limits
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// ModifyIf
//
// Locate the attribute identifided by the tag. If it doesn't exist, return
// ..an error. If it does exist, perform the comparison indicated by
// ..'nHow' and if true modify the value by adding 'nValue' to the attribute value.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::ModifyIf(const char *szTag, int nHow, int nCompareValue, int nValue)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_NOT_FOUND; // No such attribute
if(LocalCompare(pAttr, nHow, nCompareValue))
pAttr->Value += nValue;
ClampValue(pAttr); // Clamp the values to limits
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// Add
//
// Add a new attribute to the attribute list. If the attribute already exists,
// ..don't return an error but don't perform the add.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Add(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr != NULL)
{
pAttr->Count++; // Another one of these...
return RGF_SUCCESS; // Already there, ignore call
}
AddNew(szTag, 0); // Add new attribute
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// AddAndSet
//
// Add a new attribute to the attribute list. If the attribute already exists,
// ..don't return an error but don't perform the add.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::AddAndSet(const char *szTag, int nValue)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr != NULL)
{
pAttr->Value = nValue;
pAttr->Count++; // Another one of these...
return RGF_SUCCESS; // Already there, ignore call
}
AddNew(szTag, nValue); // Add new attribute with value
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// AddIf
//
// Add a new attribute to the list if the comparison indicated by 'nHow'
// ..with the value of attribute 'szWhat' and 'nCompareValue' is true.
// ..If the attribute already exists, take no action but return no error.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::AddIf(const char *szTag, int nHow, int nCompareValue, const char *szWhat)
{
PersistAttribute *pAttr = Locate(szWhat);
if(pAttr == NULL)
return RGF_NOT_FOUND; // Attribute not found
if(LocalCompare(pAttr, nHow, nCompareValue))
{
pAttr = Locate(szTag); // Is it there?
if(pAttr != NULL)
return RGF_SUCCESS; // Already exists
AddNew(szTag, 0); // Add a new attribute
}
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// Remove
//
// Remove an instance of the attribute 'szTag' from the list. If the last
// ..instance is removed, delete the attribute from the list.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Remove(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_NOT_FOUND; // No such attribute
pAttr->Count--; // Decrement instance count
if(pAttr->Count <= 0)
Delete(szTag); // None left, wipe it out.
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// RemoveIf
//
// Remove an instance of the attribute 'szTag' if the compare indicated
// ..by 'nHow' and 'nCompareValue' is true. If the last instance is
// ..removed, delete the attribute from the attribute list.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::RemoveIf(const char *szTag, int nHow, int nCompareValue)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_NOT_FOUND; // Not found.
if(LocalCompare(pAttr, nHow, nCompareValue))
{
pAttr->Count--; // Decrement instance count
if(pAttr->Count <= 0)
Delete(szTag); // Down to zero, drop it.
}
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// RemoveAll
//
// Delete all instances of the indicated attribute from the list.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::RemoveAll(const char *szTag)
{
return Delete(szTag);
}
/* ------------------------------------------------------------------------------------ */
// Has
//
// Return true if the list has at least one instance of the attribute.
/* ------------------------------------------------------------------------------------ */
bool CPersistentAttributes::Has(const char *szTag)
{
if(Locate(szTag) != NULL)
return true;
else
return false;
}
/* ------------------------------------------------------------------------------------ */
// Value
//
// Return the value of an attribute, if it exists, otherwise 0.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Value(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return 0;
else
return pAttr->Value;
}
// changed RF064
/* ------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::GetModifyAmt(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return 0;
else
return pAttr->ModifyAmt;
}
// end change RF064
// changed QD 12/15/05
/* ------------------------------------------------------------------------------------ */
// GetPowerUpLevel
//
// Get number of highlimit changes (PowerUp Level)
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::GetPowerUpLevel(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return 0;
else
return pAttr->PowerUpLevel;
}
// end change
/* ------------------------------------------------------------------------------------ */
// Low
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Low(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return 0;
else
return pAttr->ValueLowLimit;
}
/* ------------------------------------------------------------------------------------ */
// High
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::High(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return 0;
else
return pAttr->ValueHighLimit;
}
/* ------------------------------------------------------------------------------------ */
// Count
//
// Return the count of instance of an attribute, otherwise 0.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Count(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return 0;
else
return pAttr->Count;
}
/* ------------------------------------------------------------------------------------ */
// Compare
//
// Perform a comparison between the value of the attribute 'szTag' and
// ..'nCompareValue' of the type indicated by 'nHow'. Return the status
// ..of the compare, true or false.
/* ------------------------------------------------------------------------------------ */
bool CPersistentAttributes::Compare(const char *szTag, int nHow, int nCompareValue)
{
bool bResult = false;
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return false; // Attribute not found
return LocalCompare(pAttr, nHow, nCompareValue);
}
/* ------------------------------------------------------------------------------------ */
// GetAttribute
//
// Iterate through the attribute list. If pPrevious is NULL, then we get the
// ..list head, otherwise we get the pNext element.
/* ------------------------------------------------------------------------------------ */
PersistAttribute *CPersistentAttributes::GetAttribute(PersistAttribute *pPrevious)
{
if(pPrevious == NULL)
return theList;
else
return pPrevious->pNext;
}
/* ------------------------------------------------------------------------------------ */
// AllocateUserData
//
// Allocate user data to be associated with an attribute
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::AllocateUserData(const char *szTag, int nDataSize)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_FAILURE; // Attribute not found
if(pAttr->UserData)
delete pAttr->UserData;
pAttr->UserData = new unsigned char[nDataSize];
pAttr->UserDataSize = nDataSize;
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// DeleteUserData
//
// Delete the user data associated with an attribute.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::DeleteUserData(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return RGF_FAILURE; // Attribute not found
delete pAttr->UserData;
pAttr->UserData = NULL;
pAttr->UserDataSize = 0;
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// UserData
//
// Return a pointer to the user data associated with an attribute
/* ------------------------------------------------------------------------------------ */
unsigned char *CPersistentAttributes::UserData(const char *szTag)
{
PersistAttribute *pAttr = Locate(szTag);
if(pAttr == NULL)
return NULL; // Attribute not found
return pAttr->UserData;
}
/* ------------------------------------------------------------------------------------ */
// SaveTo
//
// Save all attributes in this object to the supplied file.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::SaveTo(FILE *SaveFD, bool type)
{
int nLen = 0;
PersistAttribute *pAttr = theList;
fwrite(&m_nCount, sizeof(int), 1, SaveFD);
while(pAttr != NULL)
{
nLen = strlen(pAttr->Name) + 1;
WRITEDATA(type, &nLen, sizeof(int), 1, SaveFD);
WRITEDATA(type, pAttr->Name, 1, nLen, SaveFD);
WRITEDATA(type, &(pAttr->Count), sizeof(int), 1, SaveFD);
WRITEDATA(type, &(pAttr->Value), sizeof(int), 1, SaveFD);
WRITEDATA(type, &(pAttr->ValueLowLimit), sizeof(int), 1, SaveFD);
WRITEDATA(type, &(pAttr->ValueHighLimit), sizeof(int), 1, SaveFD);
// changed QD 12/15/05
WRITEDATA(type, &(pAttr->PowerUpLevel), sizeof(int), 1, SaveFD);
// end change
WRITEDATA(type, &(pAttr->UserDataSize), sizeof(int), 1, SaveFD);
if(pAttr->UserDataSize != 0)
WRITEDATA(type, &(pAttr->UserData), 1, pAttr->UserDataSize, SaveFD);
pAttr = pAttr->pNext; // Next item
}
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// SaveAscii
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::SaveAscii(FILE *SaveFD)
{
PersistAttribute *pAttr = theList;
char szTemp[64];
while(pAttr != NULL)
{
fputs(pAttr->Name, SaveFD); fputs("\n", SaveFD);
sprintf(szTemp, "%d", pAttr->Value);
fputs(szTemp, SaveFD); fputs("\n", SaveFD);
sprintf(szTemp, "%d", pAttr->ValueLowLimit);
fputs(szTemp, SaveFD); fputs("\n", SaveFD);
sprintf(szTemp, "%d", pAttr->ValueHighLimit);
fputs(szTemp, SaveFD); fputs("\n", SaveFD);
pAttr = pAttr->pNext; // Next item
}
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// RestoreFrom
//
// Restore the attributes in this object from the supplied file.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::RestoreFrom(FILE *RestoreFD, bool type)
{
char szTempTag[256];
int nValue, nCount, nLen, nUserDataSize, Low, High, PowerUpLevel;
int nInFile;
unsigned char *theUserData;
PersistAttribute *pTemp = NULL;
Clear(); // Zap everything first
fread(&nInFile, sizeof(int), 1, RestoreFD);
for(int nTemp = 0; nTemp < nInFile; nTemp++)
{
READDATA(type, &nLen, sizeof(int), 1, RestoreFD); // Tag size
READDATA(type, &szTempTag, 1, nLen, RestoreFD);
READDATA(type, &nCount, sizeof(int), 1, RestoreFD);
READDATA(type, &nValue, sizeof(int), 1, RestoreFD);
READDATA(type, &Low, sizeof(int), 1, RestoreFD);
READDATA(type, &High, sizeof(int), 1, RestoreFD);
// changed QD 12/15/05
READDATA(type, &PowerUpLevel, sizeof(int), 1, RestoreFD);
// end change
READDATA(type, &nUserDataSize, sizeof(int), 1, RestoreFD);
if(nUserDataSize != 0)
{
theUserData = new unsigned char[nUserDataSize];
READDATA(type, &theUserData, 1, nUserDataSize, RestoreFD);
}
else
theUserData = NULL;
pTemp = AddNew(szTempTag, nValue);
pTemp->ValueLowLimit = Low;
pTemp->ValueHighLimit = High;
// changed QD 12/15/05
pTemp->PowerUpLevel = PowerUpLevel;
// end change
pTemp->Count = nCount; // Adjust count
pTemp->UserDataSize = nUserDataSize;
pTemp->UserData = theUserData;
}
return RGF_SUCCESS;
}
// ************ PRIVATE MEMBER FUNCTIONS ***********
/* ------------------------------------------------------------------------------------ */
// ClampValue
//
// Clamp the value of an attribute to its limits
/* ------------------------------------------------------------------------------------ */
void CPersistentAttributes::ClampValue(PersistAttribute *pAttr)
{
if(pAttr->Value < pAttr->ValueLowLimit)
pAttr->Value = pAttr->ValueLowLimit; // Clamp LOW limit
if(pAttr->Value > pAttr->ValueHighLimit)
pAttr->Value = pAttr->ValueHighLimit; // Clamp HIGH limit
return;
}
/* ------------------------------------------------------------------------------------ */
// Locate
//
// Search our internal list of a specific attribute, and return a
// ..pointer to it if it exists.
/* ------------------------------------------------------------------------------------ */
PersistAttribute *CPersistentAttributes::Locate(const char *szTag)
{
PersistAttribute *pTemp = theList;
while(pTemp != NULL)
{
if(!strcmp(szTag, pTemp->Name))
return pTemp; // This is the one!
pTemp = pTemp->pNext; // Keep looking
}
return NULL; // Couldn't find the attribute in our list.
}
/* ------------------------------------------------------------------------------------ */
// AddNew
//
// Add a new attribute on to the end of the list
/* ------------------------------------------------------------------------------------ */
PersistAttribute *CPersistentAttributes::AddNew(const char *szTag, int nValue)
{
PersistAttribute *pTemp = theList;
if(pTemp != NULL)
{
while(pTemp->pNext != NULL)
pTemp = pTemp->pNext; // Slew to end of list
}
// Construct the new attribute
PersistAttribute *pNew = new PersistAttribute;
pNew->Name = new char[strlen(szTag)+1];
strcpy(pNew->Name, szTag);
pNew->pNext = NULL;
pNew->Count = 1;
pNew->Value = nValue;
// changed RF064
pNew->ModifyAmt = 0;
// end change RF064
// changed QD 12/15/05
pNew->PowerUpLevel = 0;
// end change
// changed QD 07/15/05 - BUGFIX
pNew->ValueLowLimit = 0;
pNew->ValueHighLimit = 100;
// end change
pNew->UserData = NULL;
pNew->UserDataSize = 0;
// Check to see if this is the FIRST attribute
if(pTemp == NULL)
theList = pNew; // This is the list head
else
pTemp->pNext = pNew; // Add to the end of the list
m_nCount++; // List got bigger.
return pNew;
}
/* ------------------------------------------------------------------------------------ */
// Delete
//
// Locate an attribute and delete it from the list.
/* ------------------------------------------------------------------------------------ */
int CPersistentAttributes::Delete(const char *szTag)
{
PersistAttribute *pTemp = theList;
PersistAttribute *pPrev = NULL;
while(pTemp != NULL)
{
if(!strcmp(szTag, pTemp->Name))
{
if(pPrev == NULL)
{
// Gunning the list head
delete pTemp->Name;
delete pTemp;
theList = NULL; // Attribute list now EMPTY
}
else
{
// Gunning Just Another Entry
pPrev->pNext = pTemp->pNext; // Patch around entry to be deleted
delete pTemp->Name;
delete pTemp; // Attribute is DOA
}
m_nCount--; // List got smaller
return RGF_SUCCESS; // Zapped.
}
pPrev = pTemp; // Save previous
pTemp = pTemp->pNext; // Keep looking
}
return RGF_FAILURE; // Couldn't find it
}
/* ------------------------------------------------------------------------------------ */
// LocalCompare
//
// Perform a compare with the passed-in PersistAttribute
/* ------------------------------------------------------------------------------------ */
bool CPersistentAttributes::LocalCompare(PersistAttribute *pAttr, int nHow,
int nCompareValue)
{
bool bResult = false;
switch(nHow)
{
case RGF_ATTR_EQUAL:
if(pAttr->Value == nCompareValue)
bResult = true;
break;
case RGF_ATTR_LESS:
if(pAttr->Value < nCompareValue)
bResult = true;
break;
case RGF_ATTR_GREATER:
if(pAttr->Value > nCompareValue)
bResult = true;
break;
case RGF_ATTR_NOTEQUAL:
if(pAttr->Value != nCompareValue)
bResult = true;
break;
case RGF_ATTR_LESSEQ:
if(pAttr->Value <= nCompareValue)
bResult = true;
break;
case RGF_ATTR_GREATEREQ:
if(pAttr->Value >= nCompareValue)
bResult = true;
break;
}
return bResult;
}
/* ------------------------------------------------------------------------------------ */
// Dump
//
// Dump the contents of the attribute list to the debug window and the debug file.
// Typically used for debugging.
/* ------------------------------------------------------------------------------------ */
void CPersistentAttributes::Dump()
{
PersistAttribute *pTemp = theList;
char szDump[256];
while(pTemp != NULL)
{
sprintf(szDump, "%s count %d value %d", pTemp->Name, pTemp->Count, pTemp->Value);
CCD->ReportError(szDump, false);
pTemp = pTemp->pNext;
}
return;
}
/* ----------------------------------- END OF FILE ------------------------------------ */