diff options
-rw-r--r-- | include/fs.h | 3 | ||||
-rw-r--r-- | include/mirrors.h | 1 | ||||
-rw-r--r-- | lib/fs.c | 83 | ||||
-rw-r--r-- | lib/mirrors.c | 81 |
4 files changed, 84 insertions, 84 deletions
diff --git a/include/fs.h b/include/fs.h index c41b649..81acd92 100644 --- a/include/fs.h +++ b/include/fs.h @@ -41,6 +41,5 @@ char *human_readable_size(uint64_t n); char *expandpath(const char *_path); char *spm_mkdtemp(const char *base, const char *name, const char *extended_path); int touch(const char *path); - - +char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn *readerFn); #endif //SPM_FSTREE_H diff --git a/include/mirrors.h b/include/mirrors.h index c9c5c23..dd4b256 100644 --- a/include/mirrors.h +++ b/include/mirrors.h @@ -7,7 +7,6 @@ #define SPM_MIRROR_MAX 0xff #define SPM_MIRROR_FILENAME "mirrorlist" -char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn *readerFn); char **mirror_list(const char *filename); void mirror_list_free(char **m); void mirror_clone(Manifest *info, char *dest); @@ -641,3 +641,86 @@ char *spm_mkdtemp(const char *base, const char *name, const char *extended_path) return 0; } + +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); + result = calloc(2, sizeof(char *)); + result[0] = strdup(""); + return result; + } + + 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; +}
\ No newline at end of file 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 |