@@ -14,8 +14,8 @@ namespace btf
14
14
{
15
15
imageFileContent.assign (FLOPPY_SIZE, 0 );
16
16
17
- writeLoader (loaderFilename);
18
- int codeSize = assignCode (codeFilename);
17
+ int loaderSize = writeLoader (loaderFilename);
18
+ int codeSize = assignCode (codeFilename, loaderSize );
19
19
replaceFlagWithMachineCode (codeSize);
20
20
}
21
21
@@ -29,20 +29,20 @@ namespace btf
29
29
std::copy (imageFileContent.begin (), imageFileContent.end (), std::ostreambuf_iterator<char >(imageFile));
30
30
}
31
31
32
- void FloppyImage::writeLoader (const std::string& loaderFilename)
32
+ int FloppyImage::writeLoader (const std::string& loaderFilename)
33
33
{
34
34
std::ifstream loaderFile (loaderFilename, std::ios::binary);
35
35
36
36
if (!loaderFile.is_open ())
37
37
throw Error (" Couldn\' t find MBR file: " + loaderFilename);
38
38
39
39
std::vector<char > loader ((std::istreambuf_iterator<char >(loaderFile)), std::istreambuf_iterator<char >());
40
-
41
- imageFileContent. erase (imageFileContent. begin (), imageFileContent. begin () + loader. size ());
42
- imageFileContent. insert (imageFileContent. begin (), loader.begin (), loader. end () );
40
+ std::copy (loader. begin (), loader. end (), imageFileContent. begin ());
41
+
42
+ return loader.size ( );
43
43
}
44
44
45
- int FloppyImage::assignCode (const std::string& codeFilename)
45
+ int FloppyImage::assignCode (const std::string& codeFilename, int offset )
46
46
{
47
47
std::ifstream codeFile (codeFilename);
48
48
@@ -58,8 +58,7 @@ namespace btf
58
58
if (code.size () > 65536 )
59
59
throw Error (" Accteptable brainfuck source code size is 64KiB" );
60
60
61
- imageFileContent.erase (imageFileContent.begin () + 512 , imageFileContent.begin () + 512 + code.size ());
62
- imageFileContent.insert (imageFileContent.begin () + 512 , code.begin (), code.end ());
61
+ std::copy (code.begin (), code.end (), imageFileContent.begin () + offset);
63
62
64
63
return code.size ();
65
64
}
@@ -82,7 +81,6 @@ namespace btf
82
81
83
82
std::vector<uint8_t > opcode = {0xB0 , static_cast <uint8_t >(sectorsAmount & 0xFF ), 0x90 , 0x90 };
84
83
85
- imageFileContent.erase (iterator, iterator + 4 );
86
- imageFileContent.insert (iterator, opcode.begin (), opcode.end ());
84
+ std::copy (opcode.begin (), opcode.end (), iterator);
87
85
}
88
86
}
0 commit comments