config: use white background for translation
[cnoor.git] / quran.c
blob5f9788d728d35856ffdc58837146213463363733
1 #include <fcntl.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include "util.h"
8 #include "quran.h"
10 static size_t file_size(char *path)
12 struct stat st;
13 if (!stat(path, &st))
14 return st.st_size;
15 return 0;
18 static char *file_read(char *path)
20 char *buf;
21 int fd;
22 int c = 0;
23 int n = 0;
24 size_t size = file_size(path);
25 buf = xmalloc(size);
26 fd = open(path, O_RDONLY);
27 while ((c = read(fd, buf + n, size - n)) > 0)
28 n += c;
29 close(fd);
30 return buf;
33 struct quran *quran_alloc(char *path)
35 struct quran *quran = xmalloc(sizeof(struct quran));
36 memset(quran, 0, sizeof(quran));
37 quran->text = file_read(path);
38 return quran;
41 void quran_aya(struct quran *quran, char *buf, size_t len, int aya)
43 int i;
44 char *s = quran->text;
45 char *e;
46 int n;
47 for (i = 0; i < aya; i++)
48 s = strchr(s, '\n') + 1;
49 e = strchr(s, '\n');
50 n = e - s - 1;
51 if (n > len - 1)
52 n = len - 1;
53 memcpy(buf, s, n);
54 buf[n] = '\0';
57 void quran_free(struct quran *quran)
59 free(quran->text);
60 free(quran);
63 static int ayas[] = {
64 7, 286, 200, 176, 120, 165, 206, 75, 129, 109,
65 123, 111, 43, 52, 99, 128, 111, 110, 98, 135,
66 112, 78, 118, 64, 77, 227, 93, 88, 69, 60,
67 34, 30, 73, 54, 45, 83, 182, 88, 75, 85,
68 54, 53, 89, 59, 37, 35, 38, 29, 18, 45,
69 60, 49, 62, 55, 78, 96, 29, 22, 24, 13,
70 14, 11, 11, 18, 12, 12, 30, 52, 52, 44,
71 28, 28, 20, 56, 40, 31, 50, 40, 46, 42,
72 29, 19, 36, 25, 22, 17, 19, 26, 30, 20,
73 15, 21, 11, 8, 8, 19, 5, 8, 8, 11,
74 11, 8, 3, 9, 5, 4, 7, 3, 6,
75 3, 5, 4, 5, 6};
77 int sura_ayas(int sura)
79 return ayas[sura - 1];
82 int sura_start(int sura)
84 int i;
85 int n = 0;
86 for (i = 0; i < sura - 1; i++)
87 n += ayas[i];
88 return n;
91 int aya_num(int sura, int aya)
93 return sura_start(sura) + aya;