Changeset: 371628a49bce for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=371628a49bce
Modified Files:
        monetdb5/modules/atoms/url.mx
Branch: Apr2012
Log Message:

url: implement getContent

Added an implementation for url.getContent() based on urlstream.  This
means besides supporting "local" files (file://) we support any url that
cURL supports.


diffs (53 lines):

diff --git a/monetdb5/modules/atoms/url.mx b/monetdb5/modules/atoms/url.mx
--- a/monetdb5/modules/atoms/url.mx
+++ b/monetdb5/modules/atoms/url.mx
@@ -996,9 +996,46 @@ URLgetBasename(str *retval, str *t)
 str
 URLgetContent(str *retval, str *Str1)
 {
-       (void) Str1;            /* fool compiler */
-       *retval = 0;
-       throw(MAL, "url.getContent", "not yet implemented");
+       stream *f;
+       str retbuf = NULL;
+       str oldbuf = NULL;
+       char *buf[8096];
+       size_t len;
+       size_t rlen;
+
+       if ((f = open_urlstream(*Str1)) == NULL)
+               throw(MAL, "url.getContent", "failed to open urlstream");
+
+       if (mnstr_errnr(f) != 0) {
+               str err = createException(MAL, "url.getContent",
+                               "opening stream failed: %s", mnstr_error(f));
+               mnstr_destroy(f);
+               *retval = NULL;
+               return err;
+       }
+
+       rlen = 0;
+       while ((len = mnstr_read(f, buf, 1, sizeof(buf))) != 0) {
+               if (retbuf != NULL) {
+                       oldbuf = retbuf;
+                       retbuf = GDKrealloc(retbuf, rlen + len + 1);
+               } else {
+                       retbuf = GDKmalloc(len + 1);
+               }
+               if (retbuf == NULL) {
+                       if (oldbuf != NULL)
+                               GDKfree(oldbuf);
+                       mnstr_destroy(f);
+                       throw(MAL, "url.getContent", "contents too large");
+               }
+               oldbuf = NULL;
+               (void)memcpy(retbuf + rlen, buf, len);
+               rlen += len;
+       }
+       retbuf[rlen] = '\0';
+
+       *retval = retbuf;
+       return MAL_SUCCEED;
 }
 
 str
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to