#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "urldecode.h"
struct MemoryStruct {
char *memory;
size_t size;
};
typedef struct wazeedata wazeedata;
struct wazeedata{
char *key;
char *payload;
wazeedata *next;
wazeedata *last;
};
void wazee_addstruct(char *data,wazeedata **wz_data){
char *key=0;
char *payload=0;
int payloaddata=0;
int i;
wazeedata *inarray;
wazeedata *nextelem;
for(i=0;data[i]!=0;i++){
if(data[i]=='='){
payloaddata=1;
} else {
if(payloaddata==0){
if(key){
key=(char*)realloc(key,strlen(key)+2);
key[strlen(key)+1]=0;
key[strlen(key)]=data[i];
} else {
key=(char*)malloc(2);
key[0]=data[i];
key[1]=0;
}
} else {
if(payload){
payload=(char*)realloc(payload,strlen(payload)+2);
payload[strlen(payload)+1]=0;
payload[strlen(payload)]=data[i];
} else {
payload=(char*)malloc(2);
payload[0]=data[i];
payload[1]=0;
}
}
}
}
urldecode(key);
urldecode(payload);
if((*wz_data)==0){
(*wz_data)=(wazeedata*)malloc(sizeof(wazeedata));
(*wz_data)->key=key;
(*wz_data)->payload=payload;
(*wz_data)->next=NULL;
(*wz_data)->last=(*wz_data);
} else {
nextelem=(wazeedata*)malloc(sizeof(wazeedata));
nextelem->key=key;
nextelem->payload=payload;
nextelem->next=NULL;
nextelem->last=nextelem;
(*wz_data)->last->next=nextelem;
(*wz_data)->last=nextelem;
}
}
void wazee_split(char *indata,char delim, wazeedata **wz_inarray){
int i;
char *data=0;
wazeedata *inarray = (*wz_inarray);
for(i=0;indata[i]!=0;i++){
if(indata[i]==delim){
wazee_addstruct(data,&inarray);
if(data){
free(data);
data=0;
}
} else {
if(data){
data=(char*)realloc(data,strlen(data)+2);
data[strlen(data)+1]=0;
data[strlen(data)]=indata[i];
} else {
data=(char*)malloc(2);
data[0]=indata[i];
data[1]=0;
}
}
}
(*wz_inarray)=inarray;
}
void *myrealloc(void *ptr, size_t size){
if(ptr)
return (void*)realloc(ptr,size);
else
return (void*)malloc(size);
}
size_t memcallback(void *ptr, size_t size, size_t nmemb, void *data){
size_t realsize = size * nmemb;
struct MemoryStruct *mem=(struct MemoryStruct *)data;
mem->memory = (char*)myrealloc(mem->memory,mem->size + realsize + 1);
if(mem->memory){
memcpy(&(mem->memory[mem->size]),ptr,realsize);
mem->size += realsize;
mem->memory[mem->size]=0;
}
// mem->memory =
return(realsize);
}
char *wazee_parse(wazeedata **indata,char *key){
wazeedata *iterator;
iterator=(*indata);
if(iterator!=0){
for(iterator=(*indata);iterator!=NULL&&strcmp(iterator->key,key)!=0;iterator=iterator->next);
if(iterator!=NULL){
return(iterator->payload);
} else {
return(NULL);
}
}
return(NULL);
}
char *wazee_filtertags(char *data){
char *returndata;
int i,x,_switch=0;
if(data!=0){
x=0;
returndata=(char*)malloc(strlen(data));
for(i=0;i<=strlen(data);i++){
if(data[i]=='<'){
_switch=1;
}
if(_switch==1&&data[i]=='>'){
_switch=0;
} else {
if(_switch==0){
returndata[x]=data[i];
x++;
}
}
}
return(returndata);
}
return(NULL);
}
size_t wazee_save_cover(void *ptr, size_t size, size_t nmemb, void *data){
size_t realsize = size * nmemb;
struct MemoryStruct *mem=(struct MemoryStruct *)data;
FILE *imagefile;
mem->memory = (char*)myrealloc(mem->memory,mem->size + realsize + 1);
if(mem->memory){
memcpy(&(mem->memory[mem->size]),ptr,realsize);
mem->size += realsize;
mem->memory[mem->size]=0;
}
imagefile=fopen("test.jpg","a+");
fwrite(ptr,size,nmemb,imagefile);
fclose(imagefile);
printf("returned %d bytes\n",realsize);
return(realsize);
}
void wazee_fetch_cover(char *url){
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = NULL;
chunk.size = 0;
printf("trying to fetch url: %s\n",url);
curl=curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,url);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, wazee_save_cover);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "wazee-xmms taskbarvoter/testing..");
curl_easy_setopt(curl, CURLOPT_WRITEDATA,(void *)&chunk);
res=curl_easy_perform(curl);
}
wazeedata *fetchdata(void){
CURL *curl;
CURLcode res;
char wazeelink[100];
struct MemoryStruct chunk;
wazeedata *returndata=0;
wazeedata *iterator=0;
chunk.memory = NULL;
chunk.size = 0;
char *data;
curl = curl_easy_init();
sprintf(wazeelink,"http://www.wazee.org/bg_transport/currSong.php?clientTime=1108897128");
if(curl){
curl_easy_setopt(curl,CURLOPT_URL,wazeelink);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memcallback);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "wazee-xmms taskbarvoter/testing..");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
res=curl_easy_perform(curl);
printf("chunk: %s\n",chunk.memory);
if(chunk.size!=0){
wazee_split(chunk.memory,';',&returndata);
}
curl_easy_cleanup(curl);
}
return(returndata);
} |