forked from snap-stanford/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.cpp
95 lines (86 loc) · 2.98 KB
/
book.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
/////////////////////////////////////////////////
// Includes
#include "StdAfx.h"
#include "book.h"
/////////////////////////////////////////////////
// Book
THash<TStr, TBook::TBookLoadF> TBook::TypeToLoadFH(100);
PBook TBook::Load(TSIn& SIn){
TStr TypeNm(SIn);
TBookLoadF LoadF=TypeToLoadFH.GetDat(TypeNm);
return (*LoadF())(SIn);
}
bool TBook::Reg(const TStr& TypeNm, const TBookLoadF& LoadF){
IAssert(!TypeToLoadFH.IsKey(TypeNm));
TypeToLoadFH.AddDat(TypeNm, LoadF);
return true;
}
/////////////////////////////////////////////////
// Bible
bool TBible::IsReg=TBible::MkReg();
void TBible::AddPsalm(
const TStr& ChpNm, const int& SecN, const int& SSecN, const TStr& SSecStr){
TBiblePsalm Psalm(ChpNm, SecN, SSecN, SSecStr);
int SecId=PsalmV.Add(Psalm);
TStr TitleStr=ChpNm+"|"+TInt::GetStr(SecN)+":"+TInt::GetStr(SSecN);
Bix->AddSec(SecId, TitleStr+" "+SSecStr);
}
TBible::TBible(const TStr& FNm):
TBook("Bible", "King James Version", "Religious"),
ChpNmSecIdKdV(), PsalmV(), Bix(TBix::New()){
// open file
PSIn SIn=TFIn::New(FNm);
TILx Lx(SIn, TFSet());
// define state variables
bool InPsalm=false; TStr ChpNm; TChA SecNChA; int SecN;
TChA SSecNChA; int SSecN; TStr SSecStr;
// parsing
while (Lx.GetSym(syLn, syEof)!=syEof){
TChA& Ln=Lx.Str;
int VBarChN=Ln.SearchCh('|');
if ((VBarChN!=-1)&&(0<VBarChN)&&(VBarChN<Lx.Str.Len())&&
TCh::IsAlNum(Ln[VBarChN-1])&&TCh::IsNum(Ln[VBarChN+1])){
if (InPsalm){AddPsalm(ChpNm, SecN, SSecN, SSecStr);}
// if (PsalmV.Len()>100){return;}
InPsalm=true;
// psalm name
ChpNm=Ln.GetSubStr(0, VBarChN-1).GetTrunc();
if (ChpNmSecIdKdV.Empty()||ChpNmSecIdKdV.Last().Key!=ChpNm){
ChpNmSecIdKdV.Add(TStrIntKd(ChpNm, PsalmV.Len()));}
// section number
SecNChA.Clr(); int ChN=VBarChN+1;
while (TCh::IsNum(Ln[ChN])){SecNChA+=Ln[ChN]; ChN++;}
SecN=TStr(SecNChA).GetInt();
IAssert(Ln[ChN]==':');
// subsection number
SSecNChA.Clr(); ChN++;
while (TCh::IsNum(Ln[ChN])){SSecNChA+=Ln[ChN]; ChN++;}
SSecN=TStr(SSecNChA).GetInt();
// text
SSecStr=Ln.GetSubStr(ChN, Ln.Len()-1).GetTrunc();
} else
if (InPsalm){
SSecStr=(SSecStr+' '+Ln).GetTrunc();
}
}
// save last psalm
if (InPsalm){AddPsalm(ChpNm, SecN, SSecN, SSecStr);}
}
PBookToc TBible::GetBookToc() const {
PBookToc BookToc=TBookToc::New();
BookToc->AddLevNm("Chapter");
for (int ChpN=0; ChpN<ChpNmSecIdKdV.Len(); ChpN++){
TStr TitleNm=ChpNmSecIdKdV[ChpN].Key;
int SecId=ChpNmSecIdKdV[ChpN].Dat;
BookToc->AddItem(0, TitleNm, SecId);
}
return BookToc;
}
void TBible::GetSecInfo(
const int& SecId, TStr& SecIdStr, TStr& TitleStr, TStr& SecStr) const {
TBiblePsalm& Psalm=PsalmV[SecId];
SecIdStr=TInt::GetStr(SecId);
TitleStr=Psalm.GetChpNm()+"|"+
TInt::GetStr(Psalm.GetSecN())+":"+TInt::GetStr(Psalm.GetSSecN());
SecStr=Psalm.GetSSecStr();
}