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 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
|
/***************************************************************************
textfile_evaluation.cpp - description
-------------------
begin : Thu Oct 06 2002
copyright : (C) 2002 by Martin Bickel
email : bickel@asc-hq.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <vector>
#include <algorithm>
#include <iostream>
#include "global.h"
#include "ascstring.h"
#include "textfileparser.h"
#include "textfile_evaluation.h"
#include "stringtokenizer.h"
#ifdef ParserLoadImages
#include <SDL_image.h>
// #include "basegfx.h"
#include "typen.h"
#include "graphics/blitter.h"
#include "fieldimageloader.h"
#include "graphics/surface2png.h"
#endif
#ifdef WIN32
#include "Windows.h"
#endif
#include <boost/regex.hpp>
template <class T>
class PropertyTemplate: public PropertyContainer::Property {
void operation_not_defined( const TextPropertyGroup::Entry& entry ) const;
protected:
T& property;
T defaultValue;
virtual T parse ( const TextPropertyGroup::Entry& entry ) const;
virtual T operation_mult ( const TextPropertyGroup::Entry& entry ) const;
virtual T operation_add ( const TextPropertyGroup::Entry& entry ) const;
virtual T operation_sub ( const TextPropertyGroup::Entry& entry ) const;
virtual T operation_eq ( const TextPropertyGroup::Entry& entry ) const;
public:
void evaluate ( );
PropertyTemplate ( T& property_ ) : PropertyContainer::Property ( false ), property ( property_ ) {};
PropertyTemplate ( T& property_, const T& defaultValue_ ) : PropertyContainer::Property ( true ), property ( property_ ), defaultValue ( defaultValue_) {};
};
typedef PropertyTemplate<int> PTI;
class IntegerProperty : public PTI {
protected:
ASCString toString ( ) const ;
int operation_eq ( const TextPropertyGroup::Entry& entry ) const;
int operation_add ( const TextPropertyGroup::Entry& entry ) const;
int operation_sub ( const TextPropertyGroup::Entry& entry ) const;
int operation_mult ( const TextPropertyGroup::Entry& entry ) const;
public:
IntegerProperty ( int& property_ ) : PTI ( property_ ) {};
IntegerProperty ( int& property_, int defaultValue_ ) : PTI ( property_, defaultValue_ ) {};
};
typedef PropertyTemplate<double> PTD;
class FloatProperty : public PTD {
protected:
ASCString toString ( ) const ;
double operation_eq ( const TextPropertyGroup::Entry& entry ) const;
double operation_add ( const TextPropertyGroup::Entry& entry ) const;
double operation_mult ( const TextPropertyGroup::Entry& entry ) const;
public:
FloatProperty ( double& property_ ) : PTD ( property_ ) {};
FloatProperty ( double& property_, double defaultValue_ ) : PTD ( property_, defaultValue_ ) {};
};
typedef PropertyTemplate<bool> PTB;
class BoolProperty : public PTB {
protected:
ASCString toString ( ) const;
bool operation_eq ( const TextPropertyGroup::Entry& entry ) const;
public:
BoolProperty ( bool& property_ ) : PTB ( property_ ) {};
BoolProperty ( bool& property_, bool defaultValue_ ) : PTB ( property_, defaultValue_ ) {};
};
typedef PropertyTemplate<ASCString> PTS;
class StringProperty : public PTS {
protected:
ASCString toString ( ) const;
ASCString operation_eq ( const TextPropertyGroup::Entry& entry ) const;
ASCString operation_add ( const TextPropertyGroup::Entry& entry ) const;
public:
StringProperty ( ASCString& property_ ) : PTS ( property_ ) {};
StringProperty ( ASCString& property_, const ASCString& defaultValue ) : PTS ( property_, defaultValue ) {};
};
typedef PropertyTemplate< vector<ASCString> > PTSA;
class StringArrayProperty : public PTSA {
typedef vector<ASCString> PropertyType;
protected:
ASCString toString ( ) const;
PropertyType operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
public:
StringArrayProperty ( vector<ASCString>& property_ ) : PTSA ( property_ ) {};
};
template <class T>
class ValArrayProperty: public PropertyTemplate< vector<T> > {
protected:
typedef vector<T> PropertyType;
PropertyType operation_add ( const TextPropertyGroup::Entry& entry ) const;
PropertyType operation_sub ( const TextPropertyGroup::Entry& entry ) const;
PropertyType operation_mult ( const TextPropertyGroup::Entry& entry ) const;
ValArrayProperty ( PropertyType& property_ ) : PropertyTemplate<vector<T> > ( property_ ) {};
};
typedef ValArrayProperty<int> PTIA;
class IntegerArrayProperty : public PTIA {
protected:
PropertyType operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
bool required;
bool hasDefault() {return !required; };
public:
IntegerArrayProperty ( vector<int>& property_, bool _required ) : PTIA ( property_ ), required(_required) {};
};
typedef ValArrayProperty<double> PTDA;
class DoubleArrayProperty : public PTDA {
protected:
PropertyType operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
public:
DoubleArrayProperty ( vector<double>& property_ ) : PTDA ( property_ ) {};
};
typedef PropertyTemplate< vector<IntRange> > PTIRA;
class IntRangeArrayProperty : public PTIRA {
typedef vector<IntRange> PropertyType;
bool required;
bool hasDefault() {return !required; };
protected:
PropertyType operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
public:
IntRangeArrayProperty ( vector<IntRange>& property_, bool _required ) : PTIRA ( property_ ), required(_required) {};
};
typedef PropertyTemplate< BitSet > PTTA;
class TagArrayProperty : public PTTA {
int tagNum;
const char** tags;
bool inverted;
protected:
BitSet operation_eq ( const TextPropertyGroup::Entry& entry ) const;
BitSet operation_add ( const TextPropertyGroup::Entry& entry ) const;
BitSet operation_sub ( const TextPropertyGroup::Entry& entry ) const;
ASCString toString ( ) const;
public:
TagArrayProperty ( BitSet& property_, int tagNum_, const char** tags_, bool inverted_ ) : PTTA ( property_ ), tagNum (tagNum_), tags ( tags_ ), inverted ( inverted_ ) {};
};
typedef PropertyTemplate<int> PTTI;
class TagIntProperty : public PTTI {
int tagNum;
const char** tags;
bool inverted;
protected:
int operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
int operation_add ( const TextPropertyGroup::Entry& entry ) const ;
int operation_sub ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
public:
TagIntProperty ( int& property_, int tagNum_, const char** tags_, bool inverted_ ) : PTTI ( property_ ), tagNum (tagNum_), tags ( tags_ ), inverted ( inverted_ ) {};
TagIntProperty ( int& property_, int tagNum_, const char** tags_, int defaultValue_, bool inverted_ ) : PTTI ( property_, defaultValue_ ), tagNum (tagNum_), tags ( tags_ ), inverted ( inverted_ ) {};
};
typedef PropertyTemplate<int> PTNI;
class NamedIntProperty : public PTNI {
int tagNum;
const char** tags;
protected:
int operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
public:
NamedIntProperty ( int& property_, int tagNum_, const char** tags_ ) : PTNI ( property_ ), tagNum (tagNum_), tags ( tags_ ) {};
NamedIntProperty ( int& property_, int tagNum_, const char** tags_, int defaultValue_ ) : PTNI ( property_, defaultValue_ ), tagNum (tagNum_), tags ( tags_ ) {};
};
/*
typedef PropertyTemplate<void*> PTIMG;
class ImageProperty : public PTIMG {
ASCString fileName;
protected:
void* operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
public:
ImageProperty ( void* &property_, const ASCString& fileName_ ) : PTIMG ( property_ ), fileName ( fileName_ ) {};
};
*/
typedef PropertyTemplate<Surface> PTIMG2;
class ASCImageProperty : public PTIMG2 {
typedef Surface PropertyType;
ASCString& fileName;
bool fieldMask;
protected:
PropertyType operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
public:
ASCImageProperty ( Surface &property_, ASCString& fileName_, bool applyFieldMask ) : PTIMG2 ( property_ ), fileName ( fileName_ ), fieldMask( applyFieldMask ) {};
};
/*
typedef PropertyTemplate< vector<void*> > PTIMGA;
class ImageArrayProperty : public PTIMGA {
typedef vector<void*> PropertyType;
ASCString fileName;
protected:
PropertyType operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
public:
ImageArrayProperty ( PropertyType &property_, const ASCString& fileName_ ) : PTIMGA ( property_ ), fileName ( fileName_ ) {};
};
*/
typedef PropertyTemplate< vector<Surface> > PTIMGA2;
class ASCImageArrayProperty : public PTIMGA2 {
typedef vector<Surface> PropertyType;
ASCString& fileName;
protected:
PropertyType operation_eq ( const TextPropertyGroup::Entry& entry ) const ;
ASCString toString ( ) const;
public:
ASCImageArrayProperty ( PropertyType &property_, ASCString& fileName_ ) : PTIMGA2 ( property_ ), fileName ( fileName_ ) {};
};
/*
void PropertyContainer :: run ( )
{
if ( isReading() )
for ( Properties::iterator i = properties.begin(); i != properties.end(); i++ )
if ( !(*i)->evaluated )
(*i)->evaluate();
closeBracket();
if ( levelDepth )
error ( "PropertyReadingContainer :: ~PropertyReadingContainer - still brackets open" );
}
*/
void PropertyReadingContainer :: writeProperty ( Property& p, const ASCString& value )
{
fatalError ( "Attempt to use PropertyReadingContainer :: writeProperty !");
}
void PropertyWritingContainer :: writeProperty ( Property& p, const ASCString& value )
{
ASCString output;
for ( int i = 0; i < levelDepth; i++ )
output += " ";
output += p.getLastName();
output += " = ";
size_t indent = output.length();
ASCString::size_type pos = value.find ( "\n" );
if ( pos != ASCString::npos ) {
output += "[";
ASCString::size_type oldpos = 0;
do {
output += value.substr( oldpos, pos-oldpos+1 );
oldpos = pos+1;
for ( int i = 0; i < indent; i++ )
output += " ";
if ( pos+1 < value.length() )
pos = value.find ( "\n", pos+1 );
else
pos = ASCString::npos;
} while ( pos != ASCString::npos );
output += value.substr( oldpos );
output += "]";
} else
output += value;
output += "\n";
stream.writeString ( output, false );
}
ASCString PropertyContainer :: getNameStack()
{
ASCString s;
for ( Level::iterator i = level.begin(); i != level.end(); ++i ) {
if ( s.length() )
s += ".";
s += *i;
}
return s;
}
void PropertyContainer :: openBracket( const ASCString& name )
{
level.push_back ( name );
levelDepth++;
}
void PropertyContainer :: closeBracket( )
{
level.pop_back();
levelDepth--;
}
void PropertyWritingContainer :: openBracket( const ASCString& name )
{
for ( int i = 0; i < levelDepth; i++ )
stream.writeString ( " ", false );
stream.writeString ( name + " { \n", false );
PropertyContainer :: openBracket ( name );
}
void PropertyWritingContainer :: closeBracket( )
{
ASCString name = level.back();
PropertyContainer :: closeBracket ( );
for ( int i = 0; i < levelDepth; i++ )
stream.writeString ( " ", false );
stream.writeString ( "} " + name + "\n", false );
}
void PropertyContainer::setup ( Property* p, const ASCString& name_ )
{
ASCString name;
for ( Level::iterator i = level.begin(); i != level.end(); i++ )
name += *i + ".";
name += name_;
p->setName( name, name_ );
p->setPropertyContainer ( this );
properties.push_back ( p );
p->evaluate();
}
void PropertyContainer::addInteger ( const ASCString& name, int& property )
{
IntegerProperty* ip = new IntegerProperty ( property );
setup ( ip, name );
}
void PropertyContainer::addInteger ( const ASCString& name, int& property, int defaultValue )
{
IntegerProperty* ip = new IntegerProperty ( property, defaultValue );
setup ( ip, name );
}
void PropertyContainer::addDFloat ( const ASCString& name, double& property )
{
FloatProperty* ip = new FloatProperty ( property );
setup ( ip, name );
}
void PropertyContainer::addDFloat ( const ASCString& name, double& property, double defaultValue )
{
FloatProperty* ip = new FloatProperty ( property, defaultValue );
setup ( ip, name );
}
void PropertyContainer::addBool ( const ASCString& name, bool& property )
{
BoolProperty* ip = new BoolProperty ( property );
setup ( ip, name );
}
void PropertyContainer::addBool ( const ASCString& name, bool& property, bool defaultValue )
{
BoolProperty* ip = new BoolProperty ( property, defaultValue );
setup ( ip, name );
}
void PropertyContainer::addString ( const ASCString& name, ASCString& property )
{
StringProperty* ip = new StringProperty ( property );
setup ( ip, name );
}
void PropertyContainer::addString ( const ASCString& name, ASCString& property, const ASCString& defaultValue )
{
StringProperty* ip = new StringProperty ( property, defaultValue );
setup ( ip, name );
}
void PropertyContainer::addStringArray ( const ASCString& name, vector<ASCString>& property )
{
StringArrayProperty* ip = new StringArrayProperty ( property );
setup ( ip, name );
}
void PropertyContainer::addIntegerArray ( const ASCString& name, vector<int>& property, bool required )
{
IntegerArrayProperty* ip = new IntegerArrayProperty ( property, required );
setup ( ip, name );
}
void PropertyContainer::addDFloatArray ( const ASCString& name, vector<double>& property )
{
DoubleArrayProperty* dp = new DoubleArrayProperty ( property );
setup ( dp, name );
}
void PropertyContainer::addDFloatArray ( const ASCString& name, vector<int>& property )
{
vector<double> dproperty;
for ( vector<int>::iterator i = property.begin(); i != property.end(); i++ )
dproperty.push_back (*i);
DoubleArrayProperty* dp = new DoubleArrayProperty ( dproperty );
setup ( dp, name );
property.clear();
for ( vector<double>::iterator i = dproperty.begin(); i != dproperty.end(); i++ )
property.push_back ( int(*i) );
}
void PropertyContainer::addIntRangeArray ( const ASCString& name, vector<IntRange>& property, bool required )
{
IntRangeArrayProperty* ip = new IntRangeArrayProperty ( property, required );
setup ( ip, name );
}
void PropertyContainer::addTagArray ( const ASCString& name, BitSet& property, int tagNum, const char** tags, bool inverted )
{
TagArrayProperty* ip = new TagArrayProperty ( property, tagNum, tags, inverted );
setup ( ip, name );
}
void PropertyContainer::addTagInteger ( const ASCString& name, int& property, int tagNum, const char** tags, bool inverted )
{
TagIntProperty* ip = new TagIntProperty ( property, tagNum, tags, inverted );
setup ( ip, name );
}
void PropertyContainer::addTagInteger ( const ASCString& name, int& property, int tagNum, const char** tags, int defaultValue, bool inverted )
{
TagIntProperty* ip = new TagIntProperty ( property, tagNum, tags, defaultValue, inverted );
setup ( ip, name );
}
void PropertyContainer::addNamedInteger ( const ASCString& name, int& property, int tagNum, const char** tags )
{
NamedIntProperty* ip = new NamedIntProperty ( property, tagNum, tags );
setup ( ip, name );
}
void PropertyContainer::addNamedInteger ( const ASCString& name, int& property, int tagNum, const char** tags, int defaultValue )
{
NamedIntProperty* ip = new NamedIntProperty ( property, tagNum, tags, defaultValue );
setup ( ip, name );
}
void PropertyContainer::addBreakpoint ()
{
bool breakpoint = false;
if ( isReading() ) {
addBool( "breakpoint", breakpoint, false);
if ( breakpoint ) {
#ifdef WIN32
DebugBreak();
#else
cerr << "breakpoint hit";
#endif
}
}
}
void PropertyContainer::storeContext( const ASCString& label )
{
storedContext[label] = make_pair( levelDepth, level );
}
bool PropertyContainer::restoreContext( const ASCString& label )
{
StoredContext::iterator pos = storedContext.find( label );
if ( pos != storedContext.end() ) {
levelDepth = pos->second.first;
level = pos->second.second;
return true;
}
return false;
}
#ifdef ParserLoadImages
void PropertyContainer::addImageArray ( const ASCString& name, vector<Surface> &property, ASCString& filename )
{
ASCImageArrayProperty* ip = new ASCImageArrayProperty ( property, filename );
setup ( ip, name );
}
void PropertyContainer::addImage ( const ASCString& name, Surface &property, ASCString& filename, bool applyFieldMask )
{
ASCImageProperty* ip = new ASCImageProperty ( property, filename, applyFieldMask );
setup ( ip, name );
}
#endif
void PropertyContainer::warning ( const ASCString& errmsg )
{
#ifdef converter
fatalError ( errmsg );
#else
::warningMessage( "file " + textPropertyGroup->fileName+ ": " + errmsg );
#endif
}
void PropertyContainer::error ( const ASCString& errmsg )
{
displayLogMessage ( 0, getLocation() + " : " + errmsg + "\n" );
throw ParsingError ( getLocation() + " : " + errmsg );
}
bool PropertyContainer::find ( const ASCString& name )
{
ASCString n;
for ( Level::iterator i = level.begin(); i != level.end(); i++ )
n += *i + ".";
n += name;
n.toLower();
return textPropertyGroup->find ( n ) != NULL;
}
PropertyReadingContainer :: PropertyReadingContainer ( const ASCString& baseName, TextPropertyGroup* tpg ) : PropertyContainer ( baseName, tpg, true )
{
openBracket ( baseName );
}
PropertyReadingContainer :: ~PropertyReadingContainer ( )
{
}
PropertyWritingContainer :: PropertyWritingContainer ( const ASCString& baseName, tnstream& stream ) : PropertyContainer ( baseName, NULL, false ), stream ( stream )
{
textPropertyGroup = new TextPropertyGroup();
textPropertyGroup->fileName = stream.getDeviceName();
textPropertyGroup->location = stream.getLocation();
openBracket ( baseName );
}
PropertyWritingContainer :: ~PropertyWritingContainer()
{
closeBracket();
if ( levelDepth )
error ( "PropertyWritingContainer :: ~PropertyWritingContainer - still brackets open" );
delete textPropertyGroup;
}
///////////////////// PropertyContainer::Properties //////////////////////////
void PropertyContainer::Property::setName ( const ASCString& name_, const ASCString& lastName_ )
{
name = name_ ;
name.toLower();
lastName = lastName_;
}
void PropertyContainer::Property::findEntry ()
{
if ( !propertyContainer )
fatalError ( "PropertyContainer::Property::evaluate - no propertyContainer ");
name.toLower();
if (!entry )
entry = propertyContainer->textPropertyGroup->find ( name );
if ( !entry && !hasDefault() )
propertyContainer->error ( "entry " + name +" not found" );
}
void PropertyContainer::Property::writeProperty( )
{
propertyContainer->writeProperty ( *this, toString() );
}
template <class T>
T PropertyTemplate<T>::parse ( const TextPropertyGroup::Entry& entry ) const
{
if ( entry.op == TextPropertyGroup::Entry::eq )
return operation_eq( entry );
if ( entry.op == TextPropertyGroup::Entry::alias_all )
operation_not_defined( entry );
if ( !entry.parent )
propertyContainer->error ( ASCString("PropertyContainer::PropertyTemplate::parse - no parent for operator ") + TextFormatParser::operations[entry.op] + " at entry " + entry.propertyName + " !");
switch ( entry.op ) {
case TextPropertyGroup::Entry::mult_eq : return operation_mult ( entry );
case TextPropertyGroup::Entry::add_eq : return operation_add ( entry );
case TextPropertyGroup::Entry::sub_eq : return operation_sub ( entry );
default:;
}
propertyContainer->error ( "PropertyTemplate::parse - invalid operator !");
return defaultValue;
}
template <class T>
void PropertyTemplate<T>::operation_not_defined(const TextPropertyGroup::Entry& entry) const
{
propertyContainer->error ( ASCString("operator ") + TextFormatParser::operations[entry.op] + " not defined for this type !\nEntry " + entry.propertyName);
}
template <class T>
T PropertyTemplate<T>::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
operation_not_defined( entry );
return T();
}
template <class T>
T PropertyTemplate<T>::operation_mult ( const TextPropertyGroup::Entry& entry ) const
{
operation_not_defined( entry );
return T();
}
template <class T>
T PropertyTemplate<T>::operation_add ( const TextPropertyGroup::Entry& entry ) const
{
operation_not_defined( entry );
return T();
}
template <class T>
T PropertyTemplate<T>::operation_sub ( const TextPropertyGroup::Entry& entry ) const
{
operation_not_defined( entry );
return T();
}
template <class T>
void PropertyTemplate<T>::evaluate ()
{
if ( evaluated )
return;
if ( propertyContainer->isReading() ) {
findEntry();
if ( entry ) {
property = parse ( *entry );
} else
property = defaultValue;
evaluated = true;
} else {
writeProperty();
evaluated = true;
}
}
int IntegerProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
char* p = NULL;
ASCString value = entry.value;
ASCString::size_type i;
while ( (i = value.find_first_of( " \t\n\r" )) != ASCString::npos )
value.erase( i, 1 );
while ( value.find( "0") == 0 && value.find( "0x") != 0) // removing leading zeroes
value.erase(0,1);
int res = strtol ( value.c_str(), &p, 0 ); // strtol(nptr, NULL, 10);
if ( *p != 0 && *p != ';' ) {
ASCString s = name + ": value "+ entry.value +" is no numerical value \n" ;
// propertyContainer->error ( s );
fprintf(stderr, "%s", s.c_str() );
}
return res;
}
int IntegerProperty::operation_add ( const TextPropertyGroup::Entry& entry ) const
{
return parse ( *entry.parent ) + operation_eq ( entry );
}
int IntegerProperty::operation_sub ( const TextPropertyGroup::Entry& entry ) const
{
return parse ( *entry.parent ) - operation_eq ( entry );
}
int IntegerProperty::operation_mult ( const TextPropertyGroup::Entry& entry ) const
{
return int ( double ( parse ( *entry.parent )) * atof ( entry.value.c_str() ));
}
ASCString IntegerProperty::toString ( ) const
{
return strrr ( property );
}
double FloatProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
return atof ( entry.value.c_str() ); // strtol(nptr, NULL, 10);
}
double FloatProperty::operation_add ( const TextPropertyGroup::Entry& entry ) const
{
return parse ( *entry.parent ) + operation_eq ( entry );
}
double FloatProperty::operation_mult ( const TextPropertyGroup::Entry& entry ) const
{
return double ( parse ( *entry.parent )) * atof ( entry.value.c_str() );
}
ASCString FloatProperty::toString ( ) const
{
ASCString s;
s.format("%f", property );
return s;
}
bool BoolProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
StringTokenizer st ( entry.value );
ASCString s = st.getNextToken();
if ( s.compare_ci ( "true" )==0 || s.compare_ci ( "1" )==0 )
return true;
else
if ( s.compare_ci ( "false" )==0 || s.compare_ci ( "0" )==0 )
return false;
else {
propertyContainer->error ( name + ": token "+ s +" unknown" );
return false;
}
}
ASCString BoolProperty::toString ( ) const
{
if ( property )
return "true";
else
return "false";
}
ASCString StringProperty::operation_add ( const TextPropertyGroup::Entry& entry ) const
{
/* ASCString s = entry.value;
while ( s.find ( "%s" ) != ASCString::npos )
s.replace ( s.find ( "%s" ), 2, parse ( *entry.parent ));
return s;
*/
return parse( *entry.parent ) + entry.value;
}
ASCString StringProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
ASCString s = entry.value;
ASCString::size_type pos = s.find_first_not_of ( TextFormatParser::whiteSpace );
if ( pos == ASCString::npos )
s.erase();
else
s.erase ( 0, pos );
pos = s.find_last_not_of ( TextFormatParser::whiteSpace );
if ( pos != ASCString::npos )
s.erase ( pos+1 );
return s;
}
ASCString StringProperty::toString ( ) const
{
ASCString valueToWrite = property ;
ASCString::size_type pos = 0;
static const int linewidth = 60;
do {
if ( pos + linewidth < valueToWrite.length() ) {
pos = valueToWrite.find_first_of ( TextFormatParser::whiteSpace, pos + linewidth );
if ( pos != ASCString::npos )
valueToWrite[pos] = '\n';
} else
pos = ASCString::npos;
} while ( pos != ASCString::npos );
return valueToWrite;
}
StringArrayProperty::PropertyType StringArrayProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
PropertyType sa;
StringTokenizer st ( entry.value, true );
ASCString s = st.getNextToken();
while ( !s.empty() ) {
sa.push_back ( s );
s = st.getNextToken();
}
return sa;
}
ASCString StringArrayProperty::toString ( ) const
{
ASCString valueToWrite;
for ( PropertyType::iterator i = property.begin(); i != property.end(); i++ ) {
valueToWrite += *i;
valueToWrite += " ";
}
return valueToWrite;
}
template<class T>
typename ValArrayProperty<T>::PropertyType ValArrayProperty<T>::operation_add ( const TextPropertyGroup::Entry& entry ) const
{
PropertyType child = this->operation_eq( entry );
PropertyType parent = this->parse ( *entry.parent );
if ( child.size() == parent.size() ) {
PropertyType res;
for ( int i = 0; i < child.size(); i++ )
res.push_back ( parent[i] + child[i] );
return res;
}
if ( child.size() == 1 ) {
PropertyType res;
for ( int i = 0; i < parent.size(); i++ )
res.push_back ( parent[i] + child[0] );
return res;
}
if ( parent.size() == 1 ) {
PropertyType res;
for ( int i = 0; i < child.size(); i++ )
res.push_back ( parent[0] + child[i] );
return res;
}
this->propertyContainer->error ( this->name + ": array dimensions do not agree" );
return child;
}
template<class T>
typename ValArrayProperty<T>::PropertyType ValArrayProperty<T>::operation_sub ( const TextPropertyGroup::Entry& entry ) const
{
PropertyType child = this->operation_eq( entry );
PropertyType parent = this->parse ( *entry.parent );
if ( child.size() == parent.size() ) {
PropertyType res;
for ( int i = 0; i < child.size(); i++ )
res.push_back ( parent[i] - child[i] );
return res;
}
if ( child.size() == 1 ) {
PropertyType res;
for ( int i = 0; i < parent.size(); i++ )
res.push_back ( parent[i] - child[0] );
return res;
}
if ( parent.size() == 1 ) {
PropertyType res;
for ( int i = 0; i < child.size(); i++ )
res.push_back ( parent[0] - child[i] );
return res;
}
this->propertyContainer->error ( this->name + ": array dimensions do not agree" );
return child;
}
template<class T>
typename ValArrayProperty<T>::PropertyType ValArrayProperty<T>::operation_mult ( const TextPropertyGroup::Entry& entry ) const
{
PropertyType child = this->operation_eq( entry );
PropertyType parent = this->parse ( *entry.parent );
if ( child.size() == parent.size() ) {
PropertyType res;
for ( int i = 0; i < child.size(); i++ )
res.push_back ( int( double(parent[i]) * child[i]) );
return res;
}
if ( child.size() == 1 ) {
PropertyType res;
for ( int i = 0; i < parent.size(); i++ )
res.push_back ( int( double(parent[i]) * child[0]) );
return res;
}
if ( parent.size() == 1 ) {
PropertyType res;
for ( int i = 0; i < child.size(); i++ )
res.push_back ( int( double(parent[0]) * child[i]) );
return res;
}
this->propertyContainer->error ( this->name + ": array dimensions do not agree" );
return parent;
}
IntegerArrayProperty::PropertyType IntegerArrayProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
PropertyType ia;
StringTokenizer st ( entry.value, true );
ASCString s = st.getNextToken();
while ( !s.empty() ) {
/*
if ( s.find( ";") == 0 )
st.skipTill('\n');
else
*/
ia.push_back ( atoi ( s.c_str() ));
s = st.getNextToken();
}
return ia;
}
ASCString DoubleArrayProperty::toString ( ) const
{
ASCString valueToWrite;
for ( PropertyType::iterator i = property.begin(); i != property.end(); i++ ) {
ASCString s;
s.format ( "%f", *i );
valueToWrite += s;
valueToWrite += " ";
}
return valueToWrite;
}
DoubleArrayProperty::PropertyType DoubleArrayProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
PropertyType ia;
StringTokenizer st ( entry.value, true );
ASCString s = st.getNextToken();
while ( !s.empty() ) {
ia.push_back ( atof ( s.c_str() ));
s = st.getNextToken();
}
return ia;
}
ASCString IntegerArrayProperty::toString ( ) const
{
ASCString valueToWrite;
for ( PropertyType::iterator i = property.begin(); i != property.end(); i++ ) {
valueToWrite += strrr ( *i );
valueToWrite += " ";
}
return valueToWrite;
}
IntRangeArrayProperty::PropertyType IntRangeArrayProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
return String2IntRangeVector( entry.value );
}
ASCString IntRangeArrayProperty::toString() const
{
ASCString valueToWrite;
for ( PropertyType::iterator i = property.begin(); i != property.end(); i++ ) {
if ( i->from != i->to ) {
valueToWrite += strrr ( i->from );
valueToWrite += "-";
valueToWrite += strrr ( i->to );
} else
valueToWrite += strrr ( i->from );
valueToWrite += " ";
}
return valueToWrite;
}
BitSet TagArrayProperty::operation_add ( const TextPropertyGroup::Entry& entry ) const
{
return parse ( *entry.parent ) | operation_eq ( entry );
}
BitSet TagArrayProperty::operation_sub ( const TextPropertyGroup::Entry& entry ) const
{
return parse ( *entry.parent ) & ~operation_eq ( entry );
}
BitSet TagArrayProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
BitSet bs;
if ( inverted )
bs.set();
else
bs.reset();
StringTokenizer st ( entry.value );
ASCString s = st.getNextToken();
while ( !s.empty() ) {
bool found = false;
for ( int i = 0; i < tagNum; i++ )
if ( s.compare_ci ( tags[i] )==0 ) {
if ( inverted )
bs.reset ( i );
else
bs.set ( i );
found = true;
break;
}
if ( !found )
propertyContainer->error ( name + ": token "+ s +" unknown" );
s = st.getNextToken();
}
return bs;
}
ASCString TagArrayProperty::toString() const
{
ASCString valueToWrite;
for ( int i = 0; i < tagNum; i++ )
if ( property.test(i) != inverted ) {
valueToWrite += tags[i];
valueToWrite += " ";
}
return valueToWrite;
}
int TagIntProperty::operation_add ( const TextPropertyGroup::Entry& entry ) const
{
return parse ( *entry.parent ) | operation_eq ( entry );
}
int TagIntProperty::operation_sub ( const TextPropertyGroup::Entry& entry ) const
{
return parse ( *entry.parent ) & ~operation_eq ( entry );
}
int TagIntProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
int i;
if ( inverted )
i = -1;
else
i = 0;
StringTokenizer st ( entry.value );
ASCString s = st.getNextToken();
while ( !s.empty() ) {
bool found = false;
for ( int j = 0; j < tagNum; j++ )
if ( s.compare_ci ( tags[j] )==0 ) {
i ^= 1 << j;
found = true;
break;
}
if ( !found )
propertyContainer->error ( name + ": token "+ s +" unknown" );
s = st.getNextToken();
}
return i;
}
ASCString TagIntProperty::toString() const
{
ASCString valueToWrite;
for ( int i = 0; i < tagNum; i++ )
if ( !!(property & (1 << i)) != inverted ) {
valueToWrite += tags[i];
valueToWrite += " ";
}
return valueToWrite;
}
int NamedIntProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
int i = 0;
StringTokenizer st ( entry.value );
ASCString s = st.getNextToken();
if ( !s.empty() ) {
bool found = false;
for ( int j = 0; j < tagNum; j++ )
if ( s.compare_ci ( tags[j] )==0 ) {
i = j;
found = true;
break;
}
if ( !found )
propertyContainer->error ( name + ": token "+ s +" unknown" );
}
return i;
}
ASCString NamedIntProperty::toString() const
{
return tags[property];
}
#ifdef ParserLoadImages
ASCImageProperty::PropertyType ASCImageProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
try {
fileName = entry.value;
return loadASCFieldImage( entry.value, fieldMask );
}
catch ( ASCexception ){
}
propertyContainer->error( "error accessing file " + entry.value );
return Surface();
}
ASCString ASCImageProperty::toString() const
{
ASCString valueToWrite = constructFileName( 0, "", extractFileName_withoutSuffix(fileName) + ".png");
writePNG( valueToWrite, property );
return valueToWrite;
}
ASCImageArrayProperty::PropertyType ASCImageArrayProperty::operation_eq ( const TextPropertyGroup::Entry& entry ) const
{
try {
boost::smatch what;
static boost::regex splitter( "\\s*(\\S+)\\s+(\\d+)\\s*");
if( boost::regex_match( entry.value, what, splitter)) {
ASCString imgName;
imgName.assign( what[1].first, what[1].second );
ASCString imgNumS;
imgNumS.assign( what[2].first, what[2].second );
int imgNum = atoi ( imgNumS.c_str() );
fileName = entry.value;
return loadASCFieldImageArray ( imgName, imgNum );
} else
propertyContainer->error( name + ": invalid format. Syntax is <ImageName> <ImageNum>" );
}
catch ( ASCexception ){
propertyContainer->error( "error accessing file " + entry.value );
}
return PropertyType();
}
ASCString ASCImageArrayProperty::toString() const
{
int num = property.size();
Surface s = Surface::createSurface( 1100, 100 * (num / 10 + 1), property.front().GetPixelFormat().BitsPerPixel(), 0 );
int cnt = 0;
for ( PropertyType::iterator i = property.begin(); i != property.end(); i++ ) {
if ( i->valid() )
megaBlitter<ColorTransform_None, ColorMerger_AlphaOverwrite, SourcePixelSelector_Plain, TargetPixelSelector_All> ( *i, s, SPoint( (cnt % 10) * 100, (cnt / 10) * 100), nullParam, nullParam, nullParam, nullParam);
cnt++;
}
ASCString valueToWrite = constructFileName( 0, "", extractFileName_withoutSuffix(fileName) + ".png");
writePNG ( valueToWrite , s );
return valueToWrite + " " + ASCString::toString(cnt);
}
#endif
|