forked from ennorehling/csmapfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatafile.cpp
More file actions
2343 lines (1956 loc) · 58.2 KB
/
Copy pathdatafile.cpp
File metadata and controls
2343 lines (1956 loc) · 58.2 KB
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
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "version.h"
#include "main.h"
#include "fxhelper.h"
#include "datafile.h"
#ifdef HAVE_BZ2LIB_H
#include <FXBZFileStream.h>
#endif
// ===========================
// === datakey implementation
void datakey::key(const FXString& type)
{
m_type = parseType(type);
if (m_type == TYPE_UNKNOWN)
m_key = type;
else
m_key.clear();
}
void datakey::value(const FXString& s)
{
m_value = s;
}
/*static*/ FXint datakey::parseType(const FXString& type)
{
if (type.empty())
return TYPE_EMPTY;
if (type == "Name")
return TYPE_NAME;
if (type == "Beschr")
return TYPE_DESCRIPTION;
if (type == "Terrain")
return TYPE_TERRAIN;
if (type == "Insel")
return TYPE_ISLAND;
if (type == "id")
return TYPE_ID;
if (type == "Partei")
return TYPE_FACTION;
if (type == "Parteiname")
return TYPE_FACTIONNAME;
if (type == "Anzahl")
return TYPE_NUMBER;
if (type == "Typ")
return TYPE_TYPE;
if (type == "skill")
return TYPE_SKILL;
if (type == "Konfiguration")
return TYPE_KONFIGURATION;
if (type == "visibility")
return TYPE_VISIBILITY;
if (type == "Runde")
return TYPE_TURN;
if (type == "Silber")
return TYPE_SILVER;
if (type == "Rekrutierungskosten")
return TYPE_RECRUITMENTCOST;
if (type == "Aura")
return TYPE_AURA;
if (type == "Auramax")
return TYPE_AURAMAX;
if (type == "Optionen")
return TYPE_OPTIONS;
if (type == "email")
return TYPE_EMAIL;
if (type == "banner")
return TYPE_BANNER;
if (type == "ejcOrdersConfirmed")
return TYPE_ORDERS_CONFIRMED;
if (type == "charset")
return TYPE_CHARSET;
return TYPE_UNKNOWN;
}
const FXString datakey::key() const
{
if (type() == TYPE_UNKNOWN)
return m_key;
else if (type() == TYPE_EMPTY)
return "";
else if (type() == TYPE_NAME)
return "Name";
else if (type() == TYPE_DESCRIPTION)
return "Beschr";
else if (type() == TYPE_TERRAIN)
return "Terrain";
else if (type() == TYPE_ISLAND)
return "Insel";
else if (type() == TYPE_ID)
return "id";
else if (type() == TYPE_FACTION)
return "Partei";
else if (type() == TYPE_FACTIONNAME)
return "Parteiname";
else if (type() == TYPE_NUMBER)
return "Anzahl";
else if (type() == TYPE_TYPE)
return "Typ";
else if (type() == TYPE_SKILL)
return "skill";
else if (type() == TYPE_KONFIGURATION)
return "Konfiguration";
else if (type() == TYPE_VISIBILITY)
return "visibility";
else if (type() == TYPE_TURN)
return "Runde";
else if (type() == TYPE_SILVER)
return "Silber";
else if (type() == TYPE_RECRUITMENTCOST)
return "Rekrutierungskosten";
else if (type() == TYPE_AURA)
return "Aura";
else if (type() == TYPE_AURAMAX)
return "Auramax";
else if (type() == TYPE_OPTIONS)
return "Optionen";
else if (type() == TYPE_EMAIL)
return "email";
else if (type() == TYPE_BANNER)
return "banner";
else if (type() == TYPE_ORDERS_CONFIRMED)
return "ejcOrdersConfirmed";
else if (type() == TYPE_CHARSET)
return "charset";
return "";
}
FXint datakey::getInt() const
{
return strtol(m_value.text(), NULL, 10);
}
// parses str and returns created datakey object or NULL pointer
FXbool datakey::parse(FXchar* str)
{
if (!str)
return false;
// skip indentation
while(*str && isspace(*str))
str++;
// Ist die Zeile ein Tag?
if (*str != '\"' && *str != '-' && (*str < '0' || *str > '9'))
return false;
char *srch = str, *semikolon = NULL;
for (; *srch; srch++) // search for semicolon ';' in this line
{
if (*srch == ';')
semikolon = srch;
else if (*srch == '\"')
semikolon = NULL;
}
// ok, so assign new values
if (semikolon)
key(semikolon+1); // ... and the FXString::operator=() copies the string itself.
else
key(""); // no semikolon found.
if (*str == '\"') // cuts the " at start and end of the string
{
str++;
if (semikolon)
{
if (semikolon[-1] == '\"')
semikolon--;
}
else
{
while (*srch == '\0' && srch > str)
srch--;
if (*srch == '\"')
*srch = '\0';
}
}
else
m_type |= TYPE_INTEGER; // no "" found -> integer
if (semikolon)
*semikolon = '\0'; // 0 byte...
/*
\\ ist ein \, \" ist ein ", einzelen " sind Anfang und Ende
von Strings, \n ist ein Zeilenumbruch, (\x ist x, falls keine Sonderregel)
*/
FXint length;
if (semikolon)
length = semikolon - str;
else
length = srch - str;
if (length < 0)
{
value("");
return true;
}
FXString val('\0', length+1); // Speicherplatz reservieren
char *ptr = &val[0];
for (; *str; str++, ptr++)
{
if (*str == '\\')
{
str++;
if (*str == 'n')
*ptr = '\n';
else if (*str == '\0')
break;
else
*ptr = *str;
}
else
*ptr = *str;
}
val.length(ptr - &val[0]);
value(val); // Wert speichern
return true;
}
// converts strings from file-charset (iso8859-15) to utf8
void datakey::iso2utf()
{
m_key = ::iso2utf(m_key);
m_value = ::iso2utf(m_value);
}
void datakey::utf2iso()
{
m_key = ::utf2iso(m_key);
m_value = ::utf2iso(m_value);
}
// =============================
// === datablock implementation
/*static*/ datablock::blocknames datablock::BLOCKNAMES[] =
{
{ TYPE_VERSION, "VERSION" },
{ TYPE_OPTIONS, "OPTIONEN" },
{ TYPE_FACTION, "PARTEI" },
{ TYPE_GROUP, "GRUPPE" },
{ TYPE_ALLIANCE, "ALLIANZ" },
{ TYPE_REGION, "REGION" },
{ TYPE_ISLAND, "ISLAND" },
{ TYPE_SCHEMEN, "SCHEMEN" },
{ TYPE_RESOURCE, "RESOURCE" },
{ TYPE_PRICES, "PREISE" },
{ TYPE_DURCHREISE, "DURCHREISE" },
{ TYPE_DURCHSCHIFFUNG, "DURCHSCHIFFUNG" },
{ TYPE_BORDER, "GRENZE" },
{ TYPE_BORDER, "BORDER" },
{ TYPE_SHIP, "SCHIFF" },
{ TYPE_BUILDING, "BURG" },
{ TYPE_UNIT, "EINHEIT" },
{ TYPE_UNITMESSAGES, "EINHEITSBOTSCHAFTEN" },
{ TYPE_TALENTS, "TALENTE" },
{ TYPE_SPELLS, "SPRUECHE" },
{ TYPE_COMBATSPELL, "KAMPFZAUBER" },
{ TYPE_ZAUBER, "ZAUBER" },
{ TYPE_KOMPONENTEN, "KOMPONENTEN" },
{ TYPE_TRANK, "TRANK" },
{ TYPE_ZUTATEN, "ZUTATEN" },
{ TYPE_ITEMS, "GEGENSTAENDE" },
{ TYPE_COMMANDS, "COMMANDS" },
{ TYPE_EFFECTS, "EFFECTS" },
{ TYPE_MESSAGE, "MESSAGE" },
{ TYPE_BATTLE, "BATTLE" },
{ TYPE_MESSAGETYPE, "MESSAGETYPE" },
{ TYPE_TRANSLATION, "TRANSLATION" },
{ 0, NULL }
};
/*static*/ FXint datablock::parseType(const FXString& type)
{
// region moved to top (performance)
if (type == "REGION")
return TYPE_REGION;
for (int i = 0; BLOCKNAMES[i].name ; i++)
if (type == BLOCKNAMES[i].name)
return BLOCKNAMES[i].id;
return TYPE_UNKNOWN;
}
const FXString datablock::terrainString() const
{
const FXString type = value(datakey::TYPE_TERRAIN);
if (!type.empty())
return type;
switch (terrain()) {
case TERRAIN_OCEAN:
return "Ozean";
case TERRAIN_SWAMP:
return "Sumpf";
case TERRAIN_PLAINS:
return "Ebene";
case TERRAIN_DESERT:
return FXString(L"W\u00fcste");
case TERRAIN_FOREST:
return "Wald";
case TERRAIN_HIGHLAND:
return "Hochland";
case TERRAIN_MOUNTAIN:
return "Berge";
case TERRAIN_GLACIER:
return "Gletscher";
case TERRAIN_VOLCANO:
return "Vulkan";
case TERRAIN_VOLCANO_ACTIVE:
return "Aktiver Vulkan";
case TERRAIN_ICEBERG:
return "Eisberg";
case TERRAIN_ICEFLOE:
return "Eisscholle";
case TERRAIN_FLOOR:
return "Gang";
case TERRAIN_WALL:
return "Wand";
case TERRAIN_PACKICE:
return "Packeis";
case TERRAIN_FIREWALL:
return "Feuerwand";
case TERRAIN_MAHLSTROM:
return "Mahlstrom";
}
return "Unbekannt";
}
/*static*/ FXint datablock::parseTerrain(const FXString& terrain)
{
// this terrain does not need textual representation
if (terrain == "Ozean")
return TERRAIN_OCEAN;
if (terrain == "Sumpf")
return TERRAIN_SWAMP;
if (terrain == "Ebene")
return TERRAIN_PLAINS;
if (terrain == "Wueste" || terrain == "W\u00fcste" || terrain == FXString(L"W\u00fcste"))
return TERRAIN_DESERT;
if (terrain == "Wald")
return TERRAIN_FOREST;
if (terrain == "Hochland")
return TERRAIN_HIGHLAND;
if (terrain == "Berge")
return TERRAIN_MOUNTAIN;
if (terrain == "Gletscher")
return TERRAIN_GLACIER;
if (terrain == "Vulkan")
return TERRAIN_VOLCANO;
if (terrain == "Eisberg")
return TERRAIN_ICEBERG;
if (terrain == "Feuerwand")
return TERRAIN_FIREWALL;
if (terrain == "Mahlstrom")
return TERRAIN_MAHLSTROM;
if (terrain == "Aktiver Vulkan")
return TERRAIN_VOLCANO_ACTIVE;
if (terrain == "Packeis")
return TERRAIN_PACKICE;
if (terrain == "Eisscholle")
return TERRAIN_ICEFLOE;
if (terrain == "Wand")
return TERRAIN_WALL;
if (terrain == "Gang")
return TERRAIN_FLOOR;
if (terrain == "Rauchender Vulkan")
return TERRAIN_VOLCANO_ACTIVE;
return TERRAIN_UNKNOWN;
}
/*static*/ FXint datablock::parseSpecialTerrain(const FXString& terrain)
{
// (terrain that uses image of another terrain)
// this terrain types should be kept as text,
// so if you resave the file, it's exact terrain type (_Aktiver_ Vulkan)
// could be saved.
if (terrain == "Nebel")
return TERRAIN_GLACIER;
else if (terrain == "Dichter Nebel")
return TERRAIN_FIREWALL;
return TERRAIN_UNKNOWN;
}
/*static*/ FXString datablock::planeName(FXint plane)
{
if (plane == 0)
return "Eressea";
if (plane == 1)
return "Astralraum";
if (plane == 1137)
return "Arena";
if (plane == 59034966)
return "Eternath";
if (plane == 2000)
return "Weihnachtsinsel";
// else...
return FXString().format("Ebene %d", plane);
}
// parses str and returns created datablock object or NULL pointer
FXbool datablock::parse(FXchar* str)
{
if (!str)
return false;
// skip indentation
while (*str && isspace(*str))
str++;
if (*str < 'A' || *str > 'Z')
return false;
char* srch = str, *space = NULL;
for (; *srch; srch++)
{
if (*srch == ' ' && !space)
space = srch;
if (*srch == '\"') // can't be a '\"'
return false;
if (*srch == ';') // ';' is a comment, stop here
{
*srch = '\0';
break;
}
}
// unset flags
flags(FLAG_NONE);
if (space)
{
*space++ = '\0';
infostr(space);
}
else
infostr(FXString());
string(str);
// unset all other states
terrain(TERRAIN_UNKNOWN);
depth(0);
attachment(NULL);
return true;
}
datablock::datablock() : m_type(0), m_info(0), m_x(0),m_y(0), m_terrain(0), m_flags(0), m_depth(0), m_attachment(0)
{
}
datablock::~datablock()
{
delete m_attachment;
}
void datablock::string(const FXString& s)
{
m_type = parseType(s);
if (m_type == TYPE_UNKNOWN)
m_string = s;
else
m_string.clear();
}
const FXString datablock::string() const
{
if (m_type == TYPE_UNKNOWN)
return m_string;
for (int i = 0; BLOCKNAMES[i].name; i++)
if (m_type == BLOCKNAMES[i].id)
return BLOCKNAMES[i].name;
return "";
}
// return info() as base36
FXString datablock::id() const
{
FXchar buf[35], b36[] = "0123456789abcdefghijkLmnopqrstuvwxyz";
FXchar *p=buf+34;
FXint nn=(FXint)info();
FXbool negativ = false;
if (nn < 0)
{
nn = -nn;
negativ = true;
}
*p='\0';
do{
*--p=b36[nn%36];
nn/=36;
}
while(nn);
if (negativ)
*--p='-';
FXASSERT(buf<=p);
return FXString(p,buf+34-p);
}
void datablock::infostr(const FXString& s)
{
flags(flags() & ~(FLAG_BLOCKID_BIT0|FLAG_BLOCKID_BIT1));
m_x = m_y = m_info = 0;
if (!s.empty())
{
if (s.find(' ') == -1)
{
setFlags(FLAG_BLOCKID_BIT0);
m_info = strtol(s.text(), NULL, 10);
}
else
{
setFlags(FLAG_BLOCKID_BIT1);
m_x = strtol(s.text(), NULL, 10);
m_y = strtol(s.section(' ',1).text(), NULL, 10);
m_info = strtol(s.section(' ',2).text(), NULL, 10);
if (m_info)
setFlags(FLAG_BLOCKID_BIT0);
}
}
}
void datablock::terrain(FXint terrain)
{
m_terrain = terrain;
}
void datablock::flags(FXint flags)
{
m_flags = flags;
}
void datablock::setFlags(FXint flags)
{
m_flags |= flags;
}
void datablock::depth(FXint depth)
{
m_depth = depth;
}
void datablock::attachment(::attachment* attach)
{
delete m_attachment; // free old attachment and set new
m_attachment = attach;
}
const FXString datablock::value(const FXchar* key) const
{
for(datakey::list_type::const_iterator srch = m_data.begin(); srch != m_data.end(); srch++)
if (srch->key() == key)
return srch->value();
return "";
}
const FXString datablock::value(FXint key) const
{
for(datakey::list_type::const_iterator srch = m_data.begin(); srch != m_data.end(); srch++)
if (srch->type() == key)
return srch->value();
return "";
}
FXint datablock::valueInt(const FXchar* key, FXint def /* = 0 */) const
{
for(datakey::list_type::const_iterator srch = m_data.begin(); srch != m_data.end(); srch++)
if (srch->key() == key)
return srch->getInt();
return def;
}
FXint datablock::valueInt(FXint key, FXint def /* = 0 */) const
{
for(datakey::list_type::const_iterator srch = m_data.begin(); srch != m_data.end(); srch++)
if (srch->type() == key)
return srch->getInt();
return def;
}
const datakey* datablock::valueKey(FXint key) const
{
for(datakey::list_type::const_iterator srch = m_data.begin(); srch != m_data.end(); srch++)
if (srch->type() == key)
return &*srch;
return NULL;
}
void datablock::iso2utf()
{
m_string = ::FXString(m_string);
}
void datablock::utf2iso()
{
m_string = ::utf2iso(m_string);
}
// ===========================
// === datafile implementation
datafile::datafile() : m_cmds(), m_recruitment(0), m_turn(0), m_activefaction(end())
{
m_cmds.modified = false;
}
// helper function that strips lines and return pointer to next line
static inline char* getNextLine(char* str)
{
char* next = str;
// search start of next line
while(*next && *next != '\n')
next++;
// overwrite cr and newline
if (next > str && next[-1] == '\r')
next[-1] = '\0';
if(*next)
*next++ = '\0'; // mark end of line
return next;
}
const FXchar* UTF8BOM = "\xEF\xBB\xBF";
// loads file, parses it and returns number of block
FXint datafile::load(const FXchar* filename)
{
this->filename(filename);
this->utf8cr(false);
if (!filename)
return 0;
FXString data;
FXString filename_str = filename;
if (filename_str.length() >= 7 && filename_str.right(7) == ".cr.bz2")
{
#ifdef HAVE_BZ2LIB_H
// load bzip2 packed CR
FXBZFileStream file;
file.open(filename,FXStreamLoad);
if (file.status() != FXStreamOK)
{
this->filename("");
throw std::runtime_error("Datei konnte nicht ge\u00f6ffnet werden.");
}
FXString buffer;
buffer.length(1024*100+1);
do
{
memset(&buffer[0], 0, buffer.length());
file.load(&buffer[0], buffer.length()-1);
data.append(buffer.text());
} while(file.status() == FXStreamOK);
file.close();
#endif
}
else if (filename_str.length() >= 4 && filename_str.right(4) == ".xml")
{
this->filename("");
throw std::runtime_error("Dateiformat nicht implementiert: XML.");
}
else
{
// load plain text CR
FXFileStream file;
file.open(filename,FXStreamLoad);
if (file.status() != FXStreamOK)
{
this->filename("");
throw std::runtime_error("Datei konnte nicht ge\u00f6ffnet werden.");
}
FXString buffer;
buffer.length(1024*100+1);
do
{
memset(&buffer[0], 0, buffer.length());
file.load(&buffer[0], buffer.length()-1);
data.append(buffer.text());
} while(file.status() == FXStreamOK);
file.close();
}
// ...
m_blocks.clear();
datablock *block = NULL, newblock;
datakey key;
FXchar *ptr, *next = &data[0];
// check for utf8 BOM: EF BB BF
bool utf8mode = utf8cr();
if (!strncmp(next, UTF8BOM, strlen(UTF8BOM)))
{
utf8cr(true);
utf8mode = true;
next += strlen(UTF8BOM);
}
while(*(ptr = next))
{
// strip the line
next = getNextLine(ptr);
// erster versuch: enth\u00e4lt die Zeile einen datakey?
if (key.parse(ptr))
{
if (!utf8mode)
key.iso2utf();
if (block)
{
if (key.type() == datakey::TYPE_TERRAIN)
{
FXint terrain = datablock::parseTerrain(key.value());
if (terrain == datablock::TERRAIN_UNKNOWN)
{
block->data().push_back(key); // need textual representation of terrain type
terrain = datablock::parseSpecialTerrain(key.value());
}
block->terrain(terrain);
}
else
block->data().push_back(key); // appends "Value";Key - tags
// check if charset set
if (key.type() == datakey::TYPE_CHARSET && block->type() == datablock::TYPE_VERSION)
{
FXString charset = key.value();
charset.upper();
if (charset == "UTF-8" || charset == "UTF8")
{
utf8cr(true);
utf8mode = true;
}
}
}
}
// zweiter versuch: enth\u00e4lt die Zeile einen datablock-header?
else if (newblock.parse(ptr))
{
if (!utf8mode)
newblock.iso2utf();
m_blocks.push_back(newblock); // appends BLOCK Info - tags
block = &m_blocks.back();
}
}
createHierarchy(); // set depth field of blocks
createHashTables(); // creates hash tables and set region flags
return blocks().size();
}
// saves file
FXint datafile::save(const FXchar* filename, bool replace, bool merge_commands /*= false*/)
{
if (!filename)
return 0;
// Soll \u00fcberschrieben werden? Wenn nicht, pr\u00fcfen, ob Datei vorhanden
if (replace == false)
{
FXFileStream filestr;
filestr.open(filename, FXStreamLoad);
if (filestr.status() == FXStreamOK)
{
filestr.close();
return -2; // Datei existiert schon, Fehler!
}
}
// Datei zum Schreiben \u00f6ffnen
std::ostringstream file;
FXFileStream plainfile;
#ifdef HAVE_BZ2LIB_H
FXBZFileStream bzip2file;
#endif
FXStream *filestr_ptr;
FXString filename_str = filename;
if (filename_str.length() >= 7 && filename_str.right(7) == ".cr.bz2")
{
#ifdef HAVE_BZ2LIB_H
bzip2file.open(filename, FXStreamSave);
filestr_ptr = &bzip2file;
#endif
}
else if (filename_str.length() >= 4 && filename_str.right(4) == ".xml")
{
return -1;
}
else
{
plainfile.open(filename, FXStreamSave);
filestr_ptr = &plainfile;
}
FXStream& filestr = *filestr_ptr;
if (filestr.status() != FXStreamOK)
{
return -1;
}
// Alles ok soweit...
this->filename(filename);
//this->utf8cr(false);
const bool utf8mode = utf8cr();
if (utf8mode)
file << UTF8BOM;
const datablock::itor end = blocks().end();
for (datablock::itor block = blocks().begin(); block != end; block++)
{
bool hideKeys = false;
// Blocknamen + ID-Nummern ausgeben
file << block->string();
// VERSION auf mindestens 64 hochsetzen
if (block->type() == datablock::TYPE_VERSION)
if (block->info() < 64)
block->infostr("64");
if (block->type() == datablock::TYPE_REGION || block->type() == datablock::TYPE_BATTLE ||
block->x() != 0 || block->y() != 0)
file << ' ' << block->x() << ' ' << block->y();
if (block->type() == datablock::TYPE_COMBATSPELL || block->info())
file << ' ' << block->info();
file << std::endl;
// Name-Tag und Terrain-Tag ausgeben
if (block->type() == datablock::TYPE_REGION)
{
FXString name = block->value(datakey::TYPE_NAME);
if (!utf8mode)
name = utf2iso(name);
if (!name.empty())
{
file << '\"';
for (int i = 0; i < name.length(); i++)
{
FXchar c = name[i];
if (c == '\\' || c == '\"')
file << '\\';
if (c == '\n')
file << "\\n";
else
file << c;
}
file << "\";Name" << std::endl;
}
if (utf8mode)
file << '\"' << block->terrainString().text() << "\";Terrain" << std::endl;
else
file << '\"' << block->terrainString() << "\";Terrain" << std::endl;
}
// Konfiguration-Block anpassen und Charset auf ISO-8859-1 setzen
else if (block->type() == datablock::TYPE_VERSION)
{
bool charsettag = false;
datakey charset;
charset.key("charset");
if (utf8mode)
charset.value("UTF-8");
else
charset.value("ISO-8859-1");
for (datakey::itor tags = block->data().begin(); tags != block->data().end(); tags++)
{
if (tags->type() == datakey::TYPE_KONFIGURATION)
{
tags->value(CSMAP_APP_TITLE_VERSION);
}
if (tags->type() == datakey::TYPE_CHARSET)
{
tags->value(charset.value());
charsettag = true;
}
}
if (!charsettag)
block->data().insert(block->data().begin(), charset);
}
else if (block->type() == datablock::TYPE_UNIT && merge_commands)
{
// search for command block of unit
datablock::itor cmd = block;
for (cmd++; cmd != end && cmd->depth() > block->depth(); cmd++)
if (cmd->type() == datablock::TYPE_COMMANDS)
break; // found
bool confirmed = block->valueInt(datakey::TYPE_ORDERS_CONFIRMED) != 0;
if (cmd != end && cmd->type() == datablock::TYPE_COMMANDS)
{
// att_commands' confirmed attribute overwrites the tag
if (att_commands* cmds = dynamic_cast<att_commands*>(cmd->attachment()))
confirmed = cmds->confirmed;
}
if (confirmed) // datakey::TYPE_ORDERS_CONFIRMED
file << "1;ejcOrdersConfirmed" << std::endl;
}
else if (block->type() == datablock::TYPE_COMMANDS && merge_commands)
{
if (att_commands* cmds = dynamic_cast<att_commands*>(block->attachment()))
{
hideKeys = true; // hide original commands
for (att_commands::cmdlist_t::iterator it = cmds->commands.begin(); it != cmds->commands.end(); it++)
{
file << '\"';
FXString value = *it;
if (!utf8mode)
value = utf2iso(value);
/*
\\ ist ein \, \" ist ein ", einzelen " sind Anfang und Ende
von Strings, \n ist ein Zeilenumbruch, (\x ist x, falls keine Sonderregel)
*/
for (int i = 0; i < value.length(); i++)
{
FXchar c = value[i];
if (c == '\\' || c == '\"')
file << '\\';
if (c == '\n')