Skip to content

Commit 74b4b2d

Browse files
committed
Added printF optimization.
1 parent 98c8eda commit 74b4b2d

File tree

1 file changed

+40
-48
lines changed

1 file changed

+40
-48
lines changed

src/codegen/c-main-generator.cpp

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <iostream>
44
#include <fstream>
55
#include <cstdlib>
6+
#include <stdarg.h>
67
#include <filesystem>
78
#include <algorithm>
89
#include <regex>
@@ -13,7 +14,7 @@ static const size_t kMaxDirNum = 1000;
1314

1415
static const size_t kWBUFF_len = 2048;
1516

16-
static char wbuff[kWBUFF_len] = {0};
17+
static char wbuff[kWBUFF_len] = { 0 };
1718

1819
static std::string __typeprint[] =
1920
{
@@ -27,6 +28,15 @@ static std::string __typeprint[] =
2728
"uint64_t"
2829
};
2930

31+
char* PrintF(const char* format, ...)
32+
{
33+
va_list args;
34+
va_start(args, format);
35+
vsnprintf(wbuff, kWBUFF_len, format, args);
36+
va_end(args);
37+
return wbuff;
38+
}
39+
3040
std::string str_toupper(std::string s)
3141
{
3242
std::transform(s.begin(), s.end(), s.begin(),
@@ -89,27 +99,21 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
8999

90100
);
91101

92-
snprintf(wbuff, kWBUFF_len, "#endif // %s", usediag_str.c_str());
93-
fwriter->AppendLine(wbuff, 3);
102+
fwriter->AppendLine(PrintF("#endif // %s", usediag_str.c_str()), 3);
94103

95104
for (size_t num = 0; num < sigprt->sigs_expr.size(); num++)
96105
{
97106
// write message typedef s and additional expressions
98107
MessageDescriptor_t& m = sigprt->sigs_expr[num]->msg;
99108

100-
snprintf(wbuff, kWBUFF_len, "// def @%s CAN Message (%-4d %#x)", m.Name.c_str(), m.MsgID, m.MsgID);
101-
fwriter->AppendLine(wbuff);
102-
snprintf(wbuff, kWBUFF_len, "#define %s_IDE (%uU)", m.Name.c_str(), m.IsExt);
103-
fwriter->AppendLine(wbuff);
104-
snprintf(wbuff, kWBUFF_len, "#define %s_DLC (%uU)", m.Name.c_str(), m.DLC);
105-
fwriter->AppendLine(wbuff);
106-
snprintf(wbuff, kWBUFF_len, "#define %s_CANID (%#x)", m.Name.c_str(), m.MsgID);
107-
fwriter->AppendLine(wbuff);
109+
fwriter->AppendLine(PrintF("// def @%s CAN Message (%-4d %#x)", m.Name.c_str(), m.MsgID, m.MsgID));
110+
fwriter->AppendLine(PrintF("#define %s_IDE (%uU)", m.Name.c_str(), m.IsExt));
111+
fwriter->AppendLine(PrintF("#define %s_DLC (%uU)", m.Name.c_str(), m.DLC));
112+
fwriter->AppendLine(PrintF("#define %s_CANID (%#x)", m.Name.c_str(), m.MsgID));
108113

109114
if (m.Cycle > 0)
110115
{
111-
snprintf(wbuff, kWBUFF_len, "#define %s_CYC (%dU)", m.Name.c_str(), m.Cycle);
112-
fwriter->AppendLine(wbuff);
116+
fwriter->AppendLine(PrintF("#define %s_CYC (%dU)", m.Name.c_str(), m.Cycle));
113117
}
114118

115119
if (m.CommentText.size() > 0)
@@ -137,14 +141,12 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
137141
// empty line before struct definition
138142
fwriter->AppendLine("\n");
139143

140-
snprintf(wbuff, kWBUFF_len, "typedef struct");
141-
fwriter->AppendLine(wbuff);
144+
fwriter->AppendLine(PrintF("typedef struct"));
142145

143146
fwriter->AppendLine("{\n");
144147

145148
// Write section for bitfielded part
146-
snprintf(wbuff, kWBUFF_len, "#ifdef %s", usebits_str.c_str());
147-
fwriter->AppendLine(wbuff, 2);
149+
fwriter->AppendLine(PrintF("#ifdef %s", usebits_str.c_str()), 2);
148150

149151
for (size_t signum = 0; signum < m.Signals.size(); signum++)
150152
{
@@ -163,20 +165,13 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
163165
WriteSigStructField(sig, false, max_sig_name_len);
164166
}
165167

166-
snprintf(wbuff, kWBUFF_len, "#endif // %s", usebits_str.c_str());
167-
fwriter->AppendLine(wbuff, 2);
168+
fwriter->AppendLine(PrintF("#endif // %s", usebits_str.c_str()), 2);
168169

169170
// start mon1 section
170-
snprintf(wbuff, kWBUFF_len, "#ifdef %s", usebits_str.c_str());
171-
fwriter->AppendLine(wbuff, 2);
172-
171+
fwriter->AppendLine(PrintF("#ifdef %s", usebits_str.c_str()), 2);
173172
fwriter->AppendLine(" FrameMonitor_t mon1;", 2);
174-
175-
snprintf(wbuff, kWBUFF_len, "#endif // %s", usebits_str.c_str());
176-
fwriter->AppendLine(wbuff, 2);
177-
178-
snprintf(wbuff, kWBUFF_len, "} %s_t;", m.Name.c_str());
179-
fwriter->AppendLine(wbuff, 2);
173+
fwriter->AppendLine(PrintF("#endif // %s", usebits_str.c_str()), 2);
174+
fwriter->AppendLine(PrintF("} %s_t;", m.Name.c_str()), 2);
180175
}
181176

182177
fwriter->AppendLine("// Function signatures", 2);
@@ -185,27 +180,24 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
185180
{
186181
// write message typedef s and additional expressions
187182
MessageDescriptor_t& m = sigprt->sigs_expr[num]->msg;
188-
189-
snprintf(wbuff, kWBUFF_len, "uint32_t Unpack_%s_%s(%s_t* _m, const uint8_t* _d, uint8_t dlc_);",
190-
m.Name.c_str(), drvname.c_str(), m.Name.c_str());
191-
192-
fwriter->AppendLine(wbuff);
193-
194-
snprintf(wbuff, kWBUFF_len, "#ifdef %s", usestruct_str.c_str());
195-
fwriter->AppendLine(wbuff);
196-
snprintf(wbuff, kWBUFF_len, "uint32_t Pack_%s_%s(const %s_t* _m, __CoderDbcCanFrame_t__* cframe);",
197-
m.Name.c_str(), drvname.c_str(), m.Name.c_str());
198-
fwriter->AppendLine(wbuff);
199-
183+
184+
fwriter->AppendLine(
185+
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()));
187+
188+
fwriter->AppendLine(PrintF("#ifdef %s", usestruct_str.c_str()));
189+
190+
fwriter->AppendLine(
191+
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+
200194
fwriter->AppendLine("#else");
195+
196+
fwriter->AppendLine(
197+
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()));
201199

202-
snprintf(wbuff, kWBUFF_len,
203-
"uint32_t Pack_%s_%s(const %s_t* _m, uint8_t* _d, uint8_t* _len, uint8_t* _ide);",
204-
m.Name.c_str(), drvname.c_str(), m.Name.c_str());
205-
fwriter->AppendLine(wbuff);
206-
207-
snprintf(wbuff, kWBUFF_len, "#endif // %s", usestruct_str.c_str());
208-
fwriter->AppendLine(wbuff, 2);
200+
fwriter->AppendLine(PrintF("#endif // %s", usestruct_str.c_str()), 2);
209201
}
210202

211203
fwriter->AppendLine("#ifdef __cplusplus\n}\n#endif");
@@ -320,5 +312,5 @@ void CiMainGenerator::SetCommonValues(const std::string& drvname)
320312
usediag_str = wbuff;
321313

322314
snprintf(wbuff, kWBUFF_len, "%s_USE_CANSTRUCT", DRVNAME.c_str());
323-
canframe_str = wbuff;
315+
usestruct_str = wbuff;
324316
}

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