@@ -225,12 +225,9 @@ bool WsjcppObjTree::writeTreeToFile(const std::string &sFilename, std::string &s
225
225
// write id
226
226
this ->writeUInt32 (f, pNode->getId ());
227
227
228
- // write data size
229
- int nDataSize = pNode->getDataSize ();
230
- this ->writeUInt32 (f, nDataSize);
231
-
232
- // write data
233
- f.write (pNode->getData (), nDataSize);
228
+ if (!pNode->writeDataPartToFile (f, sError )) {
229
+ return false ;
230
+ }
234
231
}
235
232
f.close ();
236
233
return true ;
@@ -440,15 +437,13 @@ void WsjcppObjTreeNodeString::setValue(const std::string &sValue) {
440
437
441
438
// ---------------------------------------------------------------------
442
439
443
- int WsjcppObjTreeNodeString::getDataSize () {
444
- return m_sValue.size ();
445
- }
446
-
447
- // ---------------------------------------------------------------------
448
-
449
- const char *WsjcppObjTreeNodeString::getData () {
450
- return m_sValue.c_str ();
451
- }
440
+ bool WsjcppObjTreeNodeString::writeDataPartToFile (std::ofstream &f, std::string &sError ) {
441
+ uint32_t nStringLen = m_sValue.size ();
442
+ const char *pData = reinterpret_cast <const char *>(&nStringLen);
443
+ f.write (pData, 4 ); // Write size of string
444
+ f.write (m_sValue.c_str (), nStringLen);
445
+ return true ;
446
+ };
452
447
453
448
// ---------------------------------------------------------------------
454
449
@@ -500,29 +495,18 @@ void WsjcppObjTreeNodeInteger::setValue(int32_t nValue) {
500
495
501
496
// ---------------------------------------------------------------------
502
497
503
- int WsjcppObjTreeNodeInteger::getDataSize () {
504
- return sizeof (uint32_t );
505
- }
506
-
507
- // ---------------------------------------------------------------------
508
-
509
- const char *WsjcppObjTreeNodeInteger::getData () {
510
- const char *p = reinterpret_cast <const char *>(&m_nValue);
511
- return p;
512
- }
498
+ bool WsjcppObjTreeNodeInteger::writeDataPartToFile (std::ofstream &f, std::string &sError ) {
499
+ static_assert (sizeof (uint32_t ) == 4 , " Expected sizeof(uint32_t) == 4" );
500
+ const char *pData = reinterpret_cast <const char *>(&m_nValue);
501
+ f.write (pData, 4 );
502
+ return true ;
503
+ };
513
504
514
505
// ---------------------------------------------------------------------
515
506
516
507
bool WsjcppObjTreeNodeInteger::readDataPartFromFile (std::ifstream &f, std::string &sError ) {
517
- // size
518
- // TODO remove - because this not need
519
- char arrBytes[4 ];
520
- f.read (arrBytes, 4 );
521
- if (!f) {
522
- sError = " WsjcppObjTreeNodeInteger. Could not read string len. File broken. Can read " + std::to_string (f.gcount ());
523
- return false ;
524
- }
525
508
// value
509
+ char arrBytes[4 ];
526
510
f.read (arrBytes, 4 );
527
511
if (!f) {
528
512
sError = " WsjcppObjTreeNodeInteger. Could not read string len. File broken. Can read " + std::to_string (f.gcount ());
@@ -561,30 +545,19 @@ void WsjcppObjTreeNodeFloat::setValue(float nValue) {
561
545
562
546
// ---------------------------------------------------------------------
563
547
564
- int WsjcppObjTreeNodeFloat::getDataSize ( ) {
548
+ bool WsjcppObjTreeNodeFloat::writeDataPartToFile (std::ofstream &f, std::string & sError ) {
565
549
static_assert (sizeof (float ) == 4 , " Expected sizeof(float) == 4" );
566
- return sizeof (float );
567
- }
568
-
569
- // ---------------------------------------------------------------------
570
-
571
- const char *WsjcppObjTreeNodeFloat::getData () {
572
- return reinterpret_cast <const char *>(&m_nValue);
573
- }
550
+ const char *pData = reinterpret_cast <const char *>(&m_nValue);
551
+ f.write (pData, 4 );
552
+ return true ;
553
+ };
574
554
575
555
// ---------------------------------------------------------------------
576
556
577
557
bool WsjcppObjTreeNodeFloat::readDataPartFromFile (std::ifstream &f, std::string &sError ) {
578
558
static_assert (sizeof (float ) == 4 , " Expected sizeof(float) == 4" );
579
- // size
580
- // TODO remove - because this not need
581
- char arrBytes[4 ];
582
- f.read (arrBytes, 4 );
583
- if (!f) {
584
- sError = " WsjcppObjTreeNodeInteger. Could not read string len. File broken. Can read " + std::to_string (f.gcount ());
585
- return false ;
586
- }
587
559
// value
560
+ char arrBytes[4 ];
588
561
f.read (arrBytes, 4 );
589
562
if (!f) {
590
563
sError = " WsjcppObjTreeNodeFloat. Could not read string len. File broken. Can read " + std::to_string (f.gcount ());
@@ -622,29 +595,17 @@ void WsjcppObjTreeNodeDouble::setValue(float nValue) {
622
595
623
596
// ---------------------------------------------------------------------
624
597
625
- int WsjcppObjTreeNodeDouble::getDataSize ( ) {
598
+ bool WsjcppObjTreeNodeDouble::writeDataPartToFile (std::ofstream &f, std::string & sError ) {
626
599
static_assert (sizeof (double ) == 8 , " Expected sizeof(double) == 8" );
627
- return sizeof (double );
628
- }
629
-
630
- // ---------------------------------------------------------------------
631
-
632
- const char *WsjcppObjTreeNodeDouble::getData () {
633
- return reinterpret_cast <const char *>(&m_nValue);
600
+ const char *pData = reinterpret_cast <const char *>(&m_nValue);
601
+ f.write (pData, 8 );
602
+ return true ;
634
603
}
635
604
636
605
// ---------------------------------------------------------------------
637
606
638
607
bool WsjcppObjTreeNodeDouble::readDataPartFromFile (std::ifstream &f, std::string &sError ) {
639
608
static_assert (sizeof (double ) == 8 , " Expected sizeof(double) == 8" );
640
- // size
641
- // TODO remove - because this not need
642
- char arrBytes4[4 ];
643
- f.read (arrBytes4, 4 );
644
- if (!f) {
645
- sError = " WsjcppObjTreeNodeInteger. Could not read string len. File broken. Can read " + std::to_string (f.gcount ());
646
- return false ;
647
- }
648
609
// value
649
610
char arrBytes[8 ];
650
611
f.read (arrBytes, 8 );
0 commit comments