source: subversion/applications/utils/tirex/backend-mapnik/networkmessage.cc @ 29342

Last change on this file since 29342 was 29247, checked in by Dane Springmeyer, 7 years ago

malloc.h is not available on osx and should not be needed to be explicitly included on other platforms

File size: 1.9 KB
Line 
1/*
2 * Tirex Tile Rendering System
3 *
4 * Mapnik rendering backend
5 *
6 * Originally written by Jochen Topf & Frederik Ramm.
7 *
8 */
9
10#include <string.h>
11#include <stdlib.h>
12
13#include "networkmessage.h"
14
15NetworkMessage::NetworkMessage()
16{
17}
18
19NetworkMessage::~NetworkMessage()
20{
21}
22
23const std::string NetworkMessage::getParam(const std::string &key, const std::string &def) const
24{
25    std::map<std::string, std::string>::const_iterator i = mParams.find(key);
26    return (i == mParams.end() ? def : i->second);
27}
28
29int NetworkMessage::getParam(const std::string &key, int def) const
30{
31    std::map<std::string, std::string>::const_iterator i = mParams.find(key);
32    return (i == mParams.end() ? def : atoi(i->second.c_str()));
33}
34
35void NetworkMessage::setParam(const std::string &key, const std::string &value) 
36{
37    mParams[key] = value;
38}
39void NetworkMessage::setParam(const std::string &key, int value)
40{
41    char buffer[32];
42    snprintf(buffer, 31, "%d", value);
43    buffer[31]=0;
44    mParams[key] = buffer;
45}
46
47bool NetworkMessage::parse(const std::string &buffer)
48{
49    debug(">> NetworkMessage::parse");
50    char *dup = strdup(buffer.c_str());
51    char *token;
52    token = strtok(dup, "\r\n");
53    while (token)
54    {
55        char *eq = strchr(token, '=');
56        if (eq)
57        {
58            *eq++ = 0;
59            mParams[token] = eq;
60        }
61        else
62        {
63            // invalid line
64        }
65        token = strtok(NULL, "\r\n");
66    }
67    free(dup);
68    debug("<< NetworkMessage::parse");
69    return true;
70}
71
72bool NetworkMessage::build(std::string &buffer) const
73{
74    debug(">> NetworkMessage::build");
75    buffer.clear();
76    for (std::map<std::string, std::string>::const_iterator i = 
77        mParams.begin(); i != mParams.end(); i++)
78    {
79        buffer.append(i->first);
80        buffer.append("=");
81        buffer.append(i->second);
82        buffer.append("\n");
83    }
84    debug("<< NetworkMessage::build");
85    return true;
86}
Note: See TracBrowser for help on using the repository browser.