-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuranInfo.cpp
202 lines (166 loc) · 4.36 KB
/
QuranInfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "QuranInfo.h"
#include <fstream>
#include <algorithm>
#include <sstream>
//****************************************
// UtilityFunctions
//****************************************
//Returns true if c is a tashkeel character
bool IsTashkeel(char c)
{
return string("ًٌٍَُِّْ").find(c) != string::npos;
}
//Return true if the two strings are the same neglecting tashkeel differences.
bool CompareNoTashkeel(string str1, string str2)
{
int ndx1 = 0, ndx2 = 0;
while(ndx1 < (int)str1.length() || ndx2 < (int)str2.length())
{
while(ndx1 < (int)str1.length() && IsTashkeel(str1[ndx1])) ndx1++;
while(ndx2 < (int)str2.length() && IsTashkeel(str2[ndx2])) ndx2++;
if (ndx1 >= (int)str1.length() || ndx2 >= (int)str2.length())
{
if (ndx1 >= (int)str1.length() && ndx2 >= (int)str2.length())
break;
else
return false;
}
if (str1[ndx1] != str2[ndx2])
return false;
ndx1++; ndx2++;
}
return true;
}
//Trim string and remove '@' and 'ـ' characters
string AdjustString(string input)
{
int ndx = 0;
//Remove '@'
//while((pos = input.find('@',ndx)) != input.npos)
//{
// input.erase( input.begin() + pos );
// ndx = pos;
//}
string::iterator itEnd = input.end();
itEnd = remove(input.begin(), itEnd,'@');
itEnd = remove(input.begin(), itEnd,'ـ');
input.resize(itEnd - input.begin());
ndx = 0;
while(ndx < (int)input.length() && (input[ndx] == ' ' || input[ndx] == '\t' || input[ndx] == '\n' || input[ndx] == '\r'))
ndx++;
int startNdx = ndx;
ndx = input.length() - 1;
while(ndx >= startNdx && (input[ndx] == ' ' || input[ndx] == '\t' || input[ndx] == '\n' || input[ndx] == '\r'))
ndx--;
int endNdx = ndx;
input = input.substr(startNdx, endNdx - startNdx + 1);
return input;
}
void QuranInfo::LoadQuranTextMuhaddith(string fileName)
{
ifstream ifs(fileName.c_str());
if (!ifs) throw FileNotFoundException(fileName);
char tempChar;
//string ayaText = "";
wordsNdx.push_back(0);
ayasWords.push_back(0);
linesWords.push_back(0);
while(ifs.good())
{
string line;
getline(ifs,line,'\n');
if (!ifs)
break;
if (line[0] == '*') //comments
{
//Comment lines
if (line[1] == '1')
{
//Joz2
joz2Ayas.push_back(ayasWords.size()-1);
}
else if (line[1] == '2')
{
//Parse sora number and name
int soraNumber;
string soraName;
stringstream ss(line.substr(3));
ss>>soraNumber>>soraName;
if (!ss) continue; //not a new sora
//ss>>tempChar;
getline(ss,soraName,'\n');
soraName = AdjustString(soraName);
sorasNames.push_back(soraName);
sorasAyas.push_back(ayasWords.size()-1);
sorasLines.push_back(linesWords.size()-1);
soraNameNdx[soraName] = sorasNames.size()-1;
}
else if (line[1] == '3')
{
//Read page number
stringstream ss(line.substr(3));
int pageNumber;
ss>>tempChar>>pageNumber;
//Page number
if (pageNumber > (int)pagesLines.size())
{
pagesLines.push_back(linesWords.size()-1);
pagesAyas.push_back(ayasWords.size()-1);
}
}
continue;
}
//Add empty line for sora name
if ((*sorasLines.rbegin()) == linesWords.size()-1)
{
//Sora start, so add the line
linesWords.push_back( wordsNdx.size()-1 ); //Empty line for sora name
}
//Aya text
line = AdjustString(line);
bool isSpecialBasmala = false;
if (CompareNoTashkeel(line,"بسم الله الرحمن الرحيم"))
{
line = "بِسْمِ اللَّهِ الرَّحْمـَنِ الرَّحِيمِ";
if (sorasAyas.size() > 1)
isSpecialBasmala = true;
}
stringstream words(line);
while(words.good())
{
string word;
words>>word;
if (word.size() == 0) continue;
if (word == "{س}")
{
//Nothing to do!
}
else if (word[0] == '{')
{
//Aya marker
ayasWords.push_back(wordsNdx.size()-1);
int ayaNumber;
stringstream ss(word.substr(1));
ss>>ayaNumber;
ayasNumbers.push_back(ayaNumber);
}
else
{
quran.append( word + ' ' );
wordsNdx.push_back(quran.length());
}
}
if (isSpecialBasmala)
{
ayasWords.push_back(wordsNdx.size()-1);
ayasNumbers.push_back(0);
}
linesWords.push_back(wordsNdx.size()-1);
}
//indecies ends
joz2Ayas.push_back(ayasWords.size()-1);
pagesLines.push_back(linesWords.size()-1);
pagesAyas.push_back(ayasWords.size()-1);
sorasLines.push_back(linesWords.size()-1);
sorasAyas.push_back(ayasWords.size()-1);
}