Skip to content

Commit 8d89e58

Browse files
committed
Created interface INameProvider. Moved functions getDefaultFunctionIdentifier() and getDefaultHeaderFile() from main.cpp to LegacyNameProvider.h.
1 parent a8a5fa2 commit 8d89e58

File tree

5 files changed

+178
-32
lines changed

5 files changed

+178
-32
lines changed

src/bin2cpp/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ add_executable(bin2cpp
2121
crc32.h
2222
enums.h
2323
IGenerator.h
24+
INameProvider.h
25+
LegacyNameProvider.cpp
26+
LegacyNameProvider.h
2427
main.cpp
2528
ManagerGenerator.cpp
2629
ManagerGenerator.h

src/bin2cpp/INameProvider.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**********************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Antoine Beauchamp
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*********************************************************************************/
24+
25+
#ifndef INAME_PROVIDER_H
26+
#define INAME_PROVIDER_H
27+
28+
#include "types.h"
29+
30+
namespace bin2cpp
31+
{
32+
33+
class INameProvider
34+
{
35+
public:
36+
37+
///<summary>
38+
///Get the name of a function identifier based on a given file path.
39+
///</summary>
40+
///<param name="c">The new context.</param>
41+
virtual std::string getDefaultFunctionIdentifier(const std::string & path, Dictionary & dict) = 0;
42+
43+
///<summary>
44+
///Get the name of a function identifier based on a given file path.
45+
///</summary>
46+
///<param name="c">The new context.</param>
47+
virtual std::string getDefaultHeaderFile(const std::string& path) = 0;
48+
49+
};
50+
51+
}; //bin2cpp
52+
53+
#endif //INAME_PROVIDER_H

src/bin2cpp/LegacyNameProvider.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**********************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Antoine Beauchamp
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*********************************************************************************/
24+
25+
#include "LegacyNameProvider.h"
26+
27+
#include "common.h"
28+
29+
#include "rapidassist/strings.h"
30+
#include "rapidassist/filesystem.h"
31+
32+
namespace bin2cpp
33+
{
34+
LegacyNameProvider::LegacyNameProvider()
35+
{
36+
}
37+
38+
LegacyNameProvider::~LegacyNameProvider()
39+
{
40+
}
41+
42+
std::string LegacyNameProvider::getDefaultFunctionIdentifier(const std::string& path, Dictionary& dict)
43+
{
44+
std::string output;
45+
46+
//use the file name without extension as 'identifier'.
47+
output = getUniqueFunctionIdentifierFromPath(path.c_str(), dict);
48+
output = ra::strings::CapitalizeFirstCharacter(output);
49+
50+
return output;
51+
}
52+
53+
std::string LegacyNameProvider::getDefaultHeaderFile(const std::string& path)
54+
{
55+
std::string output;
56+
57+
//use the file name without extension as 'headerfile'.
58+
output = ra::filesystem::GetFilenameWithoutExtension(path.c_str());
59+
output += ".h";
60+
61+
return output;
62+
}
63+
64+
}; //bin2cpp

src/bin2cpp/LegacyNameProvider.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**********************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Antoine Beauchamp
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*********************************************************************************/
24+
25+
#ifndef LEGACYNAMEPROVIDER_H
26+
#define LEGACYNAMEPROVIDER_H
27+
28+
#include "INameProvider.h"
29+
30+
namespace bin2cpp
31+
{
32+
///<summary>
33+
///This is the legacy name generator which generates names for version 3.0.1 and before.
34+
///</summary>
35+
class LegacyNameProvider : public virtual INameProvider
36+
{
37+
public:
38+
LegacyNameProvider();
39+
virtual ~LegacyNameProvider();
40+
41+
//INameProvider methods
42+
virtual std::string getDefaultFunctionIdentifier(const std::string& path, Dictionary& dict);
43+
virtual std::string getDefaultHeaderFile(const std::string& path);
44+
};
45+
46+
}; //bin2cpp
47+
48+
#endif //LEGACYNAMEPROVIDER_H

src/bin2cpp/main.cpp

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "Win32ResourceGenerator.h"
3232
#include "ManagerGenerator.h"
3333
#include "Context.h"
34+
#include "INameProvider.h"
35+
#include "LegacyNameProvider.h"
3436

3537
#include <stdlib.h>
3638
#include <stdio.h>
@@ -140,11 +142,9 @@ struct ARGUMENTS
140142
//pre-declarations
141143
bool generateOutputFile(const Context & c, const std::string & output_file_path, bin2cpp::IGenerator * generator);
142144
APP_ERROR_CODES processInputFile(const Context & c, bin2cpp::IGenerator * generator);
143-
APP_ERROR_CODES processInputDirectory(const Context & c, bin2cpp::IGenerator * generator);
145+
APP_ERROR_CODES processInputDirectory(const Context & c, bin2cpp::INameProvider* nameProvider, bin2cpp::IGenerator * generator);
144146
APP_ERROR_CODES processManagerFiles(const Context & c);
145147
APP_ERROR_CODES processPlainOutput(const Context & c, bin2cpp::IGenerator * generator);
146-
std::string getDefaultFunctionIdentifier(const Context & c, Dictionary & identifiers_dictionary);
147-
std::string getDefaultHeaderFile(const Context & c);
148148

149149
void printHeader()
150150
{
@@ -230,7 +230,7 @@ int main(int argc, char* argv[])
230230
args.version = false;
231231

232232
Context c;
233-
233+
INameProvider& nameProvider = LegacyNameProvider();
234234
std::string dummy;
235235

236236
//help
@@ -344,14 +344,14 @@ int main(int argc, char* argv[])
344344
if (!ra::cli::ParseArgument("identifier", c.functionIdentifier, argc, argv))
345345
{
346346
//identifier is not manually specified.
347-
c.functionIdentifier = getDefaultFunctionIdentifier(c, identifiers_dictionary);
347+
c.functionIdentifier = nameProvider.getDefaultFunctionIdentifier(c.inputFilePath, identifiers_dictionary);
348348
}
349349

350350
//headerfile
351351
if (!ra::cli::ParseArgument("headerfile", c.headerFilename, argc, argv))
352352
{
353353
//use the file name without extension as 'headerfile'.
354-
c.headerFilename = getDefaultHeaderFile(c);
354+
c.headerFilename = nameProvider.getDefaultHeaderFile(c.inputFilePath);
355355
}
356356
}
357357

@@ -499,7 +499,7 @@ int main(int argc, char* argv[])
499499
}
500500
else if (c.hasInputDir)
501501
{
502-
APP_ERROR_CODES error = processInputDirectory(c, generator);
502+
APP_ERROR_CODES error = processInputDirectory(c, &nameProvider, generator);
503503
if (error != APP_ERROR_SUCCESS)
504504
{
505505
ra::logging::Log(ra::logging::LOG_ERROR, "%s.", getErrorCodeDescription(error));
@@ -521,28 +521,6 @@ int main(int argc, char* argv[])
521521
return APP_ERROR_SUCCESS;
522522
}
523523

524-
std::string getDefaultFunctionIdentifier(const Context & c, Dictionary & identifiers_dictionary)
525-
{
526-
std::string output;
527-
528-
//use the file name without extension as 'identifier'.
529-
output = getUniqueFunctionIdentifierFromPath(c.inputFilePath.c_str(), identifiers_dictionary);
530-
output = ra::strings::CapitalizeFirstCharacter(output);
531-
532-
return output;
533-
}
534-
535-
std::string getDefaultHeaderFile(const Context & c)
536-
{
537-
std::string output;
538-
539-
//use the file name without extension as 'headerfile'.
540-
output = ra::filesystem::GetFilenameWithoutExtension(c.inputFilePath.c_str());
541-
output += ".h";
542-
543-
return output;
544-
}
545-
546524
APP_ERROR_CODES processInputFile(const Context & c, bin2cpp::IGenerator * generator)
547525
{
548526
// printing info
@@ -612,7 +590,7 @@ APP_ERROR_CODES processInputFile(const Context & c, bin2cpp::IGenerator * genera
612590
return APP_ERROR_SUCCESS;
613591
}
614592

615-
APP_ERROR_CODES processInputDirectory(const Context& c, bin2cpp::IGenerator * generator)
593+
APP_ERROR_CODES processInputDirectory(const Context& c, bin2cpp::INameProvider * nameProvider, bin2cpp::IGenerator * generator)
616594
{
617595
//check if input dir exists
618596
if (!ra::filesystem::DirectoryExists(c.inputDirPath.c_str()))
@@ -680,10 +658,10 @@ APP_ERROR_CODES processInputDirectory(const Context& c, bin2cpp::IGenerator * ge
680658
cCopy.inputFilePath = file;
681659

682660
//use the file name without extension as 'headerfile'.
683-
cCopy.headerFilename = getDefaultHeaderFile(cCopy);
661+
cCopy.headerFilename = nameProvider->getDefaultHeaderFile(cCopy.inputFilePath);
684662

685663
//use the file name without extension as 'identifier'.
686-
cCopy.functionIdentifier = getDefaultFunctionIdentifier(cCopy, identifiers_dictionary);
664+
cCopy.functionIdentifier = nameProvider->getDefaultFunctionIdentifier(cCopy.inputFilePath, identifiers_dictionary);
687665

688666
//build a relative file path
689667
std::string relative_file_path = file;

0 commit comments

Comments
 (0)