Skip to content

Commit 3062715

Browse files
committed
First draft for the Context class.
1 parent 5b0d805 commit 3062715

File tree

3 files changed

+335
-0
lines changed

3 files changed

+335
-0
lines changed

src/bin2cpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ add_executable(bin2cpp
1515
bin2cpp.samples.txt
1616
common.cpp
1717
common.h
18+
Context.cpp
19+
Context.h
1820
crc32.cpp
1921
crc32.h
2022
IGenerator.h

src/bin2cpp/Context.cpp

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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 "Context.h"
26+
27+
namespace bin2cpp
28+
{
29+
Context::Context()
30+
{
31+
reset();
32+
}
33+
34+
Context::~Context()
35+
{
36+
}
37+
38+
void Context::reset()
39+
{
40+
mHasInputFile = false;
41+
mHasInputDir = false;
42+
mHasOutputDir = false;
43+
mHasReportedFilePath = false;
44+
mHasManagerFile = false;
45+
mChunkSize = 0;
46+
mCppEncoder = IGenerator::CppEncoderEnum::CPP_ENCODER_HEX;
47+
48+
memset(mFlags, 0, sizeof(mFlags));
49+
}
50+
51+
bool Context::hasInputFileSet() const
52+
{
53+
return mHasInputFile;
54+
}
55+
56+
bool Context::hasInputDirSet() const
57+
{
58+
return mHasInputDir;
59+
}
60+
61+
bool Context::hasOutputDirSet() const
62+
{
63+
return mHasOutputDir;
64+
}
65+
66+
bool Context::hasReportedFilePath() const
67+
{
68+
return mHasReportedFilePath;
69+
}
70+
71+
void Context::setFlag(const Flags& flag, bool value)
72+
{
73+
if ( flag > 0 && flag < FLAG_COUNT )
74+
mFlags[flag] = value;
75+
}
76+
77+
bool Context::isFlagSet(const Flags& flag) const
78+
{
79+
if ( flag > 0 && flag < FLAG_COUNT )
80+
return mFlags[flag];
81+
return false;
82+
}
83+
84+
void Context::setInputFilePath(const char* path)
85+
{
86+
if ( path )
87+
{
88+
mInputFilePath = path;
89+
mHasInputFile = true;
90+
}
91+
}
92+
93+
const std::string & Context::getInputFilePath() const
94+
{
95+
return mInputFilePath;
96+
}
97+
98+
void Context::setInputDirPath(const char* path)
99+
{
100+
if ( path )
101+
{
102+
mInputDirPath = path;
103+
mHasInputDir = true;
104+
}
105+
}
106+
107+
const std::string & Context::getInputDirPath() const
108+
{
109+
return mInputDirPath;
110+
}
111+
112+
void Context::setOutputDirPath(const char* path)
113+
{
114+
if ( path )
115+
{
116+
mOutputDirPath = path;
117+
mHasOutputDir = true;
118+
}
119+
}
120+
121+
const std::string & Context::getOutputDirPath() const
122+
{
123+
return mOutputDirPath;
124+
}
125+
126+
void Context::setHeaderFilename(const char* path)
127+
{
128+
if ( path )
129+
mHeaderFilename = path;
130+
}
131+
132+
const std::string & Context::getHeaderFilename() const
133+
{
134+
return mHeaderFilename;
135+
}
136+
137+
void Context::setFunctionIdentifier(const char* function_identifier)
138+
{
139+
if ( function_identifier )
140+
mFunctionIdentifier = function_identifier;
141+
}
142+
143+
const std::string & Context::getFunctionIdentifier() const
144+
{
145+
return mFunctionIdentifier;
146+
}
147+
148+
void Context::setReportedFilePath(const char* path)
149+
{
150+
if ( path )
151+
mReportedFilePath = path;
152+
}
153+
154+
const std::string & Context::getReportedFilePath() const
155+
{
156+
return mReportedFilePath;
157+
}
158+
159+
void Context::setChunkSize(size_t chunk_size)
160+
{
161+
mChunkSize = chunk_size;
162+
}
163+
164+
size_t Context::getChunkSize() const
165+
{
166+
return mChunkSize;
167+
}
168+
169+
void Context::setNamespace(const char* name)
170+
{
171+
if ( name )
172+
mNamespace = name;
173+
}
174+
175+
const std::string & Context::getNamespace() const
176+
{
177+
return mNamespace;
178+
}
179+
180+
void Context::setBaseClass(const char* name)
181+
{
182+
if ( name )
183+
mBaseClass = name;
184+
}
185+
186+
const std::string & Context::getBaseClass() const
187+
{
188+
return mBaseClass;
189+
}
190+
191+
void Context::setManagerHeaderFilename(const char* manager_file)
192+
{
193+
if ( manager_file )
194+
mManagerHeaderFilename = manager_file;
195+
}
196+
197+
const std::string & Context::getManagerHeaderFilename() const
198+
{
199+
return mManagerHeaderFilename;
200+
}
201+
202+
void Context::setCppEncoder(const IGenerator::CppEncoderEnum& cpp_encoder)
203+
{
204+
mCppEncoder = cpp_encoder;
205+
}
206+
207+
IGenerator::CppEncoderEnum Context::getCppEncoder() const
208+
{
209+
return mCppEncoder;
210+
}
211+
212+
213+
//-------------------------------
214+
//protected methods
215+
//-------------------------------
216+
217+
218+
}; //bin2cpp

src/bin2cpp/Context.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 CONTEXT_H
26+
#define CONTEXT_H
27+
28+
#include <string>
29+
#include "IGenerator.h"
30+
31+
namespace bin2cpp
32+
{
33+
34+
///<summary>
35+
///This is the command line context for generating code.
36+
///</summary>
37+
class Context
38+
{
39+
public:
40+
Context();
41+
virtual ~Context();
42+
43+
enum Flags
44+
{
45+
KEEP_DIRECTORY_STRUCTURE = 0, // true if the output files must have the same directory structure as the input files. Valid only when --dir is used.
46+
PLAIN_OUTPUT,
47+
OVERRIDE_EXISTING,
48+
REGISTER_FILE,
49+
50+
FLAG_COUNT
51+
};
52+
53+
void reset();
54+
55+
bool hasInputFileSet() const;
56+
bool hasInputDirSet() const;
57+
bool hasOutputDirSet() const;
58+
bool hasReportedFilePath() const;
59+
bool hasManagerFile() const;
60+
61+
void setFlag(const Flags & flag, bool value);
62+
bool isFlagSet(const Flags& flag) const;
63+
64+
void setInputFilePath(const char* path);
65+
const std::string& getInputFilePath() const;
66+
void setInputDirPath(const char* path);
67+
const std::string& getInputDirPath() const;
68+
void setOutputDirPath(const char* path);
69+
const std::string& getOutputDirPath() const;
70+
void setHeaderFilename(const char* path);
71+
const std::string& getHeaderFilename() const;
72+
void setFunctionIdentifier(const char* function_identifier);
73+
const std::string& getFunctionIdentifier() const;
74+
void setReportedFilePath(const char* path);
75+
const std::string& getReportedFilePath() const;
76+
void setChunkSize(size_t chunk_size);
77+
size_t getChunkSize() const;
78+
void setNamespace(const char* name);
79+
const std::string& getNamespace() const;
80+
void setBaseClass(const char* name);
81+
const std::string& getBaseClass() const;
82+
void setManagerHeaderFilename(const char* manager_file);
83+
const std::string& getManagerHeaderFilename() const;
84+
void setCppEncoder(const IGenerator::CppEncoderEnum& cpp_encoder);
85+
IGenerator::CppEncoderEnum getCppEncoder() const;
86+
87+
88+
protected:
89+
90+
//attributes
91+
bool mHasInputFile; // true if 'mInputFilePath' is set.
92+
bool mHasInputDir; // true if 'mInputDirPath' is set.
93+
bool mHasOutputDir; // true if 'mOutputDirPath' is set.
94+
bool mHasReportedFilePath; // true if 'mReportedFilePath' is set.
95+
bool mHasManagerFile; // true if 'mManagerHeaderFilename' is set.
96+
97+
bool mFlags[Flags::FLAG_COUNT];
98+
99+
std::string mInputFilePath; // path of the input binary file
100+
std::string mInputDirPath;
101+
std::string mOutputDirPath;
102+
std::string mHeaderFilename;
103+
std::string mFunctionIdentifier;
104+
std::string mReportedFilePath; // path reported in the public api when calling getFilePath();
105+
size_t mChunkSize;
106+
std::string mNamespace;
107+
std::string mBaseClass;
108+
std::string mManagerHeaderFilename;
109+
IGenerator::CppEncoderEnum mCppEncoder;
110+
std::string mGeneratorName;
111+
};
112+
113+
}; //bin2cpp
114+
115+
#endif //BASEGENERATOR_H

0 commit comments

Comments
 (0)