Browse Source

Add stream reader from libheb12

master
Daniel 1 year ago
parent
commit
aa6b97a434
3 changed files with 96 additions and 12 deletions
  1. +71
    -0
      get.c
  2. +12
    -1
      haplous.h
  3. +13
    -11
      test.c

+ 71
- 0
get.c View File

@@ -196,3 +196,74 @@ char *haplous_work_chapter_get(FILE *file, struct haplous_reference ref,
*err = HAPLOUS_OK;
return buffer;
}

struct haplous_reader haplous_reader_new(struct haplous_work work, struct haplous_reference ref, int *err) {
struct haplous_reader reader = {
.work = work,
.reference = ref,
.verse = NULL,
.current_verse = 1,
};

fseek(reader.work.file, 0, SEEK_SET);
int line = 0;
line = haplous_work_book_seek(reader.work.file, reader.reference.id);
line = haplous_work_chapter_seek(reader.work.file, reader.reference.chapter);
if (line < 0) {
*err = HAPLOUS_REF_NOT_FOUND;
return reader;
}

return reader;
}

// haplous_next provides an interface for obtaining a reference verse-by-verse
int haplous_next(struct haplous_reader *reader) {

// seek to the first verse
int c;
while ((c = getc(reader->work.file)) != EOF) {
if (c == '\n') {
reader->current_verse += 1;
}

if (reader->current_verse >= reader->reference.verse_start) {
break;
}
}

size_t buf_size = 500;
char *buffer = malloc(buf_size);
if (buffer == NULL) {
return HAPLOUS_OUT_OF_MEMORY;
}

// load the verse to the buffer
size_t i = 0;
while ((c = getc(reader->work.file)) != EOF) {
if (c == '\n') {
break;
}

if (i >= buf_size) {
buf_size += 1;
buffer = realloc(buffer, buf_size);
if (buffer == NULL) {
free(buffer);
return HAPLOUS_OUT_OF_MEMORY;
}
}

buffer[i] = (char)c;
i++;
}
buffer[i] = '\0';

reader->verse = buffer;
if (reader->current_verse <= reader->reference.verse_end) {
reader->current_verse += 1;
return HAPLOUS_CONTINUE;
} else {
return HAPLOUS_OK;
}
}

+ 12
- 1
haplous.h View File

@@ -12,6 +12,7 @@ extern "C" {
#define MAX_ID_LEN 20

enum haplous_error {
HAPLOUS_CONTINUE = 1, // used for the reader
HAPLOUS_OK = 0,
HAPLOUS_OTHER_ERROR = -1,
HAPLOUS_INVALID_REF = -2,
@@ -51,6 +52,13 @@ struct haplous_work {
struct haplous_work_metadata metadata;
};

struct haplous_reader {
struct haplous_work work;
struct haplous_reference reference;
char *verse;
size_t current_verse;
};

struct haplous_work haplous_work_init(const char *, int *);
int haplous_work_cleanup(struct haplous_work *);

@@ -59,8 +67,11 @@ char *haplous_work_verses_get(FILE *, struct haplous_reference, int *);

char *haplous_work_metadata_get(FILE *, const char[MAX_ID_LEN]);

struct haplous_reader haplous_reader_new(struct haplous_work, struct haplous_reference, int *);
int haplous_next(struct haplous_reader *);

#ifdef __cplusplus
}
#endif

#endif
#endif

+ 13
- 11
test.c View File

@@ -14,19 +14,22 @@ void testRef(char *name, int chapter, int start, int to)
name,
chapter,
start,
to,
to + 1,
};
char *text =
haplous_work_verses_get(work.file, ref, &err);

struct haplous_reader reader = haplous_reader_new(work, ref, &err);
assert(err == 0);

while (haplous_next(&reader) == HAPLOUS_CONTINUE) {
puts(reader.verse);
//free(reader.verse);
}

free(reader.verse);

if (err != HAPLOUS_OK) {
printf("Error parsing: %s %d %d:%d (%d)\n", name, chapter, start, to, err);
}

// Demo didn't have this, leaked 1/3 of
// allocated memory
free(text);
}

int main()
@@ -38,14 +41,13 @@ int main()
}

clock_t start_time = clock();
for (int i = 0; i < 100; i++) {
testRef("Rev", 1, 1, 1);
for (int i = 0; i < 5; i++) {
testRef("John", 3, 16, 17);
}

double elapsed_time =
(double)(clock() - start_time) / CLOCKS_PER_SEC;

printf("Done in %f seconds\n", elapsed_time);
haplous_work_cleanup(&work);
}

Loading…
Cancel
Save