@@ -225,12 +225,9 @@ bool WsjcppObjTree::writeTreeToFile(const std::string &sFilename, std::string &s
225225 // write id
226226 this ->writeUInt32 (f, pNode->getId ());
227227
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+ }
234231 }
235232 f.close ();
236233 return true ;
@@ -440,15 +437,13 @@ void WsjcppObjTreeNodeString::setValue(const std::string &sValue) {
440437
441438// ---------------------------------------------------------------------
442439
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+ };
452447
453448// ---------------------------------------------------------------------
454449
@@ -500,29 +495,18 @@ void WsjcppObjTreeNodeInteger::setValue(int32_t nValue) {
500495
501496// ---------------------------------------------------------------------
502497
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+ };
513504
514505// ---------------------------------------------------------------------
515506
516507bool 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- }
525508 // value
509+ char arrBytes[4 ];
526510 f.read (arrBytes, 4 );
527511 if (!f) {
528512 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) {
561545
562546// ---------------------------------------------------------------------
563547
564- int WsjcppObjTreeNodeFloat::getDataSize ( ) {
548+ bool WsjcppObjTreeNodeFloat::writeDataPartToFile (std::ofstream &f, std::string & sError ) {
565549 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+ };
574554
575555// ---------------------------------------------------------------------
576556
577557bool WsjcppObjTreeNodeFloat::readDataPartFromFile (std::ifstream &f, std::string &sError ) {
578558 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- }
587559 // value
560+ char arrBytes[4 ];
588561 f.read (arrBytes, 4 );
589562 if (!f) {
590563 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) {
622595
623596// ---------------------------------------------------------------------
624597
625- int WsjcppObjTreeNodeDouble::getDataSize ( ) {
598+ bool WsjcppObjTreeNodeDouble::writeDataPartToFile (std::ofstream &f, std::string & sError ) {
626599 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 ;
634603}
635604
636605// ---------------------------------------------------------------------
637606
638607bool WsjcppObjTreeNodeDouble::readDataPartFromFile (std::ifstream &f, std::string &sError ) {
639608 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- }
648609 // value
649610 char arrBytes[8 ];
650611 f.read (arrBytes, 8 );
0 commit comments