From b1908948bae2780a158e444e93212e311faee422 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Thu, 5 Mar 2009 10:51:20 +0330 Subject: [PATCH] quran: use constant NAYA aya count --- quran.c | 19 +++++++------------ quran.h | 3 ++- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/quran.c b/quran.c index 361c57b..b0c4c8d 100644 --- a/quran.c +++ b/quran.c @@ -16,17 +16,17 @@ static size_t file_size(int fd) return 0; } -static int newlines(char *s) +static void init_ayas(struct quran *quran) { - int n; - for (n = 0; (s = strchr(s, '\n')); s++, n++); - return n; + int i = 0; + char *s = quran->text; + quran->ayas[0] = quran->text; + while (i < NAYA && (s = strchr(s, '\n'))) + quran->ayas[++i] = ++s; } struct quran *quran_alloc(char *path) { - int i; - int nayas; struct quran *quran = xmalloc(sizeof(struct quran)); memset(quran, 0, sizeof(quran)); quran->fd = open(path, O_RDONLY); @@ -36,11 +36,7 @@ struct quran *quran_alloc(char *path) MAP_SHARED, quran->fd, 0); if (quran->text == MAP_FAILED) xerror("mmap: "); - nayas = newlines(quran->text); - quran->ayas = xmalloc(sizeof(quran->ayas) * nayas); - quran->ayas[0] = quran->text; - for (i = 1; i < nayas; i++) - quran->ayas[i] = strchr(quran->ayas[i - 1], '\n') + 1; + init_ayas(quran); return quran; } @@ -59,7 +55,6 @@ void quran_free(struct quran *quran) { munmap(quran->text, file_size(quran->fd)); close(quran->fd); - free(quran->ayas); free(quran); } diff --git a/quran.h b/quran.h index f951300..077aa76 100644 --- a/quran.h +++ b/quran.h @@ -2,11 +2,12 @@ #define _QURAN_H #define NSURA 114 +#define NAYA 6236 struct quran { int fd; char *text; - char **ayas; + char *ayas[NAYA]; }; struct quran *quran_alloc(char *path); -- 2.11.4.GIT