On Thu, Apr 18, 2013 at 7:27 PM, Mike Small <sma...@panix.com> wrote: > Ben Tilly <bti...@gmail.com> writes: > >> I've just come up with a better answer to this question. If I was a >> C/C++ programmer this would have been obvious to me before, and it >> solves a bunch of annoyances for me at once. >> >> #ifndef UTIL_H >> #define UTIL_H >> >> #include <string> >> #include <stdio.h> >> #include <stdarg.h> >> >> std::string stringf(const char * format_str, ...) { >> va_list arg_list; >> char * c_string; >> va_start(arg_list, format_str); >> vasprintf(&c_string, format_str, arg_list); >> std::string answer = std::string(c_string); >> free(c_string); >> va_end(arg_list); >> return answer; >> } >> >> namespace { >> int tests = 0; >> >> int next_test () { >> return ++tests; >> } >> >> int current_test () { >> return tests; >> } >> } >> >> void ok (bool is_ok, const char * description, ...) { >> std::string status >> = stringf("%s %d", is_ok ? "ok" : "not ok", next_test()); > > #include <sstream> > ... > > void ok (bool is_ok, const char * description, ...) { > ostringstream oss_status; > oss_status << (is_ok ? "ok " : "not ok ") << next_test(); > > va_list arg_list; > char * c_string; > va_start(arg_list, description); > vasprintf(&c_string, description, arg_list); > va_end(arg_list); > printf("%s: %s\n", oss_status.str().c_str(), c_string); > free(c_string); > }
I am deliberately trying to follow something fairly close to the Google C++ style. Hence not using streams for this was entirely intentional. _______________________________________________ Boston-pm mailing list Boston-pm@mail.pm.org http://mail.pm.org/mailman/listinfo/boston-pm