mirror of
https://github.com/moparisthebest/curl
synced 2024-12-21 23:58:49 -05: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:
parent
3e4cd0b422
commit
7f679c3da3
@ -1,8 +1,8 @@
|
|||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* _ _ ____ _
|
* _ _ ____ _
|
||||||
* Project ___| | | | _ \| |
|
* Project ___| | | | _ \| |
|
||||||
* / __| | | | |_) | |
|
* / __| | | | |_) | |
|
||||||
* | (__| |_| | _ <| |___
|
* | (__| |_| | _ <| |___
|
||||||
* \___|\___/|_| \_\_____|
|
* \___|\___/|_| \_\_____|
|
||||||
*
|
*
|
||||||
* $Id$
|
* $Id$
|
||||||
@ -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);
|
||||||
@ -45,25 +48,27 @@ void *curl_thread(void *ptr)
|
|||||||
CURLcode res;
|
CURLcode res;
|
||||||
FILE *outfile;
|
FILE *outfile;
|
||||||
gchar *url = ptr;
|
gchar *url = 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);
|
||||||
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
|
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
|
||||||
|
|
||||||
res = curl_easy_perform(curl);
|
res = curl_easy_perform(curl);
|
||||||
|
|
||||||
fclose(outfile);
|
fclose(outfile);
|
||||||
/* always cleanup */
|
/* always cleanup */
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,11 +76,10 @@ 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);
|
||||||
|
|
||||||
gtk_init(&argc, &argv);
|
gtk_init(&argc, &argv);
|
||||||
Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
Frame = gtk_frame_new(NULL);
|
Frame = gtk_frame_new(NULL);
|
||||||
@ -90,8 +94,10 @@ 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();
|
||||||
gdk_threads_leave();
|
gdk_threads_leave();
|
||||||
|
Loading…
Reference in New Issue
Block a user