From 0621a9d2fa2428b15d8e775c8c37b600c592cc57 Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Thu, 8 Mar 2012 08:43:26 +0100 Subject: Using asprintf instead of snprintf. --- src/msgparser.cc | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/msgparser.cc b/src/msgparser.cc index c6f83d5..d8c47f7 100644 --- a/src/msgparser.cc +++ b/src/msgparser.cc @@ -249,24 +249,29 @@ msg_t create_msg(cmd::cmd_t type, task_t t) { return m; } -#define BUF_SIZE 4096 +//#define BUF_SIZE 4096 std::string msg_tostring(msg_t m) { - char buf[4096]; +// char buf[4096]; +// buf[0] = '\0'; + char* buf = NULL; switch(m.cmd) { case cmd::add: { - printf("msg: %d, %d, %d, %s, %s\n", m.add.id, m.add.x, m.add.y, m.add.title, m.add.desc); - snprintf(buf, BUF_SIZE, "add %d %s %s %d %d;", + // printf("msg: %d, %d, %d, %s, %s\n", m.add.id, m.add.x, m.add.y, m.add.title, m.add.desc); + // snprintf(buf, BUF_SIZE, "add %d %s %s %d %d;", + asprintf(&buf, "add %d %s %s %d %d;", m.add.id, m.add.title, m.add.desc, m.add.x, m.add.y); break; } case cmd::del: { - snprintf(buf, BUF_SIZE, "del %d;", m.del.id); +// snprintf(buf, BUF_SIZE, "del %d;", m.del.id); + asprintf(&buf, "del %d;", m.del.id); break; } case cmd::move: { - snprintf(buf, BUF_SIZE, "move %d %d %d;", m.move.id, m.move.x, m.move.y); +// snprintf(buf, BUF_SIZE, "move %d %d %d;", m.move.id, m.move.x, m.move.y); + asprintf(&buf, "move %d %d %d;", m.move.id, m.move.x, m.move.y); break; } case cmd::update: { @@ -277,15 +282,15 @@ std::string msg_tostring(msg_t m) { break; } - printf("buffer: %s\n", buf); - + std::string r; if(buf) { - std::string str(buf); - return str; - } - else { - return ""; + r = buf; + free(buf); } + + return r; + +// return buf; } #ifdef TEST_MSGPARSER -- cgit v1.2.3