1 /******************************************************************************
2  *
3  *  swcom.cpp -	code for base class 'SWCom'- The basis for all commentary
4  *		modules
5  *
6  * $Id: swcom.cpp 2893 2013-07-16 03:07:02Z scribe $
7  *
8  * Copyright 1997-2013 CrossWire Bible Society (http://www.crosswire.org)
9  *	CrossWire Bible Society
10  *	P. O. Box 2528
11  *	Tempe, AZ  85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 
25 #include <utilstr.h>
26 #include <swcom.h>
27 #include <localemgr.h>
28 #include <versekey.h>
29 
30 
31 SWORD_NAMESPACE_START
32 
33 
34 /******************************************************************************
35  * SWCom Constructor - Initializes data for instance of SWCom
36  *
37  * ENT:	imodname - Internal name for module
38  *	imoddesc - Name to display to user for module
39  *	idisp	 - Display object to use for displaying
40  */
41 
SWCom(const char * imodname,const char * imoddesc,SWDisplay * idisp,SWTextEncoding enc,SWTextDirection dir,SWTextMarkup mark,const char * ilang,const char * versification)42 SWCom::SWCom(const char *imodname, const char *imoddesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char *ilang, const char *versification): SWModule(imodname, imoddesc, idisp, "Commentaries", enc, dir, mark, ilang) {
43 	this->versification = 0;
44 	stdstr(&(this->versification), versification);
45 	delete key;
46 	key = (VerseKey *)createKey();
47 	tmpVK1 = (VerseKey *)createKey();
48 	tmpVK2 = (VerseKey *)createKey();
49         tmpSecond = false;
50 }
51 
52 
53 /******************************************************************************
54  * SWCom Destructor - Cleans up instance of SWCom
55  */
56 
~SWCom()57 SWCom::~SWCom() {
58 	delete tmpVK1;
59 	delete tmpVK2;
60 	delete [] versification;
61 }
62 
63 
createKey() const64 SWKey *SWCom::createKey() const {
65 	VerseKey *vk = new VerseKey();
66 
67 	vk->setVersificationSystem(versification);
68 
69 	return vk;
70 }
71 
72 
getIndex() const73 long SWCom::getIndex() const {
74 	VerseKey *key = &getVerseKey();
75 	entryIndex = key->getIndex();
76 	return entryIndex;
77 }
78 
setIndex(long iindex)79 void SWCom::setIndex(long iindex) {
80 	VerseKey *key = &getVerseKey();
81 	key->setTestament(1);
82 	key->setIndex(iindex);
83 
84 	if (key != this->key) {
85 		this->key->copyFrom(*key);
86 	}
87 }
88 
89 
getVerseKey(const SWKey * keyToConvert) const90 VerseKey &SWCom::getVerseKey(const SWKey *keyToConvert) const {
91 	const SWKey *thisKey = keyToConvert ? keyToConvert : this->key;
92 
93 	VerseKey *key = 0;
94 	// see if we have a VerseKey * or decendant
95 	SWTRY {
96 		key = SWDYNAMIC_CAST(VerseKey, thisKey);
97 	}
98 	SWCATCH ( ... ) {	}
99 	if (!key) {
100 		ListKey *lkTest = 0;
101 		SWTRY {
102 			lkTest = SWDYNAMIC_CAST(ListKey, thisKey);
103 		}
104 		SWCATCH ( ... ) {	}
105 		if (lkTest) {
106 			SWTRY {
107 				key = SWDYNAMIC_CAST(VerseKey, lkTest->getElement());
108 			}
109 			SWCATCH ( ... ) {	}
110 		}
111 	}
112 	if (!key) {
113                 VerseKey *retKey = (tmpSecond) ? tmpVK1 : tmpVK2;
114                 tmpSecond = !tmpSecond;
115 		retKey->setLocale(LocaleMgr::getSystemLocaleMgr()->getDefaultLocaleName());
116 		(*retKey) = *(thisKey);
117 		return (*retKey);
118 	}
119 	else	return *key;
120 }
121 
122 
123 SWORD_NAMESPACE_END
124