diff options
-rw-r--r-- | include/strlist.h | 2 | ||||
-rw-r--r-- | lib/strlist.c | 104 |
2 files changed, 96 insertions, 10 deletions
diff --git a/include/strlist.h b/include/strlist.h index 76b6d5f..9614894 100644 --- a/include/strlist.h +++ b/include/strlist.h @@ -4,6 +4,7 @@ */ #ifndef SPM_STRLIST_H #define SPM_STRLIST_H +#include "metadata.h" typedef struct { size_t num_alloc; @@ -12,6 +13,7 @@ typedef struct { } StrList; StrList *strlist_init(); +int strlist_append_file(StrList *pStrList, char *path, ReaderFn *readerFn); void strlist_remove(StrList *pStrList, size_t index); long double strlist_item_as_long_double(StrList *pStrList, size_t index); double strlist_item_as_double(StrList *pStrList, size_t index); diff --git a/lib/strlist.c b/lib/strlist.c index aac2755..c2cff09 100644 --- a/lib/strlist.c +++ b/lib/strlist.c @@ -4,6 +4,7 @@ */ #include "spm.h" #include "strlist.h" +#include "url.h" /** * @@ -46,21 +47,87 @@ void strlist_append(StrList *pStrList, char *str) { pStrList->num_alloc++; } +static int reader_strlist_append_file(size_t lineno, char **line) { + (void)(lineno); // unused parameter + (void)(line); // unused parameter + return 0; +} + /** - * Produce a new copy of a `StrList` - * @param pStrList `StrList` - * @return `StrList` copy + * Append lines from a local file or remote URL (HTTP/s only) + * @param pStrList + * @param path file path or HTTP/s address + * @param readerFn pointer to a reader function (use NULL to retrieve all data) + * @return 0=success -1=error (spmerrno set) */ -StrList *strlist_copy(StrList *pStrList) { - StrList *result = strlist_init(); - if (pStrList == NULL || result == NULL) { - return NULL; +int strlist_append_file(StrList *pStrList, char *path, ReaderFn *readerFn) { + int is_remote = 0; + char *filename = NULL; + char *from_file_tmpdir = NULL; + char **from_file = NULL; + char *fetched = NULL; + + if (readerFn == NULL) { + readerFn = reader_strlist_append_file; } - for (size_t i = 0; i < strlist_count(pStrList); i++) { - strlist_append(result, strlist_item(pStrList, i)); + filename = calloc(PATH_MAX, sizeof(char)); + if (filename == NULL) { + spmerrno = errno; + return -1; } - return result; + + filename = expandpath(path); + + is_remote = (startswith(path, "http") || startswith(path, "https")); + if (is_remote) { + long response = 0; + from_file_tmpdir = spm_mkdtemp(TMP_DIR, __FUNCTION__, NULL); + + if (from_file_tmpdir == NULL) { + spmerrno = errno; + spmerrno_cause("file_from temp directory"); + return -1; + } + + fetched = join((char *[]){from_file_tmpdir, basename(path), NULL}, DIRSEPS); + if ((response = fetch(path, fetched)) >= 400) { + spmerrno = SPM_ERR_FETCH; + char cause[PATH_MAX]; + sprintf(cause, "HTTP(%ld): %s: %s", response, http_response_str(response), path); + spmerrno_cause(cause); + return -1; + } + + strcpy(filename, fetched); + } + + if (exists(filename) != 0) { + spmerrno = errno; + spmerrno_cause(filename); + return -1; + } + + from_file = file_readlines(filename, 0, 0, readerFn); + if (from_file == NULL) { + spmerrno = errno; + spmerrno_cause(filename); + return -1; + } + + for (size_t record = 0; from_file[record] != NULL; record++) { + strlist_append(pStrList, from_file[record]); + free(from_file[record]); + } + free(from_file); + + if (from_file_tmpdir) { + rmdirs(from_file_tmpdir); + } + free(from_file_tmpdir); + free(filename); + + return 0; } /** @@ -83,6 +150,23 @@ void strlist_append_strlist(StrList *pStrList1, StrList *pStrList2) { } /** + * Produce a new copy of a `StrList` + * @param pStrList `StrList` + * @return `StrList` copy + */ +StrList *strlist_copy(StrList *pStrList) { + StrList *result = strlist_init(); + if (pStrList == NULL || result == NULL) { + return NULL; + } + + for (size_t i = 0; i < strlist_count(pStrList); i++) { + strlist_append(result, strlist_item(pStrList, i)); + } + return result; +} + +/** * Remove a record by index from a `StrList` * @param pStrList * @param index |