@@ -113,7 +113,7 @@ namespace bin2cpp
113113 {
114114 if ( name == " bin2cpp_insert_input_file_as_code" )
115115 {
116- output << getInputFileDataAsCode ( );
116+ writeInputFileDataAsCode (output );
117117 return true ;
118118 }
119119
@@ -427,25 +427,18 @@ namespace bin2cpp
427427 return mContext .headerFilename ;
428428 }
429429
430- std::string BaseGenerator::getInputFileDataAsCode ( )
430+ void BaseGenerator::writeInputFileDataAsCode (std::ostream& output )
431431 {
432- std::string output;
433-
434432 // check if input file exists
435433 FILE* fin = fopen (mContext .inputFilePath .c_str (), " rb" );
436434 if ( !fin )
437- return " " ;
435+ return ;
438436
439437 uint64_t fileSize = ra::filesystem::GetFileSize64 (mContext .inputFilePath .c_str ());
440438 size_t chunkCount = fileSize / mContext .chunkSize ;
441439 if ( fileSize % mContext .chunkSize > 0 )
442440 chunkCount++;
443441
444- // Try to optimize the output buffer string.
445- // Initialize the output buffer to be around twice the size of the input file.
446- // Most files will output as hexadecimal values which is roughly doubling the number of bytes of the output file.
447- output.reserve (2 * fileSize);
448-
449442 // create buffer for each chunks from input buffer
450443 int numLinePrinted = 0 ;
451444 size_t chunkIndex = 0 ;
@@ -459,9 +452,8 @@ namespace bin2cpp
459452
460453 if ( readSize > 0 )
461454 {
462- // output
463- std::string encoded_chunk = getInputFileChunkAsCode (buffer, readSize, chunkIndex, chunkCount, isLastChunk);
464- output += encoded_chunk;
455+ // append chunk as code in output stream
456+ writeInputFileChunkAsCode (buffer, readSize, chunkIndex, chunkCount, isLastChunk, output);
465457
466458 numLinePrinted++;
467459 chunkIndex++;
@@ -472,13 +464,10 @@ namespace bin2cpp
472464 buffer = NULL ;
473465
474466 fclose (fin);
475-
476- return output;
477467 }
478468
479- std::string BaseGenerator::getInputFileChunkAsCode (const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk)
469+ void BaseGenerator::writeInputFileChunkAsCode (const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output )
480470 {
481- return " " ;
482471 }
483472
484473 bool BaseGenerator::createCppHeaderFile (const char * header_file_path)
0 commit comments