diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2020-05-25 01:17:40 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2020-05-25 01:17:40 -0400 |
commit | 2d981c10be5f902442c885fb7e2cafa6154145c7 (patch) | |
tree | 500d53d43af7cc140a556f43bf398cad5d3a1ccd /lib/mirrors.c | |
parent | c205840e737b23614a686a9675b896106cee5c64 (diff) | |
download | spmc-2d981c10be5f902442c885fb7e2cafa6154145c7.tar.gz |
Move file_readlines function out of mirrors.c
Diffstat (limited to 'lib/mirrors.c')
-rw-r--r-- | lib/mirrors.c | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/lib/mirrors.c b/lib/mirrors.c index 6bc7aed..1c51845 100644 --- a/lib/mirrors.c +++ b/lib/mirrors.c @@ -1,87 +1,6 @@ #include "spm.h" #include "url.h" -char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn *readerFn) { - FILE *fp = NULL; - char **result = NULL; - char *buffer = NULL; - size_t lines = 0; - - if ((fp = fopen(filename, "r")) == NULL) { - perror(filename); - fprintf(SYSERROR); - return NULL; - } - - // Allocate buffer - if ((buffer = calloc(BUFSIZ, sizeof(char))) == NULL) { - perror("line buffer"); - fprintf(SYSERROR); - fclose(fp); - return NULL; - } - - // count number the of lines in the file - while ((fgets(buffer, BUFSIZ - 1, fp)) != NULL) { - lines++; - } - - if (!lines) { - free(buffer); - fclose(fp); - return NULL; - } - - rewind(fp); - - // Handle invalid start offset - if (start > lines) { - start = 0; - } - - // Adjust line count when start offset is non-zero - if (start != 0 && start < lines) { - lines -= start; - } - - - // Handle minimum and maximum limits - if (limit == 0 || limit > lines) { - limit = lines; - } - - // Populate results array - result = calloc(limit + 1, sizeof(char *)); - for (size_t i = start; i < limit; i++) { - if (i < start) { - continue; - } - - if (fgets(buffer, BUFSIZ - 1, fp) == NULL) { - break; - } - - if (readerFn != NULL) { - int status = readerFn(i - start, &buffer); - // A status greater than zero indicates we should ignore this line entirely and "continue" - // A status less than zero indicates we should "break" - // A zero status proceeds normally - if (status > 0) { - i--; - continue; - } else if (status < 0) { - break; - } - } - result[i] = strdup(buffer); - memset(buffer, '\0', BUFSIZ); - } - - free(buffer); - fclose(fp); - return result; -} - /** * * @param filename |