-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBStore.cpp
More file actions
897 lines (713 loc) · 24.2 KB
/
BStore.cpp
File metadata and controls
897 lines (713 loc) · 24.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
#include <BStore.h>
////////////////////////
//Notes for Ben
///////////////////////////////
// for compressed keep track of if open in read or append and then switch between if genentry or save and close are called.
// for post pre to get rollback to work rewrite old flags at the end of the file uncompressed or find a way to write the old header uncomressed and everythingelse compressed. might need a defrag only option with no roll back
//better option might be to just have all but the version and type flags in the compressed section then the data would not be corupted (if that is the issue)
// defrag function.
BStore::BStore(bool header, bool type_checking):m_version(1.0){
// m_serialise=true;
m_type_checking=type_checking;
m_has_header=header;
Header=0;
if(header) Header=new BStore(false,false);
m_file_end=0;
m_file_name="";
m_open_file_end=0;
m_previous_file_end=0;
m_type=ram;
m_header_start=0;
m_flags_start=0;
m_lookup_start=0;
// m_lookup_size=0;
// m_current_loaded_entry=0;
m_update=false;
}
std::string BStore::GetVersion(){
std::stringstream tmp;
tmp<<"BStore:"<<m_version;
return tmp.str();
}
bool BStore::GetFlags(unsigned int file_end){
m_file_end=file_end; // not sure if i wnna keep thiese two here
m_open_file_end=file_end;
if(!output.Bseek( file_end-(sizeof(m_version)+sizeof(m_header_start)+sizeof(m_has_header)+sizeof(m_lookup_start)+sizeof(m_type_checking)+sizeof(m_type)+sizeof(m_previous_file_end)), SEEK_SET)){
std::clog<<"ERROR BStore::GetFlags : Error seeking start of flags"<<std::endl;
return false;
}
// std::cout<<"current pos-11="<<output.Btell()<<std::endl;
m_flags_start=output.Btell();
//std::cout<<"flast start pos="<<m_flags_start<<std::endl;
//std::cout<<"current pos5="<<output.Btell()<<std::endl;
float version=0;
if(!(output >> version)){
std::clog<<"ERROR BStore::GetFlags : Error reading version"<<std::endl;
return false;
}
if(version!=m_version) std::clog<<"Warning BStore::GetFlags : version missmatch m_version="<<m_version<<", file version="<<version<<". possibly incompatible"<<std::endl;
if(!(output >> m_header_start)){
std::clog<<"ERROR BStore::GetFlags : Error reading m_header_start"<<std::endl;
return false;
}
if(!(output >> m_has_header)){
std::clog<<"ERROR BStore::GetFlags : Error reading m_has_header"<<std::endl;
return false;
}
if(!(output >> m_lookup_start)){
std::clog<<"ERROR BStore::GetFlags : Error reading m_lookup_start"<<std::endl;
return false;
}
//std::cout<<"current pos="<<output.Btell()<<std::endl;
//if(!(output >> m_lookup_size)){
// std::clog<<"ERROR BStore::GetFlags : Error reading m_lookup_size"<<std::endl;
// return false;
// }
//std::cout<<"current pos="<<output.Btell()<<std::endl;
if(!(output >> m_type_checking)){
std::clog<<"ERROR BStore::GetFlags : Error reading m_type_checking"<<std::endl;
return false;
}
//std::cout<<"current pos="<<output.Btell()<<std::endl;
if(!(output >> m_type)){
std::clog<<"ERROR BStore::GetFlags : Error reading m_type"<<std::endl;
return false;
}
//std::cout<<"current pos="<<output.Btell()<<std::endl;
if(!(output >> m_previous_file_end)){
std::clog<<"ERROR BStore::GetFlags : Error reading m_precious_file_end"<<std::endl;
return false;
}
//std::cout<<"current pos="<<output.Btell()<<std::endl;
//std::cout<<"loading"<<std::endl;
//std::cout<<"m_lookup_start="<< m_lookup_start<<std::endl;
//std::cout<<"m_lookup_size="<< m_lookup_size<<std::endl;
//std::cout<<"m_type_checking="<< m_type_checking<<std::endl;
//std::cout<<"m_type="<< m_type<<std::endl;
//std::cout<<"m_previous_file_end="<< m_previous_file_end<<std::endl;
return true;
}
bool BStore::GetFlags(std::string filename, unsigned int file_end){
if(!output.Bclose()){
std::clog<<"ERROR BStore::GetFlags : Error closing any open file"<<std::endl;
return false;
}
if(!output.Bopen(filename, READ, UNCOMPRESSED)){
std::clog<<"ERROR BStore::GetFlags : Error opening file for flags"<<std::endl;
return false;
}
if(!output.Bseek(0, SEEK_END)){
std::clog<<"ERROR BStore::GetFlags : Error seeking end of file"<<std::endl;
return false;
}
if(file_end==0){
//if(!output.Bseek(0, SEEK_END)) return false;
m_file_end=output.Btell();
}
else m_file_end=file_end;
m_open_file_end=m_file_end;
//std::cout<<"flile end="<<m_file_end<<std::endl;
m_file_name=filename;
//std::cout<<"current pos-2="<<output.Btell()<<std::endl;
if(!GetFlags(m_file_end)){
std::clog<<"ERROR BStore::GetFlags : Error getting flags"<<std::endl;
return false;
}
// if(!output.Bseek(0, SEEK_END)) return false;
//std::cout<<"current pos="<<output.Btell()<<std::endl;
if(!output.Bclose()){
std::clog<<"ERROR BStore::GetFlags : Error closing file after retreiving flags"<<std::endl;
return false;
}
// m_entry=m_lookup_size;
return true;
}
bool BStore::Initnew(std::string filename, enum_type type, bool header, bool type_checking, unsigned int file_end){
m_file_name=filename;
struct stat buffer;
if(stat (filename.c_str(), &buffer) == 0){ //if file exists
//std::cout<<"file exists"<<std::endl;
if(!GetFlags(filename, file_end)){
std::clog<<"ERROR BStore::Initnew : Error in obtaining flags"<<std::endl;
return false;
}
if(m_type==uncompressed){
if(!output.Bopen(filename, READ_APPEND, UNCOMPRESSED)){
std::clog<<"ERROR Bstore::Initnew : Error opening uncompressed file"<<std::endl;
return false;
}
}
#ifdef ZLIB
else if(m_type==compressed){
if(!output.Bopen(filename, READ, COMPRESSED)){
std::clog<<"ERROR BStore::Initnew : Error openning compressed file"<<std::endl;
return false; //also need to reassign file end??
}
}
else if(m_type==post_pre_compress){
//std::cout<<"BEN loading file correctly"<<std::endl;
if(!output.Bopen(filename, READ_APPEND, POST_PRE_COMPRESS)){
std::clog<<"ERROR BStore::Initnew : Error openning post_pre_compressed file"<<std::endl;
return false;
}
if(!output.Bseek(0, SEEK_END)){
std::clog<<"ERROR BStore::Initnew : Error seeking end of file"<<std::endl;
return false;
}
m_file_end=output.Btell();
m_open_file_end=output.Btell();
}
#endif
else{
std::clog<<"ERROR BStore::Initnew : unkown m_type"<<std::endl;
return false;
}
if(!output.Bseek(m_lookup_start,SEEK_SET)){
std::clog<<"ERROR BStore::Initnew : Error seeking m_lookup_start"<<std::endl;
return false;
}
if(!(output >> m_lookup)){
std::clog<<"ERROR BStore::Initnew : Error retreiving lookup table"<<std::endl;
return false;
}
if(!GetHeader()){
std::clog<<"ERROR BStore::Initnew : Error retreiving header"<<std::endl;
return false;
}
if(!GetEntry(0)){
std::clog<<"ERROR BStore::Initnew : Error retreiving Entry(0)"<<std::endl;
return false;
}
}
else{
//std::cout<<"file doesnt exist"<<std::endl;
m_file_end=0;
//m_entry=0;
if(type==uncompressed){
if(!output.Bopen(filename, READ_APPEND, UNCOMPRESSED)){
std::clog<<"ERROR BStore::Initnew : Error openning new compressed file"<<std::endl;
return false;
}
}
#ifdef ZLIB
else if(type==compressed){
if(!output.Bopen(filename, APPEND, COMPRESSED)){
std::clog<<"ERROR BStore::Initnew : Error openning new compressed file"<<std::endl;
return false;
}
}
else if(type==post_pre_compress){
if(!output.Bopen(filename, READ_APPEND, POST_PRE_COMPRESS)){
std::clog<<"ERROR BStore::Initnew : Error openning new post_pre_compressed file"<<std::endl;
return false;
}
}
#endif
else{
std::clog<<"ERROR BStore::Initnew : unknown new file type"<<std::endl;
return false;
}
if(Header!=0){
delete Header;
Header=0;
}
m_has_header=header;
if(header) Header= new BStore(false,false);
m_type=type;
m_type_checking=type_checking;
}
m_update=false;
return true;
}
/*
void BStore::Init(){
// file = gzopen ("myfile.bin", "ab");
FILE * pFile;
pFile = fopen ("myfile.bin", "rb");
lseek(fileno(pFile), 0, SEEK_END);
//std::cout<<ftell(pFile)<<std::endl;
file_end=ftell(pFile);
lseek(fileno(pFile), 0-(sizeof(lookup_start)+sizeof(lookup_size)+sizeof(type)+sizeof(previous_file_end)), SEEK_CUR);
flags_start=ftell(pFile);
fread(&lookup_start, sizeof(lookup_start),1, pFile);
fread(&lookup_size, sizeof(lookup_size),1, pFile);
fread(&type, sizeof(type),1, pFile);
fread(&previous_file_end, sizeof(previous_file_end),1, pFile);
fclose(pFile);
entry=0;
file = gzopen ("myfile.bin", "wb");
update=false;
previous_file_end=0;
m_type_checking=false;
}
void BStore::Init2(){
FILE * pFile;
pFile = fopen ("myfile.bin", "rb");
lseek(fileno(pFile), 0, SEEK_END);
//std::cout<<ftell(pFile)<<std::endl;
file_end=ftell(pFile);
lseek(fileno(pFile), 0-(sizeof(lookup_start)+sizeof(lookup_size)+sizeof(type)+sizeof(previous_file_end)), SEEK_CUR);
flags_start=ftell(pFile);
fread(&lookup_start, sizeof(lookup_start),1, pFile);
fread(&lookup_size, sizeof(lookup_size),1, pFile);
fread(&type, sizeof(type),1, pFile);
fread(&previous_file_end, sizeof(previous_file_end),1, pFile);
// std::cout<<"lookup_start="<<lookup_start<<std::endl;
//std::cout<<"lookup_size="<<lookup_size<<std::endl;
fclose(pFile);
entry=0;
file = gzopen ("myfile.bin", "rb");
// gzbuffer(file, 128000);
gzseek(file,lookup_start,SEEK_SET);
for(unsigned int i=0;i<lookup_size; i++){
unsigned int key;
if(!gzread(file, &key, sizeof(key))) break;
unsigned int value;
if(!gzread(file, &value, sizeof(value))) break;
lookup[key]=value;
}
update=false;
m_type_checking=false;
}
*/
bool BStore::Save(unsigned int entry){ //defualt save in next entry so need to do lookup size to find it, overlad with entry number so as to overwrite in lookup table.
//std::cout<<"bob save start="<<output.Btell()<<std::endl;
//std::cout<<"save m_entry="<<m_entry<<std::endl;
m_update=true;
// std::cout<<"debug1 entry="<<entry<<std::endl;
// entry++;
//std::cout<<"m_lookup.size()="<<m_lookup.size()<<std::endl;
if(entry>=m_lookup.size()){
entry=m_lookup.size();
m_lookup.resize(m_lookup.size()+1);
}
//std::cout<<"debug2 entry="<<entry<<std::endl;
//std::cout<<"debug3 entry="<<entry<<std::endl;
//std::cout<<"save start2="<<output.Btell()<<std::endl;
//std::cout<<"m_file_end="<<m_file_end<<std::endl;
if(!output.Bseek(m_file_end,SEEK_SET)){
std::clog<<"ERROR BStore::Save : Error seeking end of file"<<std::endl;
return false;
}
//std::cout<<"save entry2="<<entry<<std::endl;
m_lookup.at(entry)=output.Btell();
//std::cout<<"m_variables.size()="<<m_variables.size()<<std::endl;
//std::cout<<"m_lookup.size()="<<m_lookup.size()<<std::endl;
//std::cout<<"before saving m_variables="<<output.Btell()<<std::endl;
if(!(output << m_variables)){
std::clog<<"ERROR BStore::Save : Error writing m_varaibles"<<std::endl;
return false;
}
//std::cout<<"after saving m_variables="<<output.Btell()<<std::endl;
if(m_type_checking){
if(!(output << m_type_info)){
std::clog<<"ERROR BStore::Save : Error writing m_type_info"<<std::endl;
return false;
}
}
m_file_end=output.Btell();
return true;
}
bool BStore::GetHeader(){
if(m_has_header){
if(Header!=0){
delete Header;
Header=0;
}
Header= new BStore(false, false);
output.Bseek(m_header_start, SEEK_SET);
if(!(output >> Header->m_variables)){
std::clog<<"ERROR BStore::GetHeader : Error retreiving header"<<std::endl;
return false;
}
}
return true;
}
bool BStore::GetEntry(unsigned int entry_request){
//std::cout<<"mode="<<output.m_mode<<std::endl;
// entry_request++;
// std::cout<<"gettentry called="<<entry_request<<std::endl;
//if(!entry_request) return false;
//std::cout<<"passed non zero check"<<std::endl;
if((m_lookup.size()-1)<entry_request){
std::clog<<"ERROR BStore::GetEntry : Entry outside of range"<<std::endl;
return false;
}
//if(!m_lookup.count(entry_request)) return false;
//std::cout<<"mode="<<output.m_mode<<std::endl;
//std::cout<<"passed get entry checks"<<std::endl;
Delete();
//std::cout<<"getting entry data: entry="<<entry_request<<", location is="<<m_lookup[entry_request]<<std::endl;
//std::cout<<"mode="<<output.m_mode<<std::endl;
if(!output.Bseek(m_lookup[entry_request], SEEK_SET)){
std::clog<<"ERROR BStore::GetEntry : Error seeking entry"<<std::endl;
return false;
}
//std::cout<<"mode="<<output.m_mode<<std::endl;
if(!(output >> m_variables)){
std::clog<<"ERROR BStore::GetEntry : Error reteriving entry varaibles"<<std::endl;
return false;
}
//std::cout<<"mode="<<output.m_mode<<std::endl;
if(m_type_checking){
if(!(output >> m_type_info)){
std::clog<<"ERROR BStore::GetEntry : Error reteriving m_type_info"<<std::endl;
return false;
}
}
//std::cout<<"mode="<<output.m_mode<<std::endl;
return true;
}
// write header and lookup
bool BStore::WriteHeader(){
m_header_start=output.Btell();
if(m_has_header>0 && !(output << Header->m_variables)){
std::clog<<"ERROR BStore::WriteHeader : Entry saving Header varaibles"<<std::endl;
return false;
} m_file_end=output.Btell();
return true;
}
bool BStore::WriteLookup(){
m_lookup_start= output.Btell();
//m_lookup_size=m_lookup.size();
m_previous_file_end=m_open_file_end;
if(!(output << m_lookup)){
std::clog<<"ERROR BStore::WriteLookup : Error saving lookup table"<<std::endl;
return false;
}
m_file_end=output.Btell();
return true;
}
bool BStore::WriteFlags(){
if(!(output << m_version)){
std::clog<<"ERROR BStore::WriteFlags : Error writing m_version"<<std::endl;
return false;
}
if(!(output << m_header_start)){
std::clog<<"ERROR BStore::WriteFlags : Error writing m_header_start"<<std::endl;
return false;
}
if(!(output << m_has_header)){
std::clog<<"ERROR BStore::WriteFlags : Error writing m_has_header"<<std::endl;
return false;
}
if(!(output << m_lookup_start)){
std::clog<<"ERROR BStore::WriteFlags : Error writing m_lookup_start"<<std::endl;
return false;
}
// if(!(output << m_lookup_size)){
// std::clog<<"ERROR BStore::WriteFlags : Error writing m_lookup_size"<<std::endl;
// return false;
//}
if(!(output << m_type_checking)){
std::clog<<"ERROR BStore::WriteFlags : Error writing m_type_checking"<<std::endl;
return false;
}
if(!(output << m_type)){
std::clog<<"ERROR BStore::WriteFlags : Error writing m_type"<<std::endl;
return false;
}
if(!(output << m_previous_file_end)){
std::clog<<"ERROR BStore::WriteFlags : Error writing m_previous_file_end"<<std::endl;
return false;
}
return true;
}
bool BStore::Close(){
if(!m_update) return output.Bclose(true);
else{
//std::cout<<"in close"<<std::endl;
//std::cout<<"btell="<<output.Btell()<<std::endl;
//std::cout<<"m_file_end"<<m_file_end<<std::endl;
// std::cout<<"s1"<<std::endl;
// write header and lookup
if(!output.Bseek(m_file_end,SEEK_SET)){
std::clog<<"ERROR BStore::Close : Error seeking m_file_end"<<std::endl;
return false;
}
// m_lookup[0]=output.Btell(); old header location need to fix
//std::cout<<"s2"<<std::endl;
// m_header_start=output.Btell();
// std::cout<<"btell="<<output.Btell()<<std::endl;
//std::cout<<"saving header"<<std::endl;
// if(m_has_header>0 && !(output << *m_header)) return false;
if(!WriteHeader()){
std::clog<<"ERROR BStore::Close : Error writing Header"<<std::endl;
return false;
}
//std::cout<<"s3"<<std::endl;
//std::cout<<"header saved"<<std::endl;
//std::cout<<"btell="<<output.Btell()<<std::endl;
// m_lookup_start= output.Btell();
//m_lookup_size=m_lookup.size();
//m_previous_file_end=m_open_file_end;
//std::cout<<"saving lookup"<<std::endl;
//if(!(output << m_lookup)) return false;
//std::cout<<"saved lookup"<<std::endl;
//std::cout<<"btell="<<output.Btell()<<std::endl;
//m_file_end=output.Btell();
//
if(!WriteLookup()){
std::clog<<"ERROR BStore::Close : Error writing loopup"<<std::endl;
return false;
} //std::cout<<"s4"<<std::endl;
if(m_type!=uncompressed && m_type!=ram){
//std::cout<<"s5"<<std::endl;
if(!output.Bclose()){
std::clog<<"ERROR BStore::Close : Error closing compressed file prior to flags"<<std::endl;
return false;
}
//std::cout<<"k1"<<std::endl;
//std::cout<<"s6"<<std::endl;
if(!output.Bopen(m_file_name,APPEND,UNCOMPRESSED)){
std::clog<<"ERROR BStore::Close : Error oppening file to append flags"<<std::endl;
return false;
} //std::cout<<"k2 ="<<output.pfile<<std::endl;
//std::cout<<"s7"<<std::endl;
}
//std::cout<<"k3 saving flag start pos="<<output.Btell()<<std::endl;
if(!output.Bseek(0,SEEK_END)){
std::clog<<"ERROR BStore::Close : Error seeking end of file"<<std::endl;
return false;
} // std::cout<<"s8"<<std::endl;
//std::cout<<" k3 saving flag start pos2="<<output.Btell()<<std::endl;
//std::cout<<"m_lookup_start="<< m_lookup_start<<std::endl;
//std::cout<<"m_lookup_size="<< m_lookup_size<<std::endl;
//std::cout<<"m_type_checking="<< m_type_checking<<std::endl;
//std::cout<<"m_type="<< m_type<<std::endl;
//std::cout<<"m_previous_file_end="<< m_previous_file_end<<std::endl;
//write flags
if(!WriteFlags()){
std::clog<<"ERROR BStore::Close : Error writing flags"<<std::endl;
return false;
} //std::cout<<"s9"<<std::endl;
//if(!(output << m_header_start)) return false;
//if(!(output << m_has_header)) return false;
//if(!(output << m_lookup_start)) return false;
//if(!(output << m_lookup_size)) return false;
//if(!(output << m_type_checking)) return false;
//if(!(output << m_type)) return false;
//if(!(output << m_previous_file_end)) return false;
//std::cout<<"k4 "<<output.Btell()<<std::endl;
if(!output.Bclose()){
std::clog<<"ERROR BStore::Close : Error closing file after writing flags"<<std::endl;
return false;
}
//std::cout<<"s10"<<std::endl;
//std::cout<<"k5"<<std::endl;
Delete();
//std::cout<<"s12"<<std::endl;
//std::cout<<"k6"<<std::endl;
return true;
}
return true; //need to check jsut added this to check compilations
}
//importing
bool BStore::Print(){
Print(false);
return true;
}
void BStore::Print(bool values){
for (std::map<std::string,BinaryStream>::iterator it=m_variables.begin(); it!=m_variables.end(); ++it){
std::cout<< it->first << " => ";
if(values) std::cout << it->second.buffer <<" :";
std::cout<<" "<<m_type_info[it->first]<<std::endl;
}
for (std::map<std::string,PointerWrapperBase*>::iterator it=m_ptrs.begin(); it!=m_ptrs.end(); ++it){
if(m_variables.count(it->first)==0){
std::cout<< it->first << " => ";
if(values) std::cout << it->second <<" :";
std::cout<<" Pointer "<<std::endl;
}
}
}
void BStore::Delete(){
m_variables.clear();
m_type_info.clear();
for (std::map<std::string,PointerWrapperBase*>::iterator it=m_ptrs.begin(); it!=m_ptrs.end(); ++it){
delete it->second;
it->second=0;
}
m_ptrs.clear();
}
void BStore::Remove(std::string key){
for (std::map<std::string,BinaryStream>::iterator it=m_variables.begin(); it!=m_variables.end(); ++it){
if(it->first==key){
m_variables.erase(it);
break;
}
}
for (std::map<std::string,PointerWrapperBase*>::iterator it=m_ptrs.begin(); it!=m_ptrs.end(); ++it){
if(it->first==key){
delete it->second;
it->second=0;
m_ptrs.erase(it);
break;
}
}
if(m_type_checking){
for (std::map<std::string,std::string>::iterator it=m_type_info.begin(); it!=m_type_info.end(); ++it){
if(it->first==key){
m_type_info.erase(it);
break;
}
}
}
}
void BStore::JsonParser(std::string input){
int type=0;
std::string key;
std::string value;
for(std::string::size_type i = 0; i < input.size(); ++i) {
if(input[i]=='\"')type++;
else if(type==1)key+=input[i];
else if(type==3)value+=input[i];
else if(type==4){
type=0;
m_variables[key] << value;
key="";
value="";
}
}
}
std::string BStore::Type(std::string key){
if(m_type_info.count(key)>0){
if(m_type_checking) return m_type_info[key];
else return "?";
}
else return "Not in Store";
}
bool BStore::Has(std::string key){
if(m_variables.count(key)>0) return true;
else return false;
}
bool BStore::DeleteEntry(unsigned int entry_request){
if(entry_request>=m_lookup.size()){
std::clog<<"ERROR BStore::DeleteEntry : Error entry "<<entry_request<<" not in BStore"<<std::endl;
return false;
}
m_lookup.erase(m_lookup.begin()+entry_request);
m_update=true;
return true;
}
bool BStore::Rollback(){
//ned to check if compressed or prepost to get location right sigh....
if(!m_previous_file_end){
std::clog<<"Warning BStore::Rollback : no rollback state exists"<<std::endl;
return false;
}
Delete();
m_lookup.clear();
Initnew(m_file_name, m_type, m_has_header, m_type_checking, m_previous_file_end); // is this better than just reloading lookup and headers etc?
return true;
}
bool BStore::Serialise(BinaryStream &bs){ // do return properly
///neive new testing
bs & output;
bs & m_lookup;
GetEntry(0);
#if 0
save;
if(bs.m_write){
finishfile
bs & output;
close
}
else{
bs & output;
close
open
}
#endif
#if 0
bs & m_lookup;
bs & m_file_end;
bs & m_file_name;
bs & m_open_file_end;
bs & m_previous_file_end;
bs & m_type;
bs & m_type_checking;
bs & m_has_header;
bs & m_header_start;
bs &m_flags_start;
bs & m_lookup_start;
bs & m_lookup_size;
bs & m_current_loaded_entry;
bs & m_update;
bs & m_file_type;
if(!GetHeader()) return false;
if(!GetEntry(0)) return false;
#endif
/////
#if 0 // uncomment here to start
//writing
if(bs.m_write){
if(not ram){
Close();
Initnew(reopen same file in ram);
}
if(output.buffer.length()>0){ //meaning iv stored more than one event in ram
if(m_update){
if(!output.Bseek(m_file_end,SEEK_SET))return false;
if(!WriteHeader()) return false;
if(!WriteLookup()) return false;
if(!WriteFlags()) return false;
}
unsigned int size= output.buffer.length();
unsigned int pos= bs.Btell();
if(!(bs & pos)) return false;
if(!(bs & size)) return false;
if(!(bs & output)) return false;
}
else{
if(!(bs & m_variables)) return false;
if typechecking if(!(bs & m_variables)) return false;
other variables, type etc
}
}
else{ //reading
unsigned int size=0;
unsigned int pos=0;
if(!(bs >> pos)) return false;
if(!(bs >> size)) return false;
if(bs.endpoint==RAM); output.buffer=bs.buffer.substr(pos,pos+size);
output.pfile=bs.pfile;
#ifdef ZLIB
output.gzfile=bs.gzfile;
#endif
output.m_mode=bs.m_mode;
output.m_endpoint = bs.m_endpoint;
if(!GetFlags(pos+size)) return false;
if(!output.Bseek(m_lookup_start,SEEK_SET)) return false;
if(!(output >> m_lookup)) return false;
if(!GetHeader()) return false;
if(!GetEntry(0)) return false;
}
#if 0
//std::cout<<"p1"<<std::endl;
if(!(bs & m_variables)) return false;
//std::cout<<"p2"<<std::endl;
if(!(bs & m_type)) return false;
//std::cout<<"p3"<<std::endl;
if(!(bs & m_type_checking)) return false;
//std::cout<<"p4"<<std::endl;
if(m_type_checking){
if(!(bs & m_type_info)) return false;
}
//std::cout<<"p5"<<std::endl;
if(!(bs & m_header)) return false;
//std::cout<<"p6"<<std::endl;
if(!(bs & m_lookup)) return false;
//std::cout<<"p7"<<std::endl;
#endif
#endif
return true;
}
unsigned int BStore::NumEntries(){
return m_lookup.size();
}
BStore::~BStore(){
delete Header;
Header=0;
Delete();
}