3
3
#include < iostream>
4
4
#include < fstream>
5
5
#include < cstdlib>
6
+ #include < stdarg.h>
6
7
#include < filesystem>
7
8
#include < algorithm>
8
9
#include < regex>
@@ -13,7 +14,7 @@ static const size_t kMaxDirNum = 1000;
13
14
14
15
static const size_t kWBUFF_len = 2048 ;
15
16
16
- static char wbuff[kWBUFF_len ] = {0 };
17
+ static char wbuff[kWBUFF_len ] = { 0 };
17
18
18
19
static std::string __typeprint[] =
19
20
{
@@ -27,6 +28,15 @@ static std::string __typeprint[] =
27
28
" uint64_t"
28
29
};
29
30
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
+
30
40
std::string str_toupper (std::string s)
31
41
{
32
42
std::transform (s.begin (), s.end (), s.begin (),
@@ -89,27 +99,21 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
89
99
90
100
);
91
101
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 );
94
103
95
104
for (size_t num = 0 ; num < sigprt->sigs_expr .size (); num++)
96
105
{
97
106
// write message typedef s and additional expressions
98
107
MessageDescriptor_t& m = sigprt->sigs_expr [num]->msg ;
99
108
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 ));
108
113
109
114
if (m.Cycle > 0 )
110
115
{
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 ));
113
117
}
114
118
115
119
if (m.CommentText .size () > 0 )
@@ -137,14 +141,12 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
137
141
// empty line before struct definition
138
142
fwriter->AppendLine (" \n " );
139
143
140
- snprintf (wbuff, kWBUFF_len , " typedef struct" );
141
- fwriter->AppendLine (wbuff);
144
+ fwriter->AppendLine (PrintF (" typedef struct" ));
142
145
143
146
fwriter->AppendLine (" {\n " );
144
147
145
148
// 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 );
148
150
149
151
for (size_t signum = 0 ; signum < m.Signals .size (); signum++)
150
152
{
@@ -163,20 +165,13 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
163
165
WriteSigStructField (sig, false , max_sig_name_len);
164
166
}
165
167
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 );
168
169
169
170
// 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 );
173
172
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 );
180
175
}
181
176
182
177
fwriter->AppendLine (" // Function signatures" , 2 );
@@ -185,27 +180,24 @@ void CiMainGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs,
185
180
{
186
181
// write message typedef s and additional expressions
187
182
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
+
200
194
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 ()));
201
199
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 );
209
201
}
210
202
211
203
fwriter->AppendLine (" #ifdef __cplusplus\n }\n #endif" );
@@ -320,5 +312,5 @@ void CiMainGenerator::SetCommonValues(const std::string& drvname)
320
312
usediag_str = wbuff;
321
313
322
314
snprintf (wbuff, kWBUFF_len , " %s_USE_CANSTRUCT" , DRVNAME.c_str ());
323
- canframe_str = wbuff;
315
+ usestruct_str = wbuff;
324
316
}
0 commit comments