Skip to content

Commit 0ef1a82

Browse files
committed
Modified getInputFileDataAsCode() and getInputFileChunkAsCode() to output data to a stream instead of returning a string
1 parent d065848 commit 0ef1a82

File tree

8 files changed

+54
-60
lines changed

8 files changed

+54
-60
lines changed

src/bin2cpp/ArrayGenerator.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdlib.h>
2929
#include <string>
3030
#include <stdlib.h>
31+
#include <sstream>
3132

3233
#include "rapidassist/code_cpp.h"
3334
#include "rapidassist/strings.h"
@@ -94,8 +95,11 @@ namespace bin2cpp
9495
if (!ra::filesystem::FileExists(mContext.inputFilePath.c_str()) )
9596
return false;
9697

97-
std::string output = getInputFileDataAsCode();
98-
printf("\"%s\"", output.c_str());
98+
std::ostringstream output_stream;
99+
writeInputFileDataAsCode(output_stream);
100+
std::string str = output_stream.str();
101+
102+
printf("\"%s\"", str.c_str());
99103

100104
return true;
101105
}
@@ -185,7 +189,7 @@ namespace bin2cpp
185189
return write_success;
186190
}
187191

188-
std::string ArrayGenerator::getInputFileChunkAsCode(const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk)
192+
void ArrayGenerator::writeInputFileChunkAsCode(const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output)
189193
{
190194
size_t indentation = 0;
191195

@@ -196,18 +200,19 @@ namespace bin2cpp
196200
else if ( mContext.code == CodeGenerationEnum::CODE_GENERATION_C )
197201
indentation = 4;
198202

199-
std::string output;
203+
std::string str;
200204
if ( indentation )
201-
output += std::string(indentation, ' ');
205+
str += std::string(indentation, ' ');
202206
if ( mContext.plainOutput )
203-
output += "\"";
204-
output += ra::code::cpp::ToCppCharactersArray(buffer, buffer_size);
207+
str += "\"";
208+
str += ra::code::cpp::ToCppCharactersArray(buffer, buffer_size);
205209
if ( mContext.plainOutput )
206-
output += "\"";
210+
str += "\"";
207211
if ( !is_last_chunk )
208-
output += ",";
209-
output += "\n";
210-
return output;
212+
str += ",";
213+
str += "\n";
214+
215+
output << str;
211216
}
212217

213218
}; //bin2cpp

src/bin2cpp/ArrayGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace bin2cpp
4343
virtual bool createCppSourceFile(const char * file_path);
4444
virtual bool createCSourceFile(const char * file_path);
4545
virtual bool printFileContent();
46-
virtual std::string getInputFileChunkAsCode(const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk);
46+
virtual void writeInputFileChunkAsCode(const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output);
4747
};
4848

4949
}; //bin2cpp

src/bin2cpp/BaseGenerator.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

src/bin2cpp/BaseGenerator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ namespace bin2cpp
7373
virtual std::string getFileClassFileName();
7474
virtual std::string getFileClassFilePath();
7575
virtual std::string getCppHeaderIncludePath();
76-
virtual std::string getInputFileDataAsCode();
77-
virtual std::string getInputFileChunkAsCode(const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk);
76+
virtual void writeInputFileDataAsCode(std::ostream& output);
77+
virtual void writeInputFileChunkAsCode(const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output);
7878

7979
//attributes
8080
Context mContext;

src/bin2cpp/SegmentGenerator.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ namespace bin2cpp
185185
return write_success;
186186
}
187187

188-
std::string SegmentGenerator::getInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk)
188+
void SegmentGenerator::writeInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output)
189189
{
190190
size_t indentation = 0;
191191

@@ -196,9 +196,9 @@ namespace bin2cpp
196196
else if ( mContext.code == CodeGenerationEnum::CODE_GENERATION_C )
197197
indentation = 2;
198198

199-
std::string output;
199+
std::string str;
200200
if ( indentation )
201-
output += std::string(indentation, ' ');
201+
str += std::string(indentation, ' ');
202202

203203
//convert to cpp string
204204
std::string code;
@@ -218,26 +218,26 @@ namespace bin2cpp
218218
}
219219
else if ( mContext.code == CodeGenerationEnum::CODE_GENERATION_CPP )
220220
{
221-
output += "mBuffer.append(\"";
222-
output += code;
223-
output += "\", ";
224-
output += ra::strings::ToString(buffer_size);
225-
output += ");";
221+
str += "mBuffer.append(\"";
222+
str += code;
223+
str += "\", ";
224+
str += ra::strings::ToString(buffer_size);
225+
str += ");";
226226
}
227227
else if ( mContext.code == CodeGenerationEnum::CODE_GENERATION_C )
228228
{
229-
output += "memcpy(next, \"";
230-
output += code;
231-
output += "\", ";
232-
output += ra::strings::ToString(buffer_size);
233-
output += "); next += ";
234-
output += ra::strings::ToString(buffer_size);
235-
output += ";";
229+
str += "memcpy(next, \"";
230+
str += code;
231+
str += "\", ";
232+
str += ra::strings::ToString(buffer_size);
233+
str += "); next += ";
234+
str += ra::strings::ToString(buffer_size);
235+
str += ";";
236236
}
237237

238-
output += "\n";
238+
str += "\n";
239239

240-
return output;
240+
output << str;
241241
}
242242

243243
}; //bin2cpp

src/bin2cpp/SegmentGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace bin2cpp
4242
virtual const char * getName() const;
4343
virtual bool createCppSourceFile(const char * file_path);
4444
virtual bool createCSourceFile(const char* file_path);
45-
virtual std::string getInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk);
45+
virtual void writeInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output);
4646
};
4747

4848
}; //bin2cpp

src/bin2cpp/StringGenerator.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ namespace bin2cpp
172172
return write_success;
173173
}
174174

175-
std::string StringGenerator::getInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk)
175+
void StringGenerator::writeInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output)
176176
{
177177
size_t indentation = 0;
178178

@@ -183,30 +183,30 @@ namespace bin2cpp
183183
else if ( mContext.code == CodeGenerationEnum::CODE_GENERATION_C )
184184
indentation = 4;
185185

186-
std::string output;
186+
std::string str;
187187
if ( indentation )
188-
output += std::string(indentation, ' ');
188+
str += std::string(indentation, ' ');
189189

190-
output += "\"";
190+
str += "\"";
191191

192192
//convert to cpp string
193193
switch ( mContext.cppEncoder )
194194
{
195195
case CPP_ENCODER_HEX:
196-
output += ra::code::cpp::ToHexString(buffer, buffer_size);
196+
str += ra::code::cpp::ToHexString(buffer, buffer_size);
197197
break;
198198
case CPP_ENCODER_OCT:
199199
default:
200-
output += ra::code::cpp::ToOctString(buffer, buffer_size, false);
200+
str += ra::code::cpp::ToOctString(buffer, buffer_size, false);
201201
break;
202202
};
203203

204-
output += "\"";
204+
str += "\"";
205205
if ( is_last_chunk )
206-
output += ";";
207-
output += "\n";
206+
str += ";";
207+
str += "\n";
208208

209-
return output;
209+
output << str;
210210
}
211211

212212
}; //bin2cpp

src/bin2cpp/StringGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace bin2cpp
4141
virtual const char * getName() const;
4242
virtual bool createCppSourceFile(const char * file_path);
4343
virtual bool createCSourceFile(const char* file_path);
44-
virtual std::string getInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk);
44+
virtual void writeInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk, std::ostream& output);
4545
};
4646

4747
}; //bin2cpp

0 commit comments

Comments
 (0)