source: subversion/applications/lib/libosm/Client.cpp @ 34393

Last change on this file since 34393 was 11750, checked in by nienhueser, 11 years ago

Less accessors alter instance state, add const to reflect this.

File size: 4.5 KB
Line 
1#include "Client.h"
2
3#include <cstdio>
4#include <cstdlib>
5#include <cstring>
6
7
8/*
9Copyright (C) 2005 Nick Whitelegg, Hogweed Software, nick@hogweed.org
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation; either version 2 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
24
25*/
26
27
28/* CREDIT: The approach taken (of a struct storing the read-in-data and its
29* size) is inspired by the callback example on the cURL web site. */
30
31namespace OSM
32{
33
34typedef struct
35{
36        char *data;
37        int nbytes;
38} Data;
39
40Client::Client(const std::string& urlbase)
41{
42        this->urlbase=urlbase;
43        username="";
44        password="";
45}
46
47void Client::setLoginDetails(const std::string& u, const std::string& p)
48{
49        username=u;
50        password=p;
51}
52
53std::string  Client::grabOSM(const char *apicall,
54                                        double west,double south,double east,double north)
55{
56        char url[1024];
57        sprintf(url,"%s/%s?bbox=%lf,%lf,%lf,%lf",
58                                        urlbase.c_str(),apicall,west,south,east,north);
59        return grab(url);
60}
61
62std::string  Client::grabOSM(const char *apicall)
63{
64        char url[1024];
65        sprintf(url,"%s/%s", urlbase.c_str(),apicall);
66        return grab(url);
67}
68
69std::string  Client::grab(const char *url)
70{
71        std::string data;
72
73        fprintf(stderr, "grab(): URL=%s\n",url);
74        CURL *curl =  curl_easy_init();
75
76
77        if(curl)
78        {
79                data = doGrab(curl,url);
80                curl_easy_cleanup(curl);
81                return data;
82        }
83        return "";
84}
85
86std::string  Client::doGrab(CURL *curl,const char *url)
87{
88        char uname_pwd[1024];
89        CURLcode res;
90        std::string returned="";
91
92        if(username!="" && password!="")
93        {
94                Data *data = (Data *)malloc(sizeof(Data));
95                data->data = NULL;
96                data->nbytes = 0;
97
98                sprintf(uname_pwd,"%s:%s",username.c_str(),password.c_str());
99                curl_easy_setopt(curl,CURLOPT_USERPWD,uname_pwd);
100
101                curl_easy_setopt(curl,CURLOPT_URL,url);
102                curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
103                curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,Client::responseCallback);
104                curl_easy_setopt(curl,CURLOPT_WRITEDATA,data);
105
106                res=curl_easy_perform(curl);
107
108                fprintf(stderr,"Got data.\n");
109
110                data->data=(char *)realloc(data->data,(data->nbytes+1)*sizeof(char));
111                data->data[data->nbytes] = '\0';
112                returned = data->data;
113                free(data->data);
114                free(data);
115        }
116
117        return returned;
118}
119
120size_t Client::responseCallback(void *ptr,size_t size,size_t nmemb,void *d)
121{
122        size_t rsize=size*nmemb;
123        Data *data=(Data *)d;
124        data->data=(char *)realloc(data->data,(data->nbytes+rsize)
125                                *sizeof(char));
126        memcpy(&(data->data[data->nbytes]),ptr,rsize);
127        data->nbytes += rsize;
128        //      fprintf(stderr,"data->nbytes is %d\n", data->nbytes);
129        return rsize;
130}
131
132size_t Client::putCallback
133                (void *bufptr,size_t size, size_t nitems, void *userp)
134{
135        char *p1= (char*)bufptr, *p2=(char*)userp;
136        //      fprintf(stderr,"callback: userp=%s\n",(char *)userp);
137        strcpy(p1,p2);
138        return strlen(p1)+1;
139}
140
141std::string Client::putToOSM(char* apicall, char* idata)
142{
143        CURL *curl;
144
145        char uname_pwd[1024];
146        char url[1024];
147        std::string returned="";
148        if(username!="" && password!="")
149        {
150                sprintf(uname_pwd,"%s:%s",username.c_str(),password.c_str());
151                sprintf(url,"%s/%s",urlbase.c_str(),apicall);
152
153                Data *odata = (Data *)malloc(sizeof(Data));
154                odata->data = NULL;
155                odata->nbytes = 0;
156
157                curl_global_init(CURL_GLOBAL_ALL);
158                curl = curl_easy_init();
159
160                if(curl)
161                {
162                        curl_easy_setopt(curl,CURLOPT_READFUNCTION,Client::putCallback);
163                        curl_easy_setopt(curl,CURLOPT_UPLOAD, true);
164                        curl_easy_setopt(curl,CURLOPT_PUT, true);
165                        curl_easy_setopt(curl,CURLOPT_URL, url);
166                        curl_easy_setopt(curl,CURLOPT_USERPWD,uname_pwd);
167                        curl_easy_setopt(curl,CURLOPT_READDATA,idata);
168                        curl_easy_setopt(curl,CURLOPT_INFILESIZE,(long)(strlen(idata)+1));
169                        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,Client::putCallback);
170                        curl_easy_setopt(curl,CURLOPT_WRITEDATA,odata);
171                        curl_easy_perform(curl);
172                        curl_easy_cleanup(curl);
173                        odata->data=(char *)realloc
174                                (odata->data,(odata->nbytes+1)*sizeof(char));
175                        odata->data[odata->nbytes] = '\0';
176                        returned = odata->data;
177                        free(odata->data);
178                }
179
180                curl_global_cleanup();
181                free(odata);
182        }
183        return returned;
184}
185
186}
Note: See TracBrowser for help on using the repository browser.