Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit14f7017

Browse files
committed
Added part of main .h file writer. Added filewriter.
1 parent7b13500 commit14f7017

File tree

4 files changed

+284
-50
lines changed

4 files changed

+284
-50
lines changed

‎src/codegen/c-main-generator.cpp‎

Lines changed: 205 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,223 @@
55
#include<cstdlib>
66
#include<filesystem>
77
#include<algorithm>
8+
#include<regex>
89

910
#include"c-main-generator.h"
1011

1112
staticconstsize_tkMaxDirNum =1000;
1213

1314
staticconstsize_tkWBUFF_len =2048;
14-
staticchar wbuff[kWBUFF_len] = {0 };
15+
staticchar wbuff[kWBUFF_len] = {0};
16+
17+
static std::string __typeprint[] =
18+
{
19+
"int8_t",
20+
"int16_t",
21+
"int32_t",
22+
"int64_t",
23+
"uint8_t",
24+
"uint16_t",
25+
"uint32_t",
26+
"uint64_t"
27+
};
28+
29+
std::stringstr_toupper(std::string s)
30+
{
31+
std::transform(s.begin(), s.end(), s.begin(),
32+
[](unsignedchar c)
33+
{
34+
returnstd::toupper(c);
35+
});
36+
return s;
37+
}
1538

1639
CiMainGenerator::CiMainGenerator()
1740
{
1841
sigprt =new CSigPrinter;
42+
fwriter =new FileWriter;
1943
}
2044

21-
voidCiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs, std::string drvname, std::string dirpath)
45+
voidCiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
46+
std::string drvname,
47+
std::string dirpath)
2248
{
23-
// Load income messages to sig printer
49+
// Load income messages to sig printer
2450
sigprt->LoadMessages(msgs);
2551

2652
std::sort(sigprt->sigs_expr.begin(), sigprt->sigs_expr.end(),
27-
[](const CiExpr_t* a,const CiExpr_t* b) ->bool
28-
{
29-
return a->msg.MsgID < b->msg.MsgID;
30-
});
53+
[](const CiExpr_t* a,const CiExpr_t* b) ->bool
54+
{
55+
return a->msg.MsgID < b->msg.MsgID;
56+
});
57+
58+
auto dirok =SetFinalPath(dirpath);
59+
60+
if (!dirok)
61+
{
62+
// TODO: handle error if directory cannot be used
63+
}
64+
65+
SetCommonValues(drvname);
66+
67+
// work_dir_path has the base dir path to gen files
68+
mhead.dir = work_dir_path;
69+
mhead.fname = drvname +".h";
70+
mhead.fpath = mhead.dir +"/" + mhead.fname;
3171

