@@ -133,7 +133,7 @@ namespace tivars
133133
134134 std::string format_unix_timestamp (uint32_t timestamp)
135135 {
136- const std::time_t raw = static_cast <std:: time_t >( timestamp) ;
136+ const std::time_t raw = timestamp;
137137 std::tm tm{};
138138#ifdef _WIN32
139139 gmtime_s (&tm, &raw);
@@ -161,8 +161,7 @@ namespace tivars
161161 const size_t infoStart = start;
162162 const uint32_t mainOffset = read_le24 (data, infoStart + 18 );
163163 out[" name" ] = std::string (data.begin () + static_cast <ptrdiff_t >(infoStart + 3 ), data.begin () + static_cast <ptrdiff_t >(infoStart + 12 ));
164- const size_t nulPos = out[" name" ].get <std::string>().find (' \0 ' );
165- if (nulPos != std::string::npos)
164+ if (const size_t nulPos = out[" name" ].get <std::string>().find (' \0 ' ); nulPos != std::string::npos)
166165 {
167166 out[" name" ] = out[" name" ].get <std::string>().substr (0 , nulPos);
168167 }
@@ -179,8 +178,7 @@ namespace tivars
179178 if (copyrightOffset < size)
180179 {
181180 out[" copyright" ] = std::string (data.begin () + static_cast <ptrdiff_t >(start + copyrightOffset), data.begin () + static_cast <ptrdiff_t >(start + size));
182- const size_t nulPos = out[" copyright" ].get <std::string>().find (' \0 ' );
183- if (nulPos != std::string::npos)
181+ if (const size_t nulPos = out[" copyright" ].get <std::string>().find (' \0 ' ); nulPos != std::string::npos)
184182 {
185183 out[" copyright" ] = out[" copyright" ].get <std::string>().substr (0 , nulPos);
186184 }
@@ -402,11 +400,11 @@ namespace tivars
402400 j[" hasChecksum" ] = header.hasChecksum ;
403401
404402 j[" devices" ] = json::array ();
405- for (const auto & device : header.devices )
403+ for (const auto & [devType, typeId] : header.devices )
406404 {
407405 j[" devices" ].push_back ({
408- {" deviceType" , device. first },
409- {" typeId" , device. second },
406+ {" deviceType" , devType },
407+ {" typeId" , typeId },
410408 });
411409 }
412410
@@ -429,12 +427,12 @@ namespace tivars
429427 else
430428 {
431429 j[" blocks" ] = json::array ();
432- for (const auto & block : parseIntelBlocks (header.calcData ))
430+ for (const auto & [address, blockType, data] : parseIntelBlocks (header.calcData ))
433431 {
434432 j[" blocks" ].push_back ({
435- {" address" , toHex ({static_cast <uint8_t >((block. address >> 8 ) & 0xFF ), static_cast <uint8_t >(block. address & 0xFF )})},
436- {" blockType" , dechex (block. blockType )},
437- {" dataHex" , toHex (block. data )},
433+ {" address" , toHex ({static_cast <uint8_t >((address >> 8 ) & 0xFF ), static_cast <uint8_t >(address & 0xFF )})},
434+ {" blockType" , dechex (blockType)},
435+ {" dataHex" , toHex (data)},
438436 });
439437 }
440438 }
@@ -449,7 +447,7 @@ namespace tivars
449447 throw std::out_of_range (" Invalid flash header index" );
450448 }
451449
452- flash_header_t & header = headers[headerIdx];
450+ auto & [magic, revision, binaryFlag, objectType, date, name, devices, productId, calcData, hasChecksum, type, model] = headers[headerIdx];
453451 const json j = json::parse (str);
454452 if (!j.is_object ())
455453 {
@@ -458,58 +456,58 @@ namespace tivars
458456
459457 if (j.contains (" typeName" ))
460458 {
461- header. type = TIVarType{j.at (" typeName" ).get <std::string>()};
459+ type = TIVarType{j.at (" typeName" ).get <std::string>()};
462460 }
463461 if (j.contains (" magic" ))
464462 {
465- header. magic = j.at (" magic" ).get <std::string>();
463+ magic = j.at (" magic" ).get <std::string>();
466464 }
467465 if (j.contains (" revision" ))
468466 {
469- header. revision = j.at (" revision" ).get <std::string>();
467+ revision = j.at (" revision" ).get <std::string>();
470468 }
471469 if (j.contains (" binaryFlag" ))
472470 {
473- header. binaryFlag = static_cast <uint8_t >(j.at (" binaryFlag" ).get <int >());
471+ binaryFlag = static_cast <uint8_t >(j.at (" binaryFlag" ).get <int >());
474472 }
475473 if (j.contains (" objectType" ))
476474 {
477- header. objectType = static_cast <uint8_t >(j.at (" objectType" ).get <int >());
475+ objectType = static_cast <uint8_t >(j.at (" objectType" ).get <int >());
478476 }
479477 if (j.contains (" date" ))
480478 {
481- const json& date = j.at (" date" );
482- if (!date .is_array () || date .size () != 3 )
479+ const json& jsonDate = j.at (" date" );
480+ if (!jsonDate .is_array () || jsonDate .size () != 3 )
483481 {
484482 throw std::invalid_argument (" Flash header date must be a [day, month, year] array" );
485483 }
486- header. date .day = static_cast <uint8_t >(date [0 ].get <int >());
487- header. date .month = static_cast <uint8_t >(date [1 ].get <int >());
488- header. date .year = static_cast <uint16_t >(date [2 ].get <int >());
484+ date.day = static_cast <uint8_t >(jsonDate [0 ].get <int >());
485+ date.month = static_cast <uint8_t >(jsonDate [1 ].get <int >());
486+ date.year = static_cast <uint16_t >(jsonDate [2 ].get <int >());
489487 }
490488 if (j.contains (" name" ))
491489 {
492- header. name = j.at (" name" ).get <std::string>();
490+ name = j.at (" name" ).get <std::string>();
493491 }
494492 if (j.contains (" devices" ))
495493 {
496- header. devices .clear ();
494+ devices.clear ();
497495 for (const json& device : j.at (" devices" ))
498496 {
499- header. devices .emplace_back (
497+ devices.emplace_back (
500498 static_cast <uint8_t >(device.at (" deviceType" ).get <int >()),
501499 static_cast <uint8_t >(device.at (" typeId" ).get <int >())
502500 );
503501 }
504502 }
505503 if (j.contains (" productId" ))
506504 {
507- header. productId = static_cast <uint8_t >(j.at (" productId" ).get <int >());
508- header. model = modelFromProductId (header. productId );
505+ productId = static_cast <uint8_t >(j.at (" productId" ).get <int >());
506+ model = modelFromProductId (productId);
509507 }
510508 if (j.contains (" hasChecksum" ))
511509 {
512- header. hasChecksum = j.at (" hasChecksum" ).get <bool >();
510+ hasChecksum = j.at (" hasChecksum" ).get <bool >();
513511 }
514512
515513 if (j.contains (" blocks" ))
@@ -522,27 +520,27 @@ namespace tivars
522520 {
523521 throw std::invalid_argument (" Flash block address must be a two-byte hex string" );
524522 }
525- flash_block_t block{};
526- block. address = static_cast <uint16_t >((addressBytes[0 ] << 8 ) | addressBytes[1 ]);
527- block. blockType = static_cast < uint8_t >( hexdec (item.at (" blockType" ).get <std::string>()));
528- block. data = parseHex (item.at (" dataHex" ).get <std::string>());
529- blocks. push_back (block );
523+ blocks. emplace_back (
524+ static_cast <uint16_t >((addressBytes[0 ] << 8 ) | addressBytes[1 ]),
525+ hexdec (item.at (" blockType" ).get <std::string>()),
526+ parseHex (item.at (" dataHex" ).get <std::string>())
527+ );
530528 }
531- header. calcData = makeIntelData (blocks);
529+ calcData = makeIntelData (blocks);
532530 }
533531 else if (j.contains (" calcDataHex" ))
534532 {
535- header. calcData = parseHex (j.at (" calcDataHex" ).get <std::string>());
533+ calcData = parseHex (j.at (" calcDataHex" ).get <std::string>());
536534 }
537- else if (header. type .getName () == " FlashLicense" && j.contains (" license" ))
535+ else if (type.getName () == " FlashLicense" && j.contains (" license" ))
538536 {
539537 const std::string license = j.at (" license" ).get <std::string>();
540- header. calcData .assign (license.begin (), license.end ());
538+ calcData.assign (license.begin (), license.end ());
541539 }
542540
543- if (!header. devices .empty ())
541+ if (!devices.empty ())
544542 {
545- header. type = TIVarType{header. devices .front ().second };
543+ type = TIVarType{devices.front ().second };
546544 }
547545 }
548546
@@ -737,7 +735,7 @@ namespace tivars
737735 uint16_t TIFlashFile::computeChecksum (const data_t & data)
738736 {
739737 uint32_t sum = 0 ;
740- for (uint8_t byte : data)
738+ for (const uint8_t byte : data)
741739 {
742740 sum += byte;
743741 }
@@ -791,7 +789,7 @@ namespace tivars
791789 << std::setw (2 ) << static_cast <int >(block.data .size ())
792790 << std::setw (4 ) << static_cast <int >(block.address )
793791 << std::setw (2 ) << static_cast <int >(block.blockType );
794- for (uint8_t byte : block.data )
792+ for (const uint8_t byte : block.data )
795793 {
796794 out << std::setw (2 ) << static_cast <int >(byte);
797795 }
@@ -800,11 +798,11 @@ namespace tivars
800798 + ((block.address >> 8 ) & 0xFF )
801799 + (block.address & 0xFF )
802800 + block.blockType ;
803- for (uint8_t byte : block.data )
801+ for (const uint8_t byte : block.data )
804802 {
805803 checksumBase += byte;
806804 }
807- out << std::setw (2 ) << static_cast < int >(( -static_cast <int >(checksumBase) ) & 0xFF );
805+ out << std::setw (2 ) << ( -static_cast <int >(checksumBase) & 0xFF );
808806 return out.str ();
809807 }
810808
@@ -824,15 +822,15 @@ namespace tivars
824822
825823 flash_block_t block{};
826824 block.address = static_cast <uint16_t >(std::stoul (record.substr (3 , 4 ), nullptr , 16 ));
827- block.blockType = static_cast < uint8_t >( hexdec (record.substr (7 , 2 ) ));
825+ block.blockType = hexdec (record.substr (7 , 2 ));
828826 block.data = parseHex (record.substr (9 , size * 2 ));
829827
830- const uint8_t fileChecksum = static_cast < uint8_t >( hexdec (record.substr (record.size () - 2 , 2 ) ));
828+ const uint8_t fileChecksum = hexdec (record.substr (record.size () - 2 , 2 ));
831829 uint32_t checksumBase = static_cast <uint32_t >(block.data .size ())
832830 + ((block.address >> 8 ) & 0xFF )
833831 + (block.address & 0xFF )
834832 + block.blockType ;
835- for (uint8_t byte : block.data )
833+ for (const uint8_t byte : block.data )
836834 {
837835 checksumBase += byte;
838836 }
@@ -879,7 +877,7 @@ namespace tivars
879877 data.reserve (hex.size () / 2 );
880878 for (size_t i = 0 ; i < hex.size (); i += 2 )
881879 {
882- data.push_back (static_cast < uint8_t >( hexdec (hex.substr (i, 2 ) )));
880+ data.push_back (hexdec (hex.substr (i, 2 )));
883881 }
884882 return data;
885883 }
@@ -888,7 +886,7 @@ namespace tivars
888886 {
889887 std::ostringstream out;
890888 out << std::uppercase << std::hex << std::setfill (' 0' );
891- for (uint8_t byte : data)
889+ for (const uint8_t byte : data)
892890 {
893891 out << std::setw (2 ) << static_cast <int >(byte);
894892 }
0 commit comments