Hi
I am currently working on a program, that parses a shoutcast stream and gives back some information. i declared three global variables (2 char pointers (stationinfo and station) and one int (metaint)).
all three variables give back exactly what i want in the size_t stream_process and the size_t hdrprocess, that is the stationname, songtitle and the metaint value.
but when i try to get access to the variables in my int main the strings are corrupted and printf for example shows some weird stuff, or nothing.![]()
the integer is fine, it keeps its value.
Code:#include <stdio.h> #include <curl/curl.h> #include <string.h> #include <stdlib.h> int metaint; static char *station; static char *stationinfo; /* Function to get the song title parsed */ size_t stream_process(void *buffer, size_t size, size_t nmemb, void *userp) { int loop; char *metadata, *token; for (loop=0;loop<=nmemb*size;loop++){ metadata = ((buffer)+loop); /* the trick with counting the bytes till bytes = metaint * never really worked out....or I never really figured it out ;) * so we just search for the string 'StreamTitle' * and then we parse the Title into our info-variable * might be more cpu-intense, but it actually works! */ if (!strncmp(metadata, "StreamTitle", 11)){ char data[256]; strncpy(data, metadata+12, 256); token = strtok(data, "'"); stationinfo = (char *) token; printf("%s\n", stationinfo); } } return size*nmemb; } /* headerprocessing: */ size_t hdrprocess( void *ptr, size_t size, size_t nmemb, void *stream){ int loop=0; char *data, *token, info[100]; data = (char *) ptr; if(!strncmp(data, "icy-name:", 9)) { printf("Got Servername!\n"); token = strtok(data, " "); while((token = strtok(0, " "))){ if(loop == 0) { strcpy(info, token); loop++; } else { strcat (info, " "); strcat (info, token); } } station = (char *)info; printf("Servername: %s\n", station); } if(!strncmp(data, "icy-metaint:", 12)) { printf("Got metaint-data!\n"); token = strtok(data, " "); token = strtok(0, " "); if (token) { metaint = atoi(token); } else { metaint = 0; } printf("metaint = %i\n", metaint); } return size*nmemb; /* we must return the amount of bytes processed */ } int main (int argc, char **argv){ char *easyhandle; char *headerdata; char *URL; struct curl_slist *headerlist=NULL; if(argc<2){ printf("\n\nUSAGE:\n\n"); printf("%s [URL]\n\n", argv[0]); return 0; } URL = argv[1]; /* CURL initialization*/ curl_global_init(CURL_GLOBAL_ALL); easyhandle = curl_easy_init(); /* append 'Icy-MetaData:1' to the header, bc we want the metadata*/ headerlist = curl_slist_append(headerlist, "Icy-MetaData:1"); headerlist = curl_slist_append(headerlist, "ICY 200 OK"); curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headerlist); /* Where are we connecting to?*/ curl_easy_setopt(easyhandle, CURLOPT_URL, URL); curl_easy_setopt(easyhandle, CURLOPT_TIMEOUT, 2); curl_easy_setopt(easyhandle, CURLOPT_HTTP200ALIASES, headerlist); curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, hdrprocess); curl_easy_setopt(easyhandle, CURLOPT_WRITEHEADER, headerdata); /* Don't send data recieved to stdout --> send to processing-function instead*/ curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, stream_process); curl_easy_perform(easyhandle); /*where are the actual values of my variables? */ printf("%s\n", station); /*outputs some crap*/ printf("%s\n", stationinfo);/*here too*/ printf("%i\n", metaint); /*everything fine here*/ curl_slist_free_all(headerlist); /* free the list again */ curl_global_cleanup(); return 0; }
thx in advance
PS i know this is probably really sloppy work, but i am far from a pro and needed something that fitted my needs, but couldn't find it.
PPS i am developing under linux and cygwin



LinkBack URL
About LinkBacks



