aboutsummaryrefslogtreecommitdiff
path: root/lib/mirrors.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2020-05-26 14:43:00 -0400
committerGitHub <noreply@github.com>2020-05-26 14:43:00 -0400
commitdf0686fb8b6fefebda0548d39caad2622e2bce89 (patch)
treecbd2d92667a0ab45221a220948f6ee33b2926a1c /lib/mirrors.c
parentc205840e737b23614a686a9675b896106cee5c64 (diff)
parent065d39bff58c02719abf5a710b22d8355ff1b19d (diff)
downloadspmc-df0686fb8b6fefebda0548d39caad2622e2bce89.tar.gz
Merge pull request #38 from jhunkeler/from-file
From file
Diffstat (limited to 'lib/mirrors.c')
-rw-r--r--lib/mirrors.c81
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