@@ -1408,57 +1408,64 @@ void Tape::moveSelectedBlocks(int targetPosition) {
1408
1408
1409
1409
uint16_t encodedlen;
1410
1410
1411
- for ( int state = 0 ; state < 3 ; state++ ) {
1412
- int nextStateBlock = -1 ;
1413
- long nextStateBlockPos = 0 ;
1414
-
1415
- while (true ) {
1416
- fseek (tape, off, SEEK_SET);
1417
-
1418
- bool block_is_selected = ( std::find (selectedBlocks.begin (), selectedBlocks.end (), blockIndex) != selectedBlocks.end () );
1419
-
1420
- if ( state == 0 && blockIndex == targetPosition ) break ;
1421
-
1422
- size_t bytesRead = fread (&encodedlen, sizeof (encodedlen), 1 , tape);
1423
- if (bytesRead != 1 ) break ;
1424
-
1425
- uint8_t l = ((uint8_t *)&encodedlen)[0 ];
1426
- uint8_t h = ((uint8_t *)&encodedlen)[1 ];
1427
- tapeBlkLen = (l | (h << 8 ));
1428
-
1429
- if ( ( state == 0 && !block_is_selected ) ||
1430
- ( state == 1 && block_is_selected ) ||
1431
- ( state == 2 && !block_is_selected && blockIndex >= targetPosition ) )
1432
- {
1433
- fwrite (&encodedlen, sizeof (encodedlen), 1 , outputFile);
1434
- size_t totalBytesRead = 0 ;
1435
- while (totalBytesRead < tapeBlkLen) {
1436
- size_t bytesToRead = std::min (sizeof (buffer), static_cast <size_t >(tapeBlkLen - totalBytesRead));
1437
- size_t bytesRead = fread (buffer, 1 , bytesToRead, tape);
1438
- if (bytesRead != bytesToRead) {
1439
- printf (" Error al leer el bloque del archivo.\n " );
1440
- break ;
1441
- }
1442
- fwrite (buffer, 1 , bytesRead, outputFile);
1443
- totalBytesRead += bytesRead;
1444
- }
1445
- } else {
1446
- if ( nextStateBlock == -1 ) {
1447
- nextStateBlock = blockIndex;
1448
- nextStateBlockPos = off;
1449
- }
1411
+ // Definir la lambda writeBlock
1412
+ auto writeBlock = [&](uint16_t encodedlen, FILE* inputFile, FILE* outputFile) {
1413
+ fwrite (&encodedlen, sizeof (encodedlen), 1 , outputFile);
1414
+ size_t totalBytesRead = 0 ;
1415
+ while (totalBytesRead < tapeBlkLen) {
1416
+ size_t bytesToRead = std::min (sizeof (buffer), static_cast <size_t >(tapeBlkLen - totalBytesRead));
1417
+ size_t bytesRead = fread (buffer, 1 , bytesToRead, inputFile);
1418
+ if (bytesRead != bytesToRead) {
1419
+ printf (" Error al leer el bloque del archivo.\n " );
1420
+ break ;
1450
1421
}
1422
+ fwrite (buffer, 1 , bytesRead, outputFile);
1423
+ totalBytesRead += bytesRead;
1424
+ }
1425
+ };
1426
+
1427
+ std::vector<long > selectedBlocksOffsets;
1428
+ std::vector<long > remainsBlocksOffsets;
1429
+
1430
+ rewind (tape);
1431
+
1432
+ while (true ) {
1433
+ fseek (tape, off, SEEK_SET);
1434
+
1435
+ bool block_is_selected = ( std::find (selectedBlocks.begin (), selectedBlocks.end (), blockIndex) != selectedBlocks.end () );
1436
+
1437
+ size_t bytesRead = fread (&encodedlen, sizeof (encodedlen), 1 , tape);
1438
+ if (bytesRead != 1 ) break ;
1439
+
1440
+ uint8_t l = ((uint8_t *)&encodedlen)[0 ];
1441
+ uint8_t h = ((uint8_t *)&encodedlen)[1 ];
1442
+ tapeBlkLen = (l | (h << 8 ));
1451
1443
1452
- off += tapeBlkLen + sizeof (encodedlen);
1453
- blockIndex++;
1444
+ if ( !block_is_selected && blockIndex < targetPosition ) {
1445
+ writeBlock (encodedlen, tape, outputFile);
1446
+ } else if ( block_is_selected ) {
1447
+ selectedBlocksOffsets.push_back (off);
1448
+ } else {
1449
+ remainsBlocksOffsets.push_back (off);
1454
1450
}
1455
1451
1456
- // move selecteds
1457
- if ( nextStateBlock != -1 ) {
1458
- blockIndex = nextStateBlock;
1459
- off = nextStateBlockPos;
1460
- } else
1461
- if ( state == 1 ) break ;
1452
+ off += tapeBlkLen + sizeof (encodedlen);
1453
+ blockIndex++;
1454
+ }
1455
+
1456
+ selectedBlocksOffsets.insert (selectedBlocksOffsets.end (), remainsBlocksOffsets.begin (), remainsBlocksOffsets.end ());
1457
+
1458
+ for (int off : selectedBlocksOffsets) {
1459
+ fseek (tape, off, SEEK_SET);
1460
+
1461
+ size_t bytesRead = fread (&encodedlen, sizeof (encodedlen), 1 , tape);
1462
+ if (bytesRead != 1 ) break ;
1463
+
1464
+ uint8_t l = ((uint8_t *)&encodedlen)[0 ];
1465
+ uint8_t h = ((uint8_t *)&encodedlen)[1 ];
1466
+ tapeBlkLen = (l | (h << 8 ));
1467
+
1468
+ writeBlock (encodedlen, tape, outputFile);
1462
1469
}
1463
1470
1464
1471
fclose (outputFile);
0 commit comments