@@ -45,71 +45,63 @@ class ValidateSchema : public Operator {
4545
4646
4747 static void error_load (void *ctx, const char *msg, ...) {
48- std::string *t = reinterpret_cast <std::string *>(ctx);
49- char buf[1024 ];
5048 va_list args;
51-
5249 va_start (args, msg);
53- int len = vsnprintf (buf, sizeof (buf) , msg, args);
50+ callback_func (ctx, append_msg, PREFIX_ERROR , msg, args);
5451 va_end (args);
55-
56- if (len > 0 ) {
57- t->append (" XML Error: " + std::string (buf));
58- }
5952 }
6053
6154
6255 static void warn_load (void *ctx, const char *msg, ...) {
63- std::string *t = reinterpret_cast <std::string *>(ctx);
64- char buf[1024 ];
6556 va_list args;
66-
6757 va_start (args, msg);
68- int len = vsnprintf (buf, sizeof (buf) , msg, args);
58+ callback_func (ctx, append_msg, PREFIX_WARNING , msg, args);
6959 va_end (args);
70-
71- if (len > 0 ) {
72- t->append (" XML Warning: " + std::string (buf));
73- }
7460 }
7561
7662
7763 static void error_runtime (void *ctx, const char *msg, ...) {
78- const Transaction *t = reinterpret_cast <Transaction *>(ctx);
79- char buf[1024 ];
80- std::string s;
8164 va_list args;
82-
8365 va_start (args, msg);
84- int len = vsnprintf (buf, sizeof (buf) , msg, args);
66+ callback_func (ctx, log_msg, PREFIX_ERROR , msg, args);
8567 va_end (args);
86-
87- if (len > 0 ) {
88- s = " XML Error: " + std::string (buf);
89- }
90- ms_dbg_a (t, 4 , s);
9168 }
9269
9370
9471 static void warn_runtime (void *ctx, const char *msg, ...) {
95- const Transaction *t = reinterpret_cast <Transaction *>(ctx);
96- char buf[1024 ];
97- std::string s;
9872 va_list args;
99-
10073 va_start (args, msg);
101- int len = vsnprintf (buf, sizeof (buf) , msg, args);
74+ callback_func (ctx, log_msg, PREFIX_WARNING , msg, args);
10275 va_end (args);
76+ }
77+
78+ static void null_error (void *, const char *, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
79+ }
80+
81+ template <typename Pred>
82+ static void callback_func (void *ctx, Pred pred, const char *base_msg, const char *msg, va_list args) {
83+ char buf[1024 ];
84+ const auto len = vsnprintf (buf, sizeof (buf), msg, args);
10385
10486 if (len > 0 ) {
105- s = " XML Warning: " + std::string (buf);
87+ const auto msg = base_msg + std::string (buf);
88+ pred (ctx, msg);
10689 }
107- ms_dbg_a (t, 4 , s);
10890 }
10991
110- static void null_error (void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
92+ static void log_msg (void *ctx, const std::string &msg) {
93+ auto t = reinterpret_cast <const Transaction *>(ctx);
94+ ms_dbg_a (t, 4 , msg);
11195 }
11296
97+ static void append_msg (void *ctx, const std::string &msg) {
98+ auto s = reinterpret_cast <std::string*>(ctx);
99+ s->append (msg);
100+ }
101+
102+ static constexpr auto PREFIX_WARNING = " XML Warning: " ;
103+ static constexpr auto PREFIX_ERROR = " XML Error: " ;
104+
113105 private:
114106 std::string m_resource;
115107 std::string m_err;
0 commit comments