Changeset: f3c92bdfb810 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/f3c92bdfb810
Added Files:
clients/mapiclient/curl-stream.h
Modified Files:
clients/examples/C/CMakeLists.txt
clients/examples/C/streamcat.c
clients/mapiclient/CMakeLists.txt
Branch: ascii-flag
Log Message:
Use a single source for open_urlstream (CURL support).
diffs (182 lines):
diff --git a/clients/examples/C/CMakeLists.txt
b/clients/examples/C/CMakeLists.txt
--- a/clients/examples/C/CMakeLists.txt
+++ b/clients/examples/C/CMakeLists.txt
@@ -43,6 +43,10 @@ target_link_libraries(smack01
add_executable(streamcat
streamcat.c)
+target_include_directories(streamcat
+ PRIVATE
+ ../../mapiclient)
+
target_link_libraries(streamcat
PRIVATE
monetdb_config_header
diff --git a/clients/examples/C/streamcat.c b/clients/examples/C/streamcat.c
--- a/clients/examples/C/streamcat.c
+++ b/clients/examples/C/streamcat.c
@@ -437,68 +437,20 @@ opener_rastream(char *filename)
}
#ifdef HAVE_CURL
-#include <curl/curl.h>
-
-#ifndef CURL_WRITEFUNC_ERROR
-#define CURL_WRITEFUNC_ERROR 0
-#endif
-
-static size_t
-write_callback(char *buffer, size_t size, size_t nitems, void *userp)
-{
- stream *s = userp;
-
- /* size is expected to always be 1 */
-
- ssize_t sz = mnstr_write(s, buffer, size, nitems);
- if (sz < 0)
- return CURL_WRITEFUNC_ERROR; /* indicate failure to library */
- return (size_t) sz * size;
-}
+#include "curl-stream.h"
static stream *
-open_urlstream(const char *url)
+opener_urlstream(char *url)
{
- CURL *handle;
- stream *s;
- CURLcode ret;
char errbuf[CURL_ERROR_SIZE];
-
- s = buffer_wastream(NULL, url);
- if (s == NULL) {
- return NULL;
- }
-
- if ((handle = curl_easy_init()) == NULL) {
- mnstr_destroy(s);
- return NULL;
- }
-
- errbuf[0] = 0;
-
- if ((ret = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf)) !=
CURLE_OK ||
- (ret = curl_easy_setopt(handle, CURLOPT_URL, url)) != CURLE_OK ||
- (ret = curl_easy_setopt(handle, CURLOPT_WRITEDATA, s)) != CURLE_OK
||
- (ret = curl_easy_setopt(handle, CURLOPT_VERBOSE, 0)) != CURLE_OK ||
- (ret = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1)) != CURLE_OK ||
- (ret = curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1)) !=
CURLE_OK ||
- (ret = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION,
write_callback)) != CURLE_OK ||
- (ret = curl_easy_perform(handle)) != CURLE_OK) {
- curl_easy_cleanup(handle);
- mnstr_destroy(s);
- if (errbuf[0])
- fprintf(stderr, "%s\n", errbuf);
- else
- fprintf(stderr, "%s\n", curl_easy_strerror(ret));
- return NULL;
- }
- curl_easy_cleanup(handle);
- (void) mnstr_get_buffer(s); /* switch to read-only */
+ stream *s = open_urlstream(url, errbuf);
+ if (s == NULL)
+ fprintf(stderr, "%s\n", errbuf);
return s;
}
#else
static stream *
-open_urlstream(const char *url)
+opener_urlstream(char *url)
{
(void) url;
return NULL;
@@ -506,13 +458,6 @@ open_urlstream(const char *url)
#endif
static stream *
-opener_urlstream(char *url)
-{
- stream *s = open_urlstream(url);
- return s;
-}
-
-static stream *
opener_wstream(char *filename)
{
stream *s = open_wstream(filename);
diff --git a/clients/mapiclient/CMakeLists.txt
b/clients/mapiclient/CMakeLists.txt
--- a/clients/mapiclient/CMakeLists.txt
+++ b/clients/mapiclient/CMakeLists.txt
@@ -38,7 +38,8 @@ target_sources(mclient
ReadlineTools.c
ReadlineTools.h
mhelp.c
- mhelp.h)
+ mhelp.h
+ curl-stream.h)
target_include_directories(mclient
PRIVATE
diff --git a/clients/mapiclient/curl-stream.h b/clients/mapiclient/curl-stream.h
new file mode 100644
--- /dev/null
+++ b/clients/mapiclient/curl-stream.h
@@ -0,0 +1,58 @@
+#include <curl/curl.h>
+
+#ifndef CURL_WRITEFUNC_ERROR
+#define CURL_WRITEFUNC_ERROR 0
+#endif
+
+static size_t
+write_callback(char *buffer, size_t size, size_t nitems, void *userp)
+{
+ stream *s = userp;
+
+ /* size is expected to always be 1 */
+
+ ssize_t sz = mnstr_write(s, buffer, size, nitems);
+ if (sz < 0)
+ return CURL_WRITEFUNC_ERROR; /* indicate failure to library */
+ return (size_t) sz * size;
+}
+
+static stream *
+open_urlstream(const char *url, char *errbuf)
+{
+ CURL *handle;
+ stream *s;
+ CURLcode ret;
+
+ s = buffer_wastream(NULL, url);
+ if (s == NULL) {
+ snprintf(errbuf, CURL_ERROR_SIZE, "could not allocate memory");
+ return NULL;
+ }
+
+ if ((handle = curl_easy_init()) == NULL) {
+ mnstr_destroy(s);
+ snprintf(errbuf, CURL_ERROR_SIZE, "could not create CURL
handle");
+ return NULL;
+ }
+
+ errbuf[0] = 0;
+
+ if ((ret = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf)) !=
CURLE_OK ||
+ (ret = curl_easy_setopt(handle, CURLOPT_URL, url)) != CURLE_OK ||
+ (ret = curl_easy_setopt(handle, CURLOPT_WRITEDATA, s)) != CURLE_OK
||
+ (ret = curl_easy_setopt(handle, CURLOPT_VERBOSE, 0)) != CURLE_OK ||
+ (ret = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1)) != CURLE_OK ||
+ (ret = curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1)) !=
CURLE_OK ||
+ (ret = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION,
write_callback)) != CURLE_OK ||
+ (ret = curl_easy_perform(handle)) != CURLE_OK) {
+ curl_easy_cleanup(handle);
+ mnstr_destroy(s);
+ if (errbuf[0] == 0)
+ snprintf(errbuf, CURL_ERROR_SIZE, "%s",
curl_easy_strerror(ret));
+ return NULL;
+ }
+ curl_easy_cleanup(handle);
+ (void) mnstr_get_buffer(s); /* switch to read-only */
+ return s;
+}
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]