From 16827df7bc4a82d74f89dc81631bd2a7d05f3121 Mon Sep 17 00:00:00 2001 From: sje Date: Fri, 29 Jun 2007 06:41:47 +0000 Subject: can add and remove contacts git-svn-id: https://server.scottellis.com.au/svn/mim_plugs@220 4f64403b-2f21-0410-a795-97e2b3489a10 --- MySpace/NetMessage.cpp | 139 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 123 insertions(+), 16 deletions(-) (limited to 'MySpace/NetMessage.cpp') diff --git a/MySpace/NetMessage.cpp b/MySpace/NetMessage.cpp index 023a459..45b9159 100644 --- a/MySpace/NetMessage.cpp +++ b/MySpace/NetMessage.cpp @@ -39,6 +39,102 @@ NMString::~NMString() { delete[] text; } +Dictionary::Dictionary(): LinkedList< KeyValue >() { +} + +Dictionary::~Dictionary() { +} + +int Dictionary::parse(char *data, int size) { + int pos = 0; + char *key, *value; + int start, end = 0; + while(end < size) { + start = end; + end = start; + while(end < size && data[end] != '=') end++; + if(end < size) { + data[end] = 0; + key = &data[start]; + + start = end + 1; + end = start; + while(end < size && data[end] != '\x1c' && data[end] != 0) end++; + if(end <= size) { + data[end] = 0; + value = &data[start]; + end++; + } else + value = 0; + } else { + key = value = 0; + } + + if(key && value) { + KeyValue kv = KeyValue(NMString(key), NMString(value)); + add(kv); + } + } + return pos; +} + +bool Dictionary::get_string(char *key, char *buff, int buffsize) { + NMString strkey(key); + for(Iterator i = start(); i.has_val(); i.next()) { + KeyValue kv = i.val(); + if(kv.first == strkey) { + mir_snprintf(buff, buffsize, "%s", kv.second.text); + return true; + } + } + return false; +} + +int Dictionary::get_int(char *key) { + NMString strkey(key); + for(Iterator i = start(); i.has_val(); i.next()) { + KeyValue kv = i.val(); + if(kv.first == strkey) { + return atoi(kv.second.text); + } + } + return 0; +} + +void Dictionary::add_string(char *key, char *buff) { + KeyValue dat = KeyValue(NMString(key), NMString(buff)); + add(dat); +} + +void Dictionary::add_int(char *key, int i) { + char buff[254]; + KeyValue dat(NMString(key), NMString(_itoa(i, buff, 10))); + add(dat); +} + +int Dictionary::make_body(char *buff, int size) { + int pos = 0, key_len, val_len, len; + bool first = true; + for(Iterator i = start(); i.has_val() && pos < size; i.next()) { + KeyValue v = i.val(); + + key_len = strlen(v.first.text); + val_len = strlen(v.second.text); + len = key_len + val_len + 2; + + if(size - pos - len > 0) { + if(first) { + mir_snprintf(buff + pos, size - pos, "%s=%s", v.first.text, v.second.text); + first = false; + } else + mir_snprintf(buff + pos, size - pos, "\x1c%s=%s", v.first.text, v.second.text); + } + pos += len; + } + + return pos; +} + NetMessage::NetMessage(): Map() { } @@ -108,6 +204,14 @@ bool NetMessage::get_string(char *key, char *buff, int buffsize) { return false; } +int NetMessage::get_int(char *key) { + NMString val; + if(get(NMString(key), val)) { + return atoi(val.text); + } + return 0; +} + bool NetMessage::get_data(char *key, char *buff, int *size) { NMString val; if(get(NMString(key), val)) { @@ -126,25 +230,20 @@ bool NetMessage::get_data(char *key, char *buff, int *size) { return false; } -int NetMessage::get_int(char *key) { - NMString val; - if(get(NMString(key), val)) { - return atoi(val.text); +Dictionary NetMessage::get_dict(char *key) { + Dictionary d; + char t[4096]; + if(get_string(key, t, 4096)) { + d.parse(t, strlen(t)); } - return 0; + return d; } -ClientNetMessage::ClientNetMessage(): LinkedList< KeyValue >() { -} -ClientNetMessage::~ClientNetMessage() { +ClientNetMessage::ClientNetMessage(): Dictionary() { } -void ClientNetMessage::add_string(char *key, char *buff) { - char *temp = escape(buff); - KeyValue dat = KeyValue(NMString(key), NMString(temp)); - delete temp; - add(dat); +ClientNetMessage::~ClientNetMessage() { } bool ClientNetMessage::add_data(char *key, char *data, int size) { @@ -169,12 +268,20 @@ bool ClientNetMessage::add_data(char *key, char *data, int size) { return false; } -void ClientNetMessage::add_int(char *key, int i) { - char buff[254]; - KeyValue dat(NMString(key), NMString(_itoa(i, buff, 10))); +void ClientNetMessage::add_string(char *key, char *buff) { + char *temp = escape(buff); + KeyValue dat = KeyValue(NMString(key), NMString(temp)); + delete temp; add(dat); } +void ClientNetMessage::add_dict(char *key, Dictionary &d) { + char t[4096]; + if(d.make_body(t, 4096)) { + add_string(key, t); + } +} + //static char *ClientNetMessage::escape(char *val) { int len = strlen(val); -- cgit v1.2.3