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

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

Have g++ print warnings and fix the existing ones.

File size: 4.4 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
34Client::Client(const std::string& urlbase)
35{
36        this->urlbase=urlbase;
37        username="";
38        password="";
39}
40
41void Client::setLoginDetails(const std::string& u, const std::string& p)
42{
43        username=u;
44        password=p;
45}
46
47std::string  Client::grabOSM(const char *apicall,
48                                        double west,double south,double east,double north)
49{
50        char url[1024];
51        sprintf(url,"%s/%s?bbox=%lf,%lf,%lf,%lf",
52                                        urlbase.c_str(),apicall,west,south,east,north);
53        return grab(url);
54}
55
56std::string  Client::grabOSM(const char *apicall)
57{
58        char url[1024];
59        sprintf(url,"%s/%s", urlbase.c_str(),apicall);
60        return grab(url);
61}
62
63std::string  Client::grab(const char *url)
64{
65        std::string data;
66
67        fprintf(stderr, "grab(): URL=%s\n",url);
68        CURL *curl =  curl_easy_init();
69
70
71        if(curl)
72        {
73                data = doGrab(curl,url);
74                curl_easy_cleanup(curl);
75                return data;
76        }
77        return "";
78}
79
80std::string  Client::doGrab(CURL *curl,const char *url)
81{
82        char uname_pwd[1024];
83        CURLcode res;
84        std::string returned="";
85
86        if(username!="" && password!="")
87        {
88                Data *data = (Data *)malloc(sizeof(Data));
89                data->data = NULL;
90                data->nbytes = 0;
91
92                sprintf(uname_pwd,"%s:%s",username.c_str(),password.c_str());
93                curl_easy_setopt(curl,CURLOPT_USERPWD,uname_pwd);
94
95                curl_easy_setopt(curl,CURLOPT_URL,url);
96                curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,Client::responseCallback);
97                curl_easy_setopt(curl,CURLOPT_WRITEDATA,data);
98
99                res=curl_easy_perform(curl);
100
101                fprintf(stderr,"Got data.\n");
102
103                data->data=(char *)realloc(data->data,(data->nbytes+1)*sizeof(char));
104                data->data[data->nbytes] = '\0';
105                returned = data->data;
106                free(data->data);
107                free(data);
108        }
109
110        return returned;
111}
112
113size_t Client::responseCallback(void *ptr,size_t size,size_t nmemb,void *d)
114{
115        size_t rsize=size*nmemb;
116        Data *data=(Data *)d;
117        data->data=(char *)realloc(data->data,(data->nbytes+rsize)
118                                *sizeof(char));
119        memcpy(&(data->data[data->nbytes]),ptr,rsize);
120        data->nbytes += rsize;
121        //      fprintf(stderr,"data->nbytes is %d\n", data->nbytes);
122        return rsize;
123}
124
125size_t Client::putCallback
126                (void *bufptr,size_t size, size_t nitems, void *userp)
127{
128        char *p1= (char*)bufptr, *p2=(char*)userp;
129        //      fprintf(stderr,"callback: userp=%s\n",(char *)userp);
130        strcpy(p1,p2);
131        return strlen(p1)+1;
132}
133
134std::string Client::putToOSM(char* apicall, char* idata)
135{
136        CURL *curl;
137
138        char uname_pwd[1024];
139        char url[1024];
140        std::string returned="";
141        if(username!="" && password!="")
142        {
143                sprintf(uname_pwd,"%s:%s",username.c_str(),password.c_str());
144                sprintf(url,"%s/%s",urlbase.c_str(),apicall);
145
146                Data *odata = (Data *)malloc(sizeof(Data));
147                odata->data = NULL;
148                odata->nbytes = 0;
149
150                curl_global_init(CURL_GLOBAL_ALL);
151                curl = curl_easy_init();
152
153                if(curl)
154                {
155                        curl_easy_setopt(curl,CURLOPT_READFUNCTION,Client::putCallback);
156                        curl_easy_setopt(curl,CURLOPT_UPLOAD, true);
157                        curl_easy_setopt(curl,CURLOPT_PUT, true);
158                        curl_easy_setopt(curl,CURLOPT_URL, url);
159                        curl_easy_setopt(curl,CURLOPT_USERPWD,uname_pwd);
160                        curl_easy_setopt(curl,CURLOPT_READDATA,idata);
161                        curl_easy_setopt(curl,CURLOPT_INFILESIZE,(long)(strlen(idata)+1));
162                        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,Client::putCallback);
163                        curl_easy_setopt(curl,CURLOPT_WRITEDATA,odata);
164                        curl_easy_perform(curl);
165                        curl_easy_cleanup(curl);
166                        odata->data=(char *)realloc
167                                (odata->data,(odata->nbytes+1)*sizeof(char));
168                        odata->data[odata->nbytes] = '\0';
169                        returned = odata->data;
170                        free(odata->data);
171                }
172
173                curl_global_cleanup();
174                free(odata);
175        }
176        return returned;
177}
178
179}
Note: See TracBrowser for help on using the repository browser.