summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2012-03-30 11:13:15 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2012-03-30 11:13:15 +0200
commitf3403c20af7c36ce9ccd157f66187525917eeb7a (patch)
tree3bb0e6094008027c6bf0870c50d60e84a687d518
parent395fc60517f70f6ae087e6919b1463f35bb54db8 (diff)
parentf90671bfc16e1fdc31c6a98a1bd03d8cb762841b (diff)
Merge branch 'master' of https://git.oftal.dk/munia
-rw-r--r--src/msgparser.cc173
-rw-r--r--src/msgparser.h17
2 files changed, 43 insertions, 147 deletions
diff --git a/src/msgparser.cc b/src/msgparser.cc
index c5d5ab3..86583e0 100644
--- a/src/msgparser.cc
+++ b/src/msgparser.cc
@@ -26,67 +26,18 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "msgparser.h"
+
#include <stdlib.h>
#include <stdio.h>
-
#include <list>
#include <vector>
-/*
-inline void parse_add(msg_t& m, std::string data) {
- int offset = 0;
- std::string title = data.substr(offset, data.find(' ', offset) - offset);
- offset += title.length() + 1;
- std::string desc = data.substr(offset, data.find(' ', offset) - offset);
- offset += desc.length() + 1;
- std::string x_str = data.substr(offset, data.find(' ', offset) - offset);
- int x = atoi(x_str.c_str());
- offset += x_str.length() + 1;
- std::string y_str = data.substr(offset, data.find(' ', offset) - offset);
- int y = atoi(y_str.c_str());
-
- sprintf(m.add.title, "%s", desc.c_str());
- sprintf(m.add.desc, "%s", desc.c_str());
- m.add.x = x;
- m.add.y = y;
-}
-
-inline void parse_del(msg_t& m, std::string data) {
- int offset = 0;
- std::string id_str = data.substr(offset, data.find(' ', offset) - offset);
- int id = atoi(id_str.c_str());
-
- m.del.id = id;
-}
+typedef std::vector<std::string> TokenVector;
+typedef std::list<TokenVector> MsgTokensList;
-inline void parse_move(msg_t& m, std::string data) {
- int offset = 1;
- std::string s_id = data.substr(offset, data.find(' ', offset) - offset);
- int id = atoi(s_id.c_str());
- offset += s_id.length() + 1;
- std::string x_str = data.substr(offset, data.find(' ', offset) - offset);
- int x = atoi(x_str.c_str());
- offset += x_str.length() + 1;
- std::string y_str = data.substr(offset, data.find(' ', offset) - offset);
- int y = atoi(y_str.c_str());
-
- m.move.id = id;
- m.move.x = x;
- m.move.y = y;
-}
-*/
-
-MsgVector parse_msg(std::string data) {
-
- printf("Parsing: %s\n", data.c_str());
-
- typedef std::vector<std::string> TokenVector;
-
- // this name is pretty stupid as we already use the name MsgVector for something completely else
- typedef std::list<TokenVector> MsgList;
-
- MsgList msglist;
- TokenVector tokens;
+inline static void parse_into_msg_tokens(std::string& data,
+ MsgTokensList& msgTokensList) {
+ TokenVector tokenVector;
std::string token;
bool inside_quote = false;
char prev_ch = '0';
@@ -110,7 +61,7 @@ MsgVector parse_msg(std::string data) {
}
if(token.empty()) continue; // skip multiple white spaces and pre white space
// printf("Adding token %s\n", token.c_str());
- tokens.push_back(token);
+ tokenVector.push_back(token);
token.clear();
break;
case ';':
@@ -121,10 +72,10 @@ MsgVector parse_msg(std::string data) {
}
// printf("Adding msg...\n");
if(!token.empty()) {
- tokens.push_back(token);
+ tokenVector.push_back(token);
}
- msglist.push_back(tokens);
- tokens.clear();
+ msgTokensList.push_back(tokenVector);
+ tokenVector.clear();
token.clear();
break;
default:
@@ -136,20 +87,19 @@ MsgVector parse_msg(std::string data) {
}
if(!token.empty()) {
- tokens.push_back(token);
+ tokenVector.push_back(token);
token.clear();
}
- if(!tokens.empty()) {
- msglist.push_back(tokens);
- tokens.clear();
+ if(!tokenVector.empty()) {
+ msgTokensList.push_back(tokenVector);
+ tokenVector.clear();
}
+}
-// printf("Number of messages: %d\n", msglist.size());
-
- MsgVector v;
- MsgList::iterator it_msg;
- for(it_msg = msglist.begin(); it_msg != msglist.end(); it_msg++) {
+inline static void create_msg_list(MsgTokensList& msgTokensList, MsgList& msgList) {
+ MsgTokensList::iterator it_msg;
+ for(it_msg = msgTokensList.begin(); it_msg != msgTokensList.end(); it_msg++) {
TokenVector t = *it_msg;
//malformed msg
@@ -173,10 +123,8 @@ MsgVector parse_msg(std::string data) {
}
sprintf(m.add.title, "%s", t[1].c_str());
sprintf(m.add.desc, "%s", t[2].c_str());
- m.add.parent_id = atoi(t[3].c_str());
-// m.add.x = atoi(t[3].c_str());
-// m.add.y = atoi(t[4].c_str());
- printf("addcmd: %s %s %d\n", m.add.title, m.add.desc, m.add.parent_id);
+ m.add.parentid = atoi(t[3].c_str());
+ printf("addcmd: %s %s %d\n", m.add.title, m.add.desc, m.add.parentid);
break;
}
case cmd::del: {
@@ -193,9 +141,7 @@ MsgVector parse_msg(std::string data) {
continue;
}
m.move.id = atoi(t[1].c_str());
- m.move.parent_id = atoi(t[2].c_str());
-// m.move.x = atoi(t[2].c_str());
-// m.move.y = atoi(t[3].c_str());
+ m.move.parentid = atoi(t[2].c_str());
break;
}
case cmd::update: {
@@ -203,89 +149,46 @@ MsgVector parse_msg(std::string data) {
printf("Wrong number of parameters\n");
continue;
}
- m.update.id = atoi(t[1].c_str());
- sprintf(m.update.title, "%s", t[2].c_str());
- sprintf(m.update.desc, "%s", t[3].c_str());
+ m.update.id = atoi(t[1].c_str());
+ sprintf(m.update.title, "%s", t[2].c_str());
+ sprintf(m.update.desc, "%s", t[3].c_str());
break;
}
default:
break;
};
-
- v.push_back(m);
+ msgList.push_back(m);
}
-
- return v;
}
-msg_t create_msg(cmd::cmd_t type, task_t t) {
- msg_t m;
- m.cmd = type;
- switch(type) {
- case cmd::add: {
- m.add.id = t.id;
- m.add.parent_id = t.parent_id;
-// m.add.x = t.x;
-// m.add.y = t.y;
-// m.add.title = t.title.c_str();
-// m.add.desc = t.desc.c_str();
- snprintf(m.add.title, sizeof(m.add.title), "%s", t.title.c_str());
- snprintf(m.add.desc, sizeof(m.add.desc), "%s", t.desc.c_str());
- break;
- }
- case cmd::del: {
- m.del.id = t.id;
- break;
- }
- case cmd::move: {
- m.move.id = t.id;
-// m.move.x = t.x;
-// m.move.y = t.y;
- m.move.parent_id = t.parent_id;
- break;
- }
- case cmd::update: {
- m.update.id = t.id;
-// m.update.title = t.title;
-// m.update.desc = t.desc;
- snprintf(m.update.title, sizeof(m.update.title), "%s", t.title.c_str());
- snprintf(m.update.desc, sizeof(m.update.desc), "%s", t.desc.c_str());
-// printf("msg: %d, %s, %s\n", m.update.id, m.update.title, m.update.desc);
- break;
- };
- default:
- break;
- }
- return m;
+MsgList parse_msg(std::string data) {
+ printf("Parsing: %s\n", data.c_str());
+
+ MsgTokensList msgTokensList;
+ parse_into_msg_tokens(data, msgTokensList);
+
+ MsgList msgList;
+ create_msg_list(msgTokensList, msgList);
+
+ return msgList;
}
-//#define BUF_SIZE 4096
std::string msg_tostring(msg_t m) {
-// 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;",
-// asprintf(&buf, "add %d \"%s\" \"%s\" %d %d;",
-// m.add.id,
-// m.add.title, m.add.desc,
-// m.add.x, m.add.y);
asprintf(&buf, "add %d \"%s\" \"%s\" %d;",
m.add.id,
m.add.title, m.add.desc,
- m.add.parent_id);
+ m.add.parentid);
break;
}
case cmd::del: {
-// 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);
- asprintf(&buf, "move %d %d;", m.move.id, m.move.parent_id);
+ asprintf(&buf, "move %d %d;", m.move.id, m.move.parentid);
break;
}
case cmd::update: {
@@ -304,8 +207,6 @@ std::string msg_tostring(msg_t m) {
}
return r;
-
-// return buf;
}
#ifdef TEST_MSGPARSER
diff --git a/src/msgparser.h b/src/msgparser.h
index 58f68da..1559e86 100644
--- a/src/msgparser.h
+++ b/src/msgparser.h
@@ -28,11 +28,9 @@
#ifndef __MUNIA_MSGPARSER_H__
#define __MUNIA_MSGPARSER_H__
-#include <vector>
+#include <list>
#include <string>
-#include "task.h"
-
namespace cmd {
enum cmd_t {
update,
@@ -45,9 +43,7 @@ namespace cmd {
typedef struct {
int id;
- int parent_id;
-// int x;
-// int y;
+ int parentid;
char title[256];
char desc[256];
} add_t;
@@ -56,7 +52,7 @@ typedef struct {
} del_t;
typedef struct {
int id;
- int parent_id;
+ int parentid;
} move_t;
typedef struct {
int id;
@@ -77,11 +73,10 @@ typedef struct msg_t {
} msg_types;
+typedef std::list<msg_t> MsgList;
-typedef std::vector<msg_t> MsgVector;
-
-MsgVector parse_msg(std::string msg);
+MsgList parse_msg(std::string msg);
-msg_t create_msg(cmd::cmd_t msg_type, task_t task);
+//msg_t create_msg(cmd::cmd_t msg_type, task_t task);
std::string msg_tostring(msg_t msg);
#endif/*__MUNIA_MSGPARSER_H__*/