Browse Source

biblec namespace

master
Daniel 4 weeks ago
parent
commit
89a6a37301
3 changed files with 29 additions and 38 deletions
  1. +13
    -17
      biblec.c
  2. +6
    -8
      main.h
  3. +10
    -13
      test.c

+ 13
- 17
biblec.c View File

@@ -14,7 +14,7 @@ int strToInt(char *buf) {
}

// Parse BibleC index file, see format.md
int parseIndexFile(struct Translation *translation, char *indexLocation) {
int biblec_parse(struct Biblec_translation *translation, char *indexLocation) {
FILE *index = fopen(indexLocation, "r");
if (index == NULL) {
return FILE_NOT_FOUND;
@@ -24,14 +24,14 @@ int parseIndexFile(struct Translation *translation, char *indexLocation) {
// file location is in the same folder as the index file.
strcpy(translation->location, indexLocation);
translation->location[strlen(indexLocation) - 1] = 't';
char line[INDEX_MAX_LENGTH];

int book = 0;
char line[INDEX_MAX_LENGTH];
while (fgets(line, INDEX_MAX_LENGTH, index) != NULL) {
// Remove trailing breakline
strtok(line, "\n");

// Pointer
// Pointer to line content
char *contents = line + 1;

// Make a duplicate for manipulation
@@ -87,8 +87,7 @@ int parseIndexFile(struct Translation *translation, char *indexLocation) {
return 0;
}

int getBookID(struct Translation *translation, char *book) {
// First, find the book
int getBookID(struct Biblec_translation *translation, char *book) {
int bookID = BOOK_NOT_FOUND;
for (int i = 0; i < translation->length; i++) {
if (!strcmp(book, translation->book[i].name)) {
@@ -99,7 +98,7 @@ int getBookID(struct Translation *translation, char *book) {
return bookID;
}

int reader_next(struct Reader *reader) {
int biblec_next(struct Biblec_reader *reader) {
// Reached end of requested verses
if (reader->linesRead > reader->to) {
return -1;
@@ -116,7 +115,8 @@ int reader_next(struct Reader *reader) {
}

// Create a new reader structure
int reader_new(struct Reader *reader, struct Translation *translation, char *book, int chapter, int verse, int to) {
int biblec_new(struct Biblec_reader *reader, struct Biblec_translation *translation,
char *book, int chapter, int verse, int to) {
int c;

// Check book ID
@@ -136,13 +136,13 @@ int reader_new(struct Reader *reader, struct Translation *translation, char *boo
line += translation->book[bookID].chapters[c];
}

// When 0 is passed for to, grab the entire chapter.
// When 0 is passed for "to", grab the entire chapter.
// Else, "to" refers to how many verse to
// count in the struct.
if (to == 0) {
to = translation->book[bookID].chapters[c] - 1;
} else {
// Else, "to" refers to how many verse to
// count in the struct. Set it here.
to = to - verse;
to -= verse;
}

// Add the line over to the specific verse
@@ -160,12 +160,8 @@ int reader_new(struct Reader *reader, struct Translation *translation, char *boo
}

// Loop through until it gets to the line
int i = 0;
char verseText[VERSE_LENGTH];
while (1) {
if (i == line) {break;}
i++;

for (int i = 0; i != line; i++) {
if (fgets(verseText, VERSE_LENGTH, reader->file) == NULL) {
return FILE_ERROR;
}


+ 6
- 8
main.h View File

@@ -1,7 +1,7 @@
#ifndef __BIBLEC
#define __BIBLEC 1

enum BibleC_max {
enum Biblec_max {
VERSE_LENGTH = 700,
INDEX_MAX_LENGTH = 500
};
@@ -13,19 +13,17 @@ enum BibleC_error {
CHAPTER_TOO_BIG = -4
};

struct Reader {
struct Biblec_reader {
char *book;
int chapter;
int verse;
int to;

char result[VERSE_LENGTH];

FILE *file;
int linesRead;
};

struct Translation {
struct Biblec_translation {
char name[20];
char lang[20];
char location[50];
@@ -38,8 +36,8 @@ struct Translation {
}book[66];
};

int parseIndexFile(struct Translation *translation, char *indexLocation);
int reader_new(struct Reader *reader, struct Translation *translation, char *book, int chapter, int verse, int to);
int reader_next(struct Reader *reader);
int biblec_parse(struct Biblec_translation *translation, char *indexLocation);
int biblec_new(struct Biblec_reader *reader, struct Biblec_translation *translation, char *book, int chapter, int verse, int to);
int biblec_next(struct Biblec_reader *reader);

#endif

+ 10
- 13
test.c View File

@@ -4,38 +4,35 @@
#include "main.h"

// Runtime struct
struct Translation loadedTranslations;
struct Biblec_translation loadedTranslation;

int main() {
int tryFile = parseIndexFile(
&loadedTranslations,
int tryFile = biblec_parse(
&loadedTranslation,
"../bibles/web.i"
);

if (tryFile) {
printf("Index parsing error");
puts("Index parsing error");
return 0;
}

struct Reader reader;
int tryReader = reader_new(
struct Biblec_reader reader;
int tryReader = biblec_new(
&reader,
&loadedTranslations,
&loadedTranslation,
"John",
3,
1,
0 // Get whole chapter
2
);

if (tryReader) {
printf("Verse error");
puts("Verse error");
return 0;
}

int status = 0;
while (1) {
status = reader_next(&reader);
if (status) {break;}
while (!biblec_next(&reader)) {
printf("%s\n", reader.result);
}



Loading…
Cancel
Save