1
0
mirror of https://github.com/moparisthebest/curl synced 2024-08-13 17:03:50 -04:00

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.
This commit is contained in:
Daniel Stenberg 2004-02-09 07:12:33 +00:00
parent 3e4cd0b422
commit 7f679c3da3

View File

@ -17,10 +17,13 @@
#include <curl/types.h> /* new for v7 */ #include <curl/types.h> /* new for v7 */
#include <curl/easy.h> /* new for v7 */ #include <curl/easy.h> /* new for v7 */
#include <pthread.h>
GtkWidget *Bar; 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) size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{ {
return fread(ptr, size, nmemb, stream); return fread(ptr, size, nmemb, stream);
@ -49,10 +52,11 @@ void *curl_thread(void *ptr)
curl = curl_easy_init(); curl = curl_easy_init();
if(curl) 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_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_READFUNCTION, my_read_func);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
@ -64,6 +68,7 @@ void *curl_thread(void *ptr)
/* always cleanup */ /* always cleanup */
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
return NULL; return NULL;
} }
@ -71,7 +76,6 @@ int main(int argc, char **argv)
{ {
GtkWidget *Window, *Frame, *Frame2; GtkWidget *Window, *Frame, *Frame2;
GtkAdjustment *adj; GtkAdjustment *adj;
pthread_t curl_tid;
/* Init thread */ /* Init thread */
g_thread_init(NULL); g_thread_init(NULL);
@ -90,7 +94,9 @@ int main(int argc, char **argv)
gtk_container_add(GTK_CONTAINER(Frame2), Bar); gtk_container_add(GTK_CONTAINER(Frame2), Bar);
gtk_widget_show_all(Window); 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(); gdk_threads_enter();
gtk_main(); gtk_main();