32-
SetFinalPath(dirpath);
3372
// 1 step is to define final directory for source code bunch
73+
fwriter->AppendLine("#pragma once",3);
74+
fwriter->AppendLine("#ifdef __cplusplus\nextern\"C\" {\n#endif",2);
75+
fwriter->AppendLine("#include <stdint.h>");
76+
77+
for (size_t num =0; num < sigprt->sigs_expr.size(); num++)
78+
{
79+
// write message typedef s and additional expressions
80+
MessageDescriptor_t& m = sigprt->sigs_expr[num]->msg;
81+
82+
snprintf(wbuff,kWBUFF_len,"// def @%s CAN Message (%-4d %#x)", m.Name.c_str(), m.MsgID, m.MsgID);
83+
fwriter->AppendLine(wbuff);
84+
snprintf(wbuff,kWBUFF_len,"#define %s_IDE (%uU)", m.Name.c_str(), m.IsExt);
85+
fwriter->AppendLine(wbuff);
86+
snprintf(wbuff,kWBUFF_len,"#define %s_DLC (%uU)", m.Name.c_str(), m.DLC);
87+
fwriter->AppendLine(wbuff);
88+
snprintf(wbuff,kWBUFF_len,"#define %s_CANID (%#x)", m.Name.c_str(), m.MsgID);
89+
fwriter->AppendLine(wbuff);
90+
91+
if (m.Cycle >0)
92+
{
93+
snprintf(wbuff,kWBUFF_len,"#define %s_CYC (%dU)", m.Name.c_str(), m.Cycle);
94+
fwriter->AppendLine(wbuff);
95+
}
96+
97+
if (m.CommentText.size() >0)
98+
{
99+
fwriter->AppendLine("// --" + m.CommentText);
100+
}
101+
102+
size_t max_sig_name_len =27;
103+
104+
for (size_t signum =0; signum < m.Signals.size(); signum++)
105+
{
106+
SignalDescriptor_t& s = m.Signals[signum];
107+
108+
// TODO: print signal to_S and from_S definitions if necessary
109+
//string ret = cprint.PrintSignalPackExpression(sig, msg.MessageName);
110+
111+
//if (ret != null)
112+
//{
113+
// fwriter->AppendLine(ret);
114+
//}
115+
if (s.Name.size() > max_sig_name_len)
116+
max_sig_name_len = s.Name.size();
117+
}
118+
119+
snprintf(wbuff,kWBUFF_len,"typedef struct");
120+
fwriter->AppendLine(wbuff);
121+
122+
fwriter->AppendLine("{\n");
123+
124+
// Write section for bitfielded part
125+
snprintf(wbuff,kWBUFF_len,"#ifdef %s", usebits_str.c_str());
126+
fwriter->AppendLine(wbuff,2);
127+
128+
for (size_t signum =0; signum < m.Signals.size(); signum++)
129+
{
130+
SignalDescriptor_t& sig = m.Signals[signum];
131+
// Write bit-fielded part
132+
WriteSigStructField(sig,true, max_sig_name_len);
133+
}
134+
135+
// Write clean part
136+
fwriter->AppendLine("#else",2);
137+
138+
for (size_t signum =0; signum < m.Signals.size(); signum++)
139+
{
140+
SignalDescriptor_t& sig = m.Signals[signum];
141+
// Write clean signals
142+
WriteSigStructField(sig,false, max_sig_name_len);
143+
}
144+
145+
snprintf(wbuff,kWBUFF_len,"#endif // %s", usebits_str.c_str());
146+
fwriter->AppendLine(wbuff,2);
34147

148+
//if (CodeSett.Code.UseMonitors == 1)
149+
// fwriter->AppendLine(" FrameMonitor_t mon1;");
150+
151+
snprintf(wbuff,kWBUFF_len,"} %s_t;", m.Name.c_str());
152+
fwriter->AppendLine(wbuff,2);
153+
//fwriter->AppendLine("} " + msg.MessageName + "_t;");
154+
//fwriter->AppendLine();
155+
}
156+
157+
fwriter->AppendLine("#ifdef __cplusplus\n}\n#endif");
158+
fwriter->Flush(mhead.fpath);
35159
// 2 step is to print main head file
36-
160+
37161
// 3 step is to print main source file
38162

39163
// 4 step is to pring fmon head file
40164

41165
// 5 step is to print fmon source file
166+
}
167+
168+
voidCiMainGenerator::WriteSigStructField(const SignalDescriptor_t& sig,bool bits,size_t padwidth)
169+
{
170+
if (sig.CommentText.size() >0)
171+
{
172+
fwriter->AppendLine(" //" +std::regex_replace(sig.CommentText,std::regex("\n"),"\n //"));
173+
}
174+
175+
if (sig.ValueText.size() >0)
176+
{
177+
fwriter->AppendLine(" //" +std::regex_replace(sig.ValueText,std::regex("\n"),"\n //"));
178+
}
179+
180+
std::string dtype ="";
181+
182+
dtype +="" + __typeprint[(int)sig.Type] +"" + sig.Name;
183+
184+
if (bits && (sig.LengthBit <8))
185+
{
186+
snprintf(wbuff,kWBUFF_len," : %d", sig.LengthBit);
187+
dtype += wbuff;
188+
}
189+
190+
dtype +=";";
191+
192+
std::string pad ="";
193+
194+
dtype += pad.insert(0, padwidth +16 - dtype.size(),'');
42195

196+
fwriter->AppendText(dtype);
197+
198+
pad =" //";
199+
pad += (sig.Signed) ?" [-]" :"";
200+
201+
fwriter->AppendText(pad);
202+
203+
snprintf(wbuff,kWBUFF_len," Bits=%2d", sig.LengthBit);
204+
fwriter->AppendText(wbuff);
205+
206+
if (sig.Unit.size() >0)
207+
{
208+
snprintf(wbuff,kWBUFF_len," Unit:'%-13s'", sig.Unit.c_str());
209+
fwriter->AppendText(wbuff);
210+
}
211+
212+
if (sig.Offset !=0)
213+
{
214+
snprintf(wbuff,kWBUFF_len," Offset= %-18f", sig.Offset);
215+
fwriter->AppendText(wbuff);
216+
}
217+
218+
if (sig.Factor !=1)
219+
{
220+
snprintf(wbuff,kWBUFF_len," Factor= %-15d", sig.LengthBit);
221+
fwriter->AppendText(wbuff);
222+
}
223+
224+
fwriter->AppendLine("",2);
43225
}
44226

45227
boolCiMainGenerator::SetFinalPath(std::string dirpath)
@@ -73,3 +255,17 @@ bool CiMainGenerator::SetFinalPath(std::string dirpath)
73255

74256
returntrue;
75257
}
258+
259+
voidCiMainGenerator::SetCommonValues(const std::string& drvname)
260+
{
261+
DRVNAME =str_toupper(drvname);
262+
263+
snprintf(wbuff,kWBUFF_len,"%s_USE_BITS_SIGNAL", DRVNAME.c_str());
264+
usebits_str = wbuff;
265+
266+
snprintf(wbuff,kWBUFF_len,"%s_USE_DIAG_MONITORS", DRVNAME.c_str());
267+
usediag_str = wbuff;
268+
269+
snprintf(wbuff,kWBUFF_len,"%s_USE_CANSTRUCT", DRVNAME.c_str());
270+
canframe_str = wbuff;
271+
}

‎src/codegen/c-main-generator.h‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,42 @@
22

33
#include<stdint.h>
44
#include"c-sigprinter.h"
5+
#include"filewriter.h"
56
#include"../types/message.h"
7+
#include"../types/outfile.h"
68

7-
classCiMainGenerator
8-
{
9-
public:
10-
CiMainGenerator();
9+
classCiMainGenerator {
10+
public:
11+
CiMainGenerator();
1112

12-
voidGenerate(std::vector<MessageDescriptor_t*>& msgs, std::string drvname, std::string dirpath);
13+
voidGenerate(std::vector<MessageDescriptor_t*>& msgs, std::string drvname, std::string dirpath);
1314

14-
private:
15-
16-
boolSetFinalPath(std::string dirpath);
15+
private:
16+
boolSetFinalPath(std::string dirpath);
1717

18-
private:
18+
voidSetCommonValues(const std::string& drvname);
1919

20-
std::string work_dir_path;
21-
22-
std::string workstr;
20+
voidWriteSigStructField(const SignalDescriptor_t& sig,bool bitfield,size_t pad);
2321

24-
CSigPrinter* sigprt;
22+
private:
23+
std::string work_dir_path;
2524

25+
std::string workstr;
26+
27+
std::string DRVNAME;
28+
29+
std::string usebits_str;
30+
std::string usediag_str;
31+
std::string canframe_str;
32+
33+
std::vector<std::string> tmpvect;
34+
35+
CSigPrinter* sigprt;
36+
37+
FileWriter* fwriter;
38+
39+
OutFileDescriptor_t mhead;
40+
OutFileDescriptor_t mcode;
41+
OutFileDescriptor_t fhead;
42+
OutFileDescriptor_t fcode;
2643
};

‎src/codegen/filewriter.cpp‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,57 @@
1+
#include<iostream>
2+
#include<fstream>
13
#include"filewriter.h"
24

5+
6+
FileWriter::FileWriter()
7+
{
8+
}
9+
10+
FileWriter::~FileWriter()
11+
{
12+
}
13+
314
voidFileWriter::Flush()
415
{
5-
strm.clear();
16+
strm.clear();
617
}
718

8-
voidFileWriter::AppendText(constchar* text,size_t len)
19+
voidFileWriter::Flush(conststd::string& fpath)
920
{
10-
strm.write(text, len);
21+
std::ofstream wfile;
22+
23+
wfile.open(fpath, std::ios::out);
24+
25+
wfile << strm.rdbuf();
26+
27+
wfile.close();
28+
29+
Flush();
1130
}
1231

13-
voidFileWriter::AppendLine(constchar* text,size_t len)
32+
voidFileWriter::AppendText(constchar* text)
1433
{
15-
AppendText(text, len);
16-
AppendText("\n",1);
34+
std::string str = text;
35+
AppendText(str);
36+
}
37+
38+
voidFileWriter::AppendLine(constchar* text,int32_t post_empty_lines)
39+
{
40+
AppendText(text);
41+
42+
for (int32_t i =0; i < post_empty_lines; i++)
43+
AppendText("\n");
1744
}
1845

1946

2047
voidFileWriter::AppendText(const std::string& str)
2148
{
22-
strm.str(str);
49+
strm <<str;
2350
}
2451

2552
voidFileWriter::AppendLine(const std::string& str)
2653
{
27-
AppendText(str);
28-
AppendText("\n",1);
54+
AppendText(str);
55+
AppendText("\n");
2956
}
3057

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp