From 7f679c3da3f941789a6a9654bbced6a81ddd932b Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 9 Feb 2004 07:12:33 +0000 Subject: [PATCH] Ken Rastatter's fixes to improve portability of this example: These minor changes remove portability issues with the this example and allow it to run on Win32. Specifically: * The use of pthread_create() has been replaced by g_thread_create(). This removes the dependency on the pthreads library. Since this is an example using GTK+, g_thread_create() is available as it is a part of glibc. * The CURLOPT_FILE option is now referred to by its "newer name" CURLOPT_WRITEDATA. * The use of CURLOPT_WRITEFUNCTION has been added. As described in the docs, this avoids the crashes when using a DLL under Win32. * The output file has been renamed from "/tmp/test.curl" to "test.curl". It's unlikely that there is a /tmp when in Win32 and other examples in libcurl write their output files to the working directory. --- docs/examples/curlgtk.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/docs/examples/curlgtk.c b/docs/examples/curlgtk.c index bba1fe1c9..69b016261 100644 --- a/docs/examples/curlgtk.c +++ b/docs/examples/curlgtk.c @@ -1,8 +1,8 @@ /***************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * $Id$ @@ -17,10 +17,13 @@ #include /* new for v7 */ #include /* new for v7 */ -#include - GtkWidget *Bar; +size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return fwrite(ptr, size, nmemb, stream); +} + size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) { return fread(ptr, size, nmemb, stream); @@ -45,25 +48,27 @@ void *curl_thread(void *ptr) CURLcode res; FILE *outfile; gchar *url = ptr; - + curl = curl_easy_init(); if(curl) { - outfile = fopen("/tmp/test.curl", "w"); + outfile = fopen("test.curl", "w"); curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_FILE, outfile); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func); curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func); curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar); - + res = curl_easy_perform(curl); fclose(outfile); /* always cleanup */ curl_easy_cleanup(curl); } + return NULL; } @@ -71,11 +76,10 @@ int main(int argc, char **argv) { GtkWidget *Window, *Frame, *Frame2; GtkAdjustment *adj; - pthread_t curl_tid; /* Init thread */ g_thread_init(NULL); - + gtk_init(&argc, &argv); Window = gtk_window_new(GTK_WINDOW_TOPLEVEL); Frame = gtk_frame_new(NULL); @@ -90,8 +94,10 @@ int main(int argc, char **argv) gtk_container_add(GTK_CONTAINER(Frame2), Bar); gtk_widget_show_all(Window); - pthread_create(&curl_tid, NULL, curl_thread, argv[1]); - + if (!g_thread_create(&curl_thread, argv[1], FALSE, NULL) != 0) + g_warning("can't create the thread"); + + gdk_threads_enter(); gtk_main(); gdk_threads_leave();