Skip to content

Commit fc9663a

Browse files
committed
Added separated common attributes creator.
1 parent 74b4b2d commit fc9663a

File tree

6 files changed

+169
-90
lines changed

6 files changed

+169
-90
lines changed

DbcScanner.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
<ClInclude Include="src\codegen\c-main-generator.h" />
159159
<ClInclude Include="src\codegen\c-sigprinter.h" />
160160
<ClInclude Include="src\codegen\filewriter.h" />
161+
<ClInclude Include="src\codegen\fs-creator.h" />
161162
<ClInclude Include="src\parser\dbclineparser.h" />
162163
<ClInclude Include="src\parser\dbcscanner.h" />
163164
<ClInclude Include="src\types\attributes.h" />
@@ -170,6 +171,7 @@
170171
<ClCompile Include="src\codegen\c-main-generator.cpp" />
171172
<ClCompile Include="src\codegen\c-sigprinter.cpp" />
172173
<ClCompile Include="src\codegen\filewriter.cpp" />
174+
<ClCompile Include="src\codegen\fs-creator.cpp" />
173175
<ClCompile Include="src\parser\dbclineparser.cpp" />
174176
<ClCompile Include="src\parser\dbcscanner.cpp" />
175177
</ItemGroup>

DbcScanner.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<ClInclude Include="src\codegen\filewriter.h">
4646
<Filter>Header Files</Filter>
4747
</ClInclude>
48+
<ClInclude Include="src\codegen\fs-creator.h">
49+
<Filter>Header Files</Filter>
50+
</ClInclude>
4851
</ItemGroup>
4952
<ItemGroup>
5053
<ClCompile Include="src\parser\dbclineparser.cpp">
@@ -62,5 +65,8 @@
6265
<ClCompile Include="src\codegen\filewriter.cpp">
6366
<Filter>Source Files</Filter>
6467
</ClCompile>
68+
<ClCompile Include="src\codegen\fs-creator.cpp">
69+
<Filter>Source Files</Filter>
70+
</ClCompile>
6571
</ItemGroup>
6672
</Project>

src/codegen/c-main-generator.cpp

Lines changed: 9 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,13 @@ char* PrintF(const char* format, ...)
3737
return wbuff;
3838
}
3939

40-
std::string str_toupper(std::string s)
41-
{
42-
std::transform(s.begin(), s.end(), s.begin(),
43-
[](unsigned char c)
44-
{
45-
return std::toupper(c);
46-
});
47-
return s;
48-
}
49-
5040
CiMainGenerator::CiMainGenerator()
5141
{
5242
sigprt = new CSigPrinter;
5343
fwriter = new FileWriter;
5444
}
5545

56-
void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
57-
std::string drvname,
58-
std::string dirpath)
46+
void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs, const FsDescriptor_t& fsd)
5947
{
6048
// Load income messages to sig printer
6149
sigprt->LoadMessages(msgs);
@@ -66,21 +54,6 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
6654
return a->msg.MsgID < b->msg.MsgID;
6755
});
6856

69-
auto dirok = SetFinalPath(dirpath);
70-
71-
if (!dirok)
72-
{
73-
// TODO: handle error if directory cannot be used
74-
}
75-
76-
SetCommonValues(drvname);
77-
78-
// work_dir_path has the base dir path to gen files
79-
// 1 step is to define final directory for source code bunch
80-
mhead.dir = work_dir_path;
81-
mhead.fname = drvname + ".h";
82-
mhead.fpath = mhead.dir + "/" + mhead.fname;
83-
8457
// 2 step is to print main head file
8558
fwriter->AppendLine("#pragma once", 2);
8659
fwriter->AppendLine("#ifdef __cplusplus\nextern \"C\" {\n#endif", 2);
@@ -180,28 +153,28 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
180153
{
181154
// write message typedef s and additional expressions
182155
MessageDescriptor_t& m = sigprt->sigs_expr[num]->msg;
183-
156+
184157
fwriter->AppendLine(
185158
PrintF("uint32_t Unpack_%s_%s(%s_t* _m, const uint8_t* _d, uint8_t dlc_);",
186-
m.Name.c_str(), drvname.c_str(), m.Name.c_str()));
159+
m.Name.c_str(), fsd.drvname.c_str(), m.Name.c_str()));
187160

188161
fwriter->AppendLine(PrintF("#ifdef %s", usestruct_str.c_str()));
189-
162+
190163
fwriter->AppendLine(
191164
PrintF("uint32_t Pack_%s_%s(const %s_t* _m, __CoderDbcCanFrame_t__* cframe);",
192-
m.Name.c_str(), drvname.c_str(), m.Name.c_str()));
193-
165+
m.Name.c_str(), fsd.drvname.c_str(), m.Name.c_str()));
166+
194167
fwriter->AppendLine("#else");
195-
168+
196169
fwriter->AppendLine(
197170
PrintF("uint32_t Pack_%s_%s(const %s_t* _m, uint8_t* _d, uint8_t* _len, uint8_t* _ide);",
198-
m.Name.c_str(), drvname.c_str(), m.Name.c_str()));
171+
m.Name.c_str(), fsd.drvname.c_str(), m.Name.c_str()));
199172

200173
fwriter->AppendLine(PrintF("#endif // %s", usestruct_str.c_str()), 2);
201174
}
202175

203176
fwriter->AppendLine("#ifdef __cplusplus\n}\n#endif");
204-
fwriter->Flush(mhead.fpath);
177+
fwriter->Flush(fsd.core_h.fpath);
205178

206179
// 3 step is to print main source file
207180

@@ -269,48 +242,3 @@ void CiMainGenerator::WriteSigStructField(const SignalDescriptor_t& sig, bool bi
269242
fwriter->AppendLine("", 2);
270243
}
271244

272-
bool CiMainGenerator::SetFinalPath(std::string dirpath)
273-
{
274-
// find free directory
275-
struct stat info;
276-
277-
for (int32_t dirnum = 0; dirnum < 1000; dirnum++)
278-
{
279-
snprintf(wbuff, kWBUFF_len, "%03d", dirnum);
280-
work_dir_path = dirpath + "/" + wbuff;
281-
282-
if (stat(work_dir_path.c_str(), &info) != 0)
283-
{
284-
if (std::filesystem::create_directory(work_dir_path))
285-
return true;
286-
else
287-
return false;
288-
}
289-
else if (info.st_mode & S_IFDIR)
290-
{
291-
// directory exists, try next num
292-
continue;
293-
}
294-
else
295-
{
296-
if (std::filesystem::create_directory(work_dir_path) != 0)
297-
return false;
298-
}
299-
}
300-
301-
return true;
302-
}
303-
304-
void CiMainGenerator::SetCommonValues(const std::string& drvname)
305-
{
306-
DRVNAME = str_toupper(drvname);
307-
308-
snprintf(wbuff, kWBUFF_len, "%s_USE_BITS_SIGNAL", DRVNAME.c_str());
309-
usebits_str = wbuff;
310-
311-
snprintf(wbuff, kWBUFF_len, "%s_USE_DIAG_MONITORS", DRVNAME.c_str());
312-
usediag_str = wbuff;
313-
314-
snprintf(wbuff, kWBUFF_len, "%s_USE_CANSTRUCT", DRVNAME.c_str());
315-
usestruct_str = wbuff;
316-
}

src/codegen/c-main-generator.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
#include "filewriter.h"
66
#include "../types/message.h"
77
#include "../types/outfile.h"
8+
#include "fs-creator.h"
89

910
class CiMainGenerator {
1011
public:
1112
CiMainGenerator();
1213

13-
void Generate(std::vector<MessageDescriptor_t*>& msgs, std::string drvname, std::string dirpath);
14+
void Generate(std::vector<MessageDescriptor_t*>& msgs, const FsDescriptor_t& fsd);
1415

1516
private:
16-
bool SetFinalPath(std::string dirpath);
17-
18-
void SetCommonValues(const std::string& drvname);
1917

2018
void WriteSigStructField(const SignalDescriptor_t& sig, bool bitfield, size_t pad);
2119

@@ -35,9 +33,4 @@ class CiMainGenerator {
3533
CSigPrinter* sigprt;
3634

3735
FileWriter* fwriter;
38-
39-
OutFileDescriptor_t mhead;
40-
OutFileDescriptor_t mcode;
41-
OutFileDescriptor_t fhead;
42-
OutFileDescriptor_t fcode;
4336
};

src/codegen/fs-creator.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "fs-creator.h"
2+
#include <sys/stat.h>
3+
#include <iostream>
4+
#include <fstream>
5+
#include <cstdlib>
6+
#include <filesystem>
7+
8+
static const int32_t kTmpLen = 1024;
9+
10+
static char _tmpb[kTmpLen];
11+
12+
13+
std::string str_toupper(std::string s)
14+
{
15+
std::transform(s.begin(), s.end(), s.begin(),
16+
[](unsigned char c)
17+
{
18+
return std::toupper(c);
19+
});
20+
return s;
21+
}
22+
23+
24+
std::string str_tolower(std::string s)
25+
{
26+
std::transform(s.begin(), s.end(), s.begin(),
27+
[](unsigned char c)
28+
{
29+
return std::tolower(c);
30+
});
31+
return s;
32+
}
33+
34+
FsCreator::FsCreator()
35+
{
36+
}
37+
38+
bool FsCreator::PrepareDirectory(std::string drvname, std::string basepath, bool rw)
39+
{
40+
bool ret = false;
41+
42+
// find free directory
43+
struct stat info;
44+
45+
std::string work_dir_path;
46+
47+
for (int32_t dirnum = 0; dirnum < 1000; dirnum++)
48+
{
49+
snprintf(_tmpb, kTmpLen, "%03d", dirnum);
50+
work_dir_path = basepath + "/" + _tmpb;
51+
52+
if (stat(work_dir_path.c_str(), &info) != 0)
53+
{
54+
if (std::filesystem::create_directory(work_dir_path))
55+
{
56+
ret = true;
57+
break;
58+
}
59+
}
60+
else if (info.st_mode & S_IFDIR)
61+
{
62+
// directory exists, try next num
63+
continue;
64+
}
65+
else
66+
{
67+
if (std::filesystem::create_directory(work_dir_path) != 0)
68+
{
69+
ret = false;
70+
break;
71+
}
72+
}
73+
}
74+
75+
if (ret)
76+
{
77+
// directory valid and exists, set all the values
78+
FS.drvname = drvname;
79+
FS.DRVNAME = str_toupper(drvname);
80+
81+
std::string ldrvname = str_tolower(drvname);
82+
83+
FS.core_h.dir = work_dir_path;
84+
FS.core_h.fname = ldrvname + ".h";
85+
FS.core_h.fpath = work_dir_path + "/" + FS.core_h.fname;
86+
87+
FS.core_c.dir = work_dir_path;
88+
FS.core_c.fname = ldrvname + ".c";
89+
FS.core_c.fpath = work_dir_path + "/" + FS.core_c.fname;
90+
91+
FS.util_h.dir = work_dir_path;
92+
FS.util_h.fname = ldrvname + "_binutil" + ".h";
93+
FS.util_h.fpath = work_dir_path + "/" + FS.util_h.fname;
94+
95+
FS.util_c.dir = work_dir_path;
96+
FS.util_c.fname = ldrvname + "_binutil" + ".c";
97+
FS.util_c.fpath = work_dir_path + "/" + FS.util_c.fname;
98+
99+
snprintf(_tmpb, kTmpLen, "%s_USE_BITS_SIGNAL", FS.DRVNAME.c_str());
100+
FS.usebits_def = _tmpb;
101+
102+
snprintf(_tmpb, kTmpLen, "%s_USE_DIAG_MONITORS", FS.DRVNAME.c_str());
103+
FS.usemon_def = _tmpb;
104+
105+
snprintf(_tmpb, kTmpLen, "%s_USE_CANSTRUCT", FS.DRVNAME.c_str());
106+
FS.usesruct_def = _tmpb;
107+
}
108+
109+
return ret;
110+
}

src/codegen/fs-creator.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <string>
5+
#include "../types/outfile.h"
6+
7+
typedef struct
8+
{
9+
// low case driver name
10+
std::string drvname;
11+
// up case driver name
12+
std::string DRVNAME;
13+
14+
OutFileDescriptor_t core_h;
15+
OutFileDescriptor_t core_c;
16+
17+
OutFileDescriptor_t util_h;
18+
OutFileDescriptor_t util_c;
19+
20+
std::string usebits_def;
21+
std::string usesruct_def;
22+
std::string usemon_def;
23+
24+
} FsDescriptor_t;
25+
26+
// This class is used to build all neccessary string -ed
27+
// value that will be required during code generation
28+
// (paths, file names, drvnames, common defines etc)
29+
// if preparation ends with true, the collection of
30+
// values can be used.
31+
class FsCreator {
32+
public:
33+
FsCreator();
34+
35+
bool PrepareDirectory(std::string drvname, std::string basepath, bool rw);
36+
37+
FsDescriptor_t FS;
38+
39+
};
40+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy