Skip to content

Commit 8b1d3b8

Browse files
committed
Implemented ITemplateVariableLookup and TemplateProcessor classes for processing code generation from templates.
1 parent 3a5f0d2 commit 8b1d3b8

File tree

7 files changed

+411
-0
lines changed

7 files changed

+411
-0
lines changed

src/bin2cpp/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_executable(bin2cpp
2222
enums.h
2323
IGenerator.h
2424
INameProvider.h
25+
ITemplateVariableLookup.h
2526
LegacyNameProvider.cpp
2627
LegacyNameProvider.h
2728
main.cpp
@@ -31,6 +32,8 @@ add_executable(bin2cpp
3132
SegmentGenerator.h
3233
StringGenerator.cpp
3334
StringGenerator.h
35+
TemplateProcessor.cpp
36+
TemplateProcessor.h
3437
types.h
3538
wildcard.cpp
3639
wildcard.h
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 ITEMPLATE_VARIABLE_LOOKUP_H
26+
#define ITEMPLATE_VARIABLE_LOOKUP_H
27+
28+
#include <string>
29+
30+
namespace bin2cpp
31+
{
32+
33+
class ITemplateVariableLookup
34+
{
35+
public:
36+
virtual ~ITemplateVariableLookup()
37+
{}
38+
39+
///<summary>
40+
///Look up the value for a given template variable name.
41+
///</summary>
42+
///<param name="name">The name of the template variable.</param>
43+
///<return>Returns the value of the template variable. Returns an empty string if unknown.<return>
44+
virtual std::string lookupTemplateVariable(const std::string& name) = 0;
45+
46+
};
47+
48+
}; //bin2cpp
49+
50+
#endif //ITEMPLATE_VARIABLE_LOOKUP_H

src/bin2cpp/TemplateProcessor.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 "TemplateProcessor.h"
26+
#include <fstream>
27+
#include <sstream>
28+
29+
namespace bin2cpp
30+
{
31+
TemplateProcessor::TemplateProcessor()
32+
{
33+
reset();
34+
}
35+
36+
TemplateProcessor::~TemplateProcessor()
37+
{
38+
}
39+
40+
void TemplateProcessor::reset()
41+
{
42+
mTemplateText = NULL;
43+
mVariableLookup = NULL;
44+
}
45+
46+
void TemplateProcessor::setTemplateText(const std::string* value)
47+
{
48+
mTemplateText = value;
49+
}
50+
51+
const std::string* TemplateProcessor::getTemplateText() const
52+
{
53+
return mTemplateText;
54+
}
55+
56+
void TemplateProcessor::setTemplateVariableLookup(ITemplateVariableLookup* lookup)
57+
{
58+
mVariableLookup = lookup;
59+
}
60+
61+
ITemplateVariableLookup* TemplateProcessor::getTemplateVariableLookup() const
62+
{
63+
return mVariableLookup;
64+
}
65+
66+
void TemplateProcessor::writeString(std::string& output)
67+
{
68+
std::ostringstream output_stream;
69+
std::set<std::string> recursion_history;
70+
processTemplate(output_stream, *mTemplateText, recursion_history);
71+
output = output_stream.str();
72+
}
73+
74+
bool TemplateProcessor::writeFile(const std::string& file_path)
75+
{
76+
std::ofstream output_file(file_path);
77+
if ( !output_file.is_open() ) return false;
78+
79+
std::set<std::string> recursion_history;
80+
processTemplate(output_file, *mTemplateText, recursion_history);
81+
return true;
82+
}
83+
84+
//-------------------------------
85+
//protected methods
86+
//-------------------------------
87+
88+
void TemplateProcessor::processTemplate(std::ostream& output_stream, const std::string& value, std::set<std::string>& recursion_history)
89+
{
90+
size_t pos = 0;
91+
while ( pos < value.size() )
92+
{
93+
if ( value[pos] == '$' && pos + 1 < value.size() && value[pos + 1] == '{' )
94+
{
95+
size_t end_pos = value.find('}', pos);
96+
if ( end_pos != std::string::npos )
97+
{
98+
std::string variable_name = value.substr(pos + 2, end_pos - pos - 2);
99+
100+
// Detect actual circular dependency within same recursion path
101+
if ( recursion_history.find(variable_name) != recursion_history.end() )
102+
{
103+
pos = end_pos + 1;
104+
continue;
105+
}
106+
107+
// Do the variable expansion
108+
std::string expanded_value = mVariableLookup ? mVariableLookup->lookupTemplateVariable(variable_name) : "";
109+
110+
// Add variable to recursion history before expanding
111+
recursion_history.insert(variable_name);
112+
113+
// Recursively process expanded value with updated recursion tracking
114+
processTemplate(output_stream, expanded_value, recursion_history);
115+
116+
// Remove variable from recursion history after recursion returns
117+
recursion_history.erase(variable_name);
118+
119+
pos = end_pos + 1;
120+
}
121+
else
122+
{
123+
output_stream.put(value[pos++]);
124+
}
125+
}
126+
else
127+
{
128+
output_stream.put(value[pos++]);
129+
}
130+
}
131+
}
132+
133+
}; //bin2cpp

src/bin2cpp/TemplateProcessor.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 TEMPLATEPROCESSOR_H
26+
#define TEMPLATEPROCESSOR_H
27+
28+
#include <string>
29+
#include <set>
30+
#include "ITemplateVariableLookup.h"
31+
32+
namespace bin2cpp
33+
{
34+
35+
///<summary>
36+
///A class for processing a template containing template markers variables
37+
///and replace those to their actual values.
38+
///</summary>
39+
class TemplateProcessor
40+
{
41+
public:
42+
TemplateProcessor();
43+
virtual ~TemplateProcessor();
44+
virtual void reset();
45+
46+
void setTemplateText(const std::string* value);
47+
const std::string* getTemplateText() const;
48+
49+
void setTemplateVariableLookup(ITemplateVariableLookup* lookup);
50+
ITemplateVariableLookup* getTemplateVariableLookup() const;
51+
52+
virtual void writeString(std::string& output);
53+
virtual bool writeFile(const std::string& file_path);
54+
55+
protected:
56+
virtual void processTemplate(std::ostream& output_stream, const std::string& value, std::set<std::string>& recursion_history);
57+
58+
//attributes
59+
const std::string* mTemplateText;
60+
ITemplateVariableLookup* mVariableLookup;
61+
};
62+
63+
}; //bin2cpp
64+
65+
#endif //TEMPLATEPROCESSOR_H

test/bin2cpp_unittest/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ source_group("External Files" FILES
285285
${CMAKE_SOURCE_DIR}/src/bin2cpp/common.h
286286
${CMAKE_SOURCE_DIR}/src/bin2cpp/wildcard.cpp
287287
${CMAKE_SOURCE_DIR}/src/bin2cpp/wildcard.h
288+
${CMAKE_SOURCE_DIR}/src/bin2cpp/ITemplateVariableLookup.h
289+
${CMAKE_SOURCE_DIR}/src/bin2cpp/TemplateProcessor.cpp
290+
${CMAKE_SOURCE_DIR}/src/bin2cpp/TemplateProcessor.h
288291
)
289292

290293
# Ensure source files are properly recognized as generated
@@ -297,6 +300,9 @@ add_executable(bin2cpp_unittest
297300
${CMAKE_SOURCE_DIR}/src/bin2cpp/common.h
298301
${CMAKE_SOURCE_DIR}/src/bin2cpp/wildcard.cpp
299302
${CMAKE_SOURCE_DIR}/src/bin2cpp/wildcard.h
303+
${CMAKE_SOURCE_DIR}/src/bin2cpp/ITemplateVariableLookup.h
304+
${CMAKE_SOURCE_DIR}/src/bin2cpp/TemplateProcessor.cpp
305+
${CMAKE_SOURCE_DIR}/src/bin2cpp/TemplateProcessor.h
300306
application.cpp
301307
application.h
302308
CMakeLists.txt
@@ -310,6 +316,8 @@ add_executable(bin2cpp_unittest
310316
TestCommon.h
311317
TestExtraction.cpp
312318
TestExtraction.h
319+
TestTemplateProcessor.cpp
320+
TestTemplateProcessor.h
313321
TestWildcard.cpp
314322
TestWildcard.h
315323
${GENERATED_TEST_FILES}

0 commit comments

Comments
 (0)