From 4722b1e5813a616ef72878e1afbf30dc8c266a4c Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Sat, 28 Feb 2009 17:27:42 +0330 Subject: [PATCH] quran: add quran parsing support --- quran.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ quran.h | 14 ++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 quran.c create mode 100644 quran.h diff --git a/quran.c b/quran.c new file mode 100644 index 0000000..bfdf712 --- /dev/null +++ b/quran.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include "util.h" +#include "quran.h" + +static size_t file_size(char *path) +{ + struct stat st; + if (!stat(path, &st)) + return st.st_size; + return 0; +} + +static char *file_read(char *path) +{ + char *buf; + int fd; + int c = 0; + int n = 0; + size_t size = file_size(path); + buf = xmalloc(size); + fd = open(path, O_RDONLY); + while ((c = read(fd, buf + n, size - n)) > 0) + n += c; + close(fd); + return buf; +} + +struct quran *quran_alloc(char *path) +{ + struct quran *quran = xmalloc(sizeof(struct quran)); + memset(quran, 0, sizeof(quran)); + quran->text = file_read(path); + return quran; +} + +void quran_aya(struct quran *quran, char *buf, size_t len, int aya) +{ + int i; + char *s = quran->text; + char *e; + int n; + for (i = 0; i < aya; i++) + s = strchr(s, '\n') + 1; + e = strchr(s, '\n'); + n = e - s; + if (n > len - 1) + n = len - 1; + memcpy(buf, s, n); + buf[n] = '\n'; +} + +void quran_free(struct quran *quran) +{ + free(quran); +} diff --git a/quran.h b/quran.h new file mode 100644 index 0000000..1b88655 --- /dev/null +++ b/quran.h @@ -0,0 +1,14 @@ +#ifndef _QURAN_H +#define _QURAN_H + +#define NSURA 114 + +struct quran { + char *text; +}; + +struct quran *quran_alloc(char *path); +void quran_aya(struct quran *quran, char *buf, size_t len, int aya); +void quran_free(struct quran *quran); + +#endif -- 2.11.4.GIT