Skip to content

Commit 22dbb60

Browse files
committed
Implemented --code argument support to specify the programming language output for code generation.
1 parent fc9feda commit 22dbb60

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/bin2cpp/Context.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ namespace bin2cpp
6969
this->generatorName = other.generatorName ;
7070
this->directoryIncludeFilters = other.directoryIncludeFilters ;
7171
this->directoryExcludeFilters = other.directoryExcludeFilters ;
72+
this->code = other.code ;
7273
}
7374
return *this;
7475
}
@@ -101,6 +102,7 @@ namespace bin2cpp
101102
generatorName.clear();
102103
directoryIncludeFilters.clear();
103104
directoryExcludeFilters.clear();
105+
code = CodeGenerationEnum::CODE_GENERATION_CPP;
104106
}
105107

106108

src/bin2cpp/Context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ namespace bin2cpp
7272
std::string generatorName;
7373
std::vector<std::string> directoryIncludeFilters;
7474
std::vector<std::string> directoryExcludeFilters;
75+
CodeGenerationEnum code;
7576

7677
void reset();
7778

src/bin2cpp/main.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ enum APP_ERROR_CODES
5959
APP_ERROR_TOOMANYARGUMENTS,
6060
APP_ERROR_INPUTDIRNOTFOUND,
6161
AAP_ERROR_NOTSUPPORTED,
62-
APP_ERROR_OPERATIONHASFAILED
62+
APP_ERROR_OPERATIONHASFAILED,
63+
APP_ERROR_INVALIDVALUE,
6364
};
6465

6566
enum FILE_UPDATE_MODE
@@ -75,6 +76,7 @@ static const size_t DEFAULT_CHUNK_SIZE = 200;
7576
static const char * DEFAULT_NAMESPACE = "bin2cpp";
7677
static const char * DEFAULT_BASECLASSNAME = "File";
7778
static const CppEncoderEnum DEFAULT_ENCODING = CPP_ENCODER_OCT;
79+
static const CodeGenerationEnum DEFAULT_CODE_GENERATION = CODE_GENERATION_CPP;
7880
static Dictionary identifiers_dictionary; // unique values for identifiers
7981
static Dictionary output_files_dictionary; // unique values for output file names
8082
#define DIRECTORY_FILTER_SEPARATOR_STR ":"
@@ -108,6 +110,9 @@ const char * getErrorCodeDescription(const APP_ERROR_CODES & error_code)
108110
case APP_ERROR_OPERATIONHASFAILED:
109111
return "Operation has failed";
110112
break;
113+
case APP_ERROR_INVALIDVALUE:
114+
return "Invalid value";
115+
break;
111116
default:
112117
return "Unknown error";
113118
};
@@ -198,6 +203,7 @@ void printUsage()
198203
" --keepdirs Keep the directory structure. Forces the output files to have the same\n"
199204
" directory structure as the input files. Valid only when --dir is used.\n"
200205
" --plainoutput Print the encoded string in plain format to stdout. Useful for scripts and integration with third party application.\n"
206+
" --code Define the programming language output for code generation. Supported values are 'c', 'cpp', 'c++'.\n"
201207
" --override Tells bin2cpp to overwrite the destination files.\n"
202208
" --noheader Do not print program header to standard output.\n"
203209
" --quiet Do not log any message to standard output.\n"
@@ -338,6 +344,25 @@ int main(int argc, char* argv[])
338344

339345
//optional arguments
340346

347+
std::string codeStr;
348+
if ( ra::cli::ParseArgument("code", codeStr, argc, argv) )
349+
{
350+
CodeGenerationEnum codeTmp = parseCode(codeStr);
351+
if ( codeTmp == CodeGenerationEnum::CODE_GENERATION_UNKNOW )
352+
{
353+
APP_ERROR_CODES error = APP_ERROR_INVALIDVALUE;
354+
ra::logging::Log(ra::logging::LOG_ERROR, "%s (code)", getErrorCodeDescription(error));
355+
printUsage();
356+
return error;
357+
}
358+
359+
c.code = codeTmp;
360+
}
361+
else
362+
{
363+
c.code = DEFAULT_CODE_GENERATION;
364+
}
365+
341366
if (c.hasInputFile)
342367
{
343368
//identifier
@@ -417,7 +442,7 @@ int main(int argc, char* argv[])
417442
c.cppEncoder = CPP_ENCODER_HEX;
418443
else
419444
{
420-
APP_ERROR_CODES error = APP_ERROR_MISSINGARGUMENTS;
445+
APP_ERROR_CODES error = APP_ERROR_INVALIDVALUE;
421446
ra::logging::Log(ra::logging::LOG_ERROR, "%s (encoding)", getErrorCodeDescription(error));
422447
printUsage();
423448
return error;

test/bin2cpp_unittest/TestCLI.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ extern const std::string & gGeneratedFilesDir;
4848

4949
enum APP_ERROR_CODES
5050
{
51-
APP_ERROR_SUCCESS,
51+
APP_ERROR_SUCCESS = 0,
5252
APP_ERROR_MISSINGARGUMENTS,
5353
APP_ERROR_INPUTFILENOTFOUND,
5454
APP_ERROR_UNABLETOCREATEOUTPUTFILES,
5555
APP_ERROR_TOOMANYARGUMENTS,
56-
APP_ERROR_INPUTDIRNOTFOUND
56+
APP_ERROR_INPUTDIRNOTFOUND,
57+
AAP_ERROR_NOTSUPPORTED,
58+
APP_ERROR_OPERATIONHASFAILED,
59+
APP_ERROR_INVALIDVALUE,
5760
};
5861

5962
namespace TestCLIUtils
@@ -1224,7 +1227,7 @@ TEST_F(TestCLI, testAutomaticIdentifierHeaderfile)
12241227
ASSERT_TRUE(deleteFile(cppFilePath.c_str()));
12251228
}
12261229

1227-
TEST_F(TestCLI, testErrorMissingArgumentEncoding)
1230+
TEST_F(TestCLI, testErrorInvalidArgumentEncoding)
12281231
{
12291232
static const std::string expectedFilePath = getExpectedFilePath();
12301233
static const std::string outputFilePath = getActualFilePath();
@@ -1258,15 +1261,15 @@ TEST_F(TestCLI, testErrorMissingArgumentEncoding)
12581261
#if defined(__linux__) || defined(__APPLE__)
12591262
returnCode = WEXITSTATUS(returnCode);
12601263
#endif
1261-
ASSERT_EQ(APP_ERROR_MISSINGARGUMENTS, returnCode) << "The command line '" << cmdline.c_str() << "' returned " << returnCode;
1264+
ASSERT_EQ(APP_ERROR_INVALIDVALUE, returnCode) << "The command line '" << cmdline.c_str() << "' returned " << returnCode;
12621265

12631266
//load output file
12641267
ra::strings::StringVector lines;
12651268
bool loaded = ra::filesystem::ReadTextFile(outputFilePath.c_str(), lines);
12661269
ASSERT_TRUE(loaded);
12671270

12681271
//assert standard output log
1269-
ASSERT_TEXT_IN_FILE(true, outputFilePath.c_str(), "Error: Missing arguments (encoding)");
1272+
ASSERT_TEXT_IN_FILE(true, outputFilePath.c_str(), "Error: Invalid value (encoding)");
12701273

12711274
//cleanup
12721275
ASSERT_TRUE(deleteFile(outputFilePath.c_str()));

0 commit comments

Comments
 (0)