[sword-svn] r2269 - in trunk: bindings/corba/java include src/keys src/mgr src/utilfuns

scribe at crosswire.org scribe at crosswire.org
Mon Feb 23 23:48:40 MST 2009


Author: scribe
Date: 2009-02-23 23:48:40 -0700 (Mon, 23 Feb 2009)
New Revision: 2269

Modified:
   trunk/bindings/corba/java/Makefile
   trunk/include/swkey.h
   trunk/include/versekey.h
   trunk/src/keys/swkey.cpp
   trunk/src/keys/versekey.cpp
   trunk/src/mgr/swlocale.cpp
   trunk/src/utilfuns/utilstr.cpp
Log:
pushed set/getLocale down to SWKey
changed stdstr to clear our target if null is passed as source


Modified: trunk/bindings/corba/java/Makefile
===================================================================
--- trunk/bindings/corba/java/Makefile	2009-02-19 01:50:49 UTC (rev 2268)
+++ trunk/bindings/corba/java/Makefile	2009-02-24 06:48:40 UTC (rev 2269)
@@ -1,7 +1,8 @@
 TOMCAT_HOME=/opt/tomcat
 #SERVLET_LIB=${TOMCAT_HOME}/common/lib/servlet-api.jar
 SERVLET_LIB=${TOMCAT_HOME}/lib/servlet-api.jar
-instdir=/home/swordweb/livehtml/webapp
+instdir=${TOMCAT_HOME}/webapps/swordweb
+#instdir=/home/swordweb/livehtml/webapp
 #instdir=/home/scribe/src/swordweb/webapp
 all: src/org/crosswire/sword/orb/SWMgr.java classes/org/crosswire/sword/orb/SwordOrb.class
 

Modified: trunk/include/swkey.h
===================================================================
--- trunk/include/swkey.h	2009-02-19 01:50:49 UTC (rev 2268)
+++ trunk/include/swkey.h	2009-02-24 06:48:40 UTC (rev 2269)
@@ -27,6 +27,7 @@
 #include <swobject.h>
 
 #include <defs.h>
+#include <utilstr.h>
 
 SWORD_NAMESPACE_START
 
@@ -66,15 +67,37 @@
 #define TOP SW_POSITION(POS_TOP)
 #define BOTTOM SW_POSITION(POS_BOTTOM)
 
+class SWLocale;
+
 /** SWKey is used for positioning an SWModule to a specific entry.
  *	It always represents a possible location into a module and can additionally represent
  *	a domain of entries (e.g. "John 3:16" in the domain "John 1:1 - Mark 5:25")
  */
 class SWDLLEXPORT SWKey : public SWObject {
+
+	class LocaleCache {
+	public:
+		char *name;
+		SWLocale *locale;
+			LocaleCache() {
+			name = 0;
+			locale = 0;
+		}
+		 virtual ~LocaleCache() {
+			if (name)
+			delete[]name;
+		}
+	};
+	static LocaleCache localeCache;
+	// for caching; don't use directly, call getPrivateLocale()
+	mutable SWLocale *locale;
+
+
 	long index;
 	static SWClass classdef;
 	void init();
 
+
 protected:
 	char *keytext;
 	mutable char *rangeText;
@@ -82,6 +105,10 @@
 	char persist;
 	char error;
 
+	char *localeName;
+	SWLocale *getPrivateLocale() const;
+
+
 public:
 
 	// misc pointer for whatever
@@ -183,6 +210,9 @@
 	 */
 	virtual bool isTraversable() const { return false; }
 
+	char *getLocale() const { return localeName; }
+	void setLocale(const char *name) { stdstr(&localeName, name); locale = 0;	} // this will force an on demand lookup of our locale
+
 	/** Use this function to get an index position within a module.
 	 * Here's a small example how to use this function and @ref Index(long).
 	 * This function uses the GerLut module and chooses a random verse from the

Modified: trunk/include/versekey.h
===================================================================
--- trunk/include/versekey.h	2009-02-19 01:50:49 UTC (rev 2268)
+++ trunk/include/versekey.h	2009-02-24 06:48:40 UTC (rev 2269)
@@ -42,43 +42,21 @@
 
 
 
-class SWLocale;
-
-
 /**
  * Class VerseKey
  * The SWKey implementation used for verse based modules like Bibles or commentaries.
  */
 class SWDLLEXPORT VerseKey : public SWKey {
 
-	class LocaleCache {
-	public:
-		char *name;
-		SWLocale *locale;
-			LocaleCache() {
-			name = 0;
-			locale = 0;
-		}
-		 virtual ~LocaleCache() {
-			if (name)
-			delete[]name;
-		}
-	};
-
 	static SWClass classdef;
 
-	static long *offsets[2][2];
-	static int offsize[2][2];
 	/** number of instantiated VerseKey objects or derivitives
 	*/
 	static int instance;
-	static LocaleCache localeCache;
 	ListKey internalListKey;
 
 	const VerseMgr::System *refSys;
 
-	char *locale;
-
 	/** The Testament: 0 - Module Heading; 1 - Old; 2 - New
 	*/
 	signed char testament;
@@ -119,10 +97,11 @@
 	*/
 	int findindex(long *array, int size, long value);
 
+	void validateCurrentLocale() const;
+
 	mutable long lowerBound, upperBound;
 	mutable VerseKey *tmpClone;
 
-
 protected:
 
 	/************************************************************************
@@ -132,7 +111,7 @@
 	 * ENT:	@param abbr - key for which to search;
 	 * RET:	@return book number or < 0 = not valid
 	 */
-	virtual int getBookAbbrev(const char *abbr);
+	virtual int getBookAbbrev(const char *abbr) const;
 
 	/** Refresh keytext based on testament|book|chapter|verse
 	* default auto normalization to true
@@ -435,11 +414,8 @@
 
 	virtual void setVersificationSystem(const char *name);
 	virtual const char *getVersificationSystem() const;
-	virtual void setLocale(const char *name);
-	virtual const char *getLocale() const { return locale; }
 
 
-
 	// OPERATORS --------------------------------------------------------------------
 
 

Modified: trunk/src/keys/swkey.cpp
===================================================================
--- trunk/src/keys/swkey.cpp	2009-02-19 01:50:49 UTC (rev 2268)
+++ trunk/src/keys/swkey.cpp	2009-02-24 06:48:40 UTC (rev 2269)
@@ -24,11 +24,13 @@
 #include <swkey.h>
 #include <utilstr.h>
 #include <string.h>
+#include <localemgr.h>
 
 SWORD_NAMESPACE_START
 
 static const char *classes[] = {"SWKey", "SWObject", 0};
 SWClass SWKey::classdef(classes);
+SWKey::LocaleCache   SWKey::localeCache;
 
 /******************************************************************************
  * SWKey Constructor - initializes instance of SWKey
@@ -38,6 +40,7 @@
 
 SWKey::SWKey(const char *ikey)
 {
+	init();
 	index     = 0;
 	persist   = 0;
 	keytext   = 0;
@@ -45,11 +48,12 @@
 	error     = 0;
 	userData  = 0;
 	stdstr(&keytext, ikey);
-	init();
 }
 
 SWKey::SWKey(SWKey const &k)
 {
+	init();
+	stdstr(&localeName, k.localeName);
 	index     = k.index;
 	persist   = k.persist;
 	userData  = k.userData;
@@ -57,12 +61,14 @@
 	rangeText = 0;
 	error     = k.error;
 	setText(k.getText());
-	init();
 }
 
 void SWKey::init() {
 	myclass = &classdef;
 	boundSet = false;
+	locale = 0;
+	localeName = 0;
+	setLocale(LocaleMgr::getSystemLocaleMgr()->getDefaultLocaleName());
 }
 
 SWKey *SWKey::clone() const
@@ -75,10 +81,9 @@
  */
 
 SWKey::~SWKey() {
-	if (keytext)
-		delete [] keytext;
-	if (rangeText)
-		delete [] rangeText;
+	delete [] keytext;
+	delete [] rangeText;
+	delete [] localeName;
 }
 
 
@@ -97,6 +102,28 @@
 
 
 /******************************************************************************
+ * SWKey::getPrivateLocale - Gets a real locale object from our name
+ *
+ * RET:	locale object associated with our name
+ */
+
+SWLocale *SWKey::getPrivateLocale() const {
+	if (!locale) {
+		if ((!localeCache.name) || (strcmp(localeCache.name, localeName))) {
+			stdstr(&(localeCache.name), localeName);
+			// this line is the entire bit of work we're trying to avoid with the cache
+			// worth it?  compare time examples/cmdline/search KJV "God love world" to
+			// same with this method reduced to:
+			// if (!local) local = ... (call below); return locale;
+			localeCache.locale = LocaleMgr::getSystemLocaleMgr()->getLocale(localeName);
+		}
+		locale = localeCache.locale;
+	}
+	return locale;
+}
+
+
+/******************************************************************************
  * SWKey::Persist - Set/gets whether this object itself persists within a
  *			module that it was used to setKey or just a copy.
  *			(1 - persists in module; 0 - a copy is attempted
@@ -150,6 +177,7 @@
 
 void SWKey::copyFrom(const SWKey &ikey) {
 // not desirable	Persist(ikey.Persist());
+	setLocale(ikey.getLocale());
 	setText((const char *)ikey);
 }
 

Modified: trunk/src/keys/versekey.cpp
===================================================================
--- trunk/src/keys/versekey.cpp	2009-02-19 01:50:49 UTC (rev 2268)
+++ trunk/src/keys/versekey.cpp	2009-02-24 06:48:40 UTC (rev 2269)
@@ -30,7 +30,6 @@
 #include <swkey.h>
 #include <swlog.h>
 #include <versekey.h>
-#include <localemgr.h>
 #include <swlocale.h>
 #include <roman.h>
 #include <versemgr.h>
@@ -45,7 +44,6 @@
  */
 
 int           VerseKey::instance       = 0;
-VerseKey::LocaleCache   VerseKey::localeCache;
 
 
 /******************************************************************************
@@ -66,10 +64,8 @@
 	chapter = 1;
 	verse = 1;
 	suffix = 0;
-	locale = 0;
 	tmpClone = 0;
 
-	setLocale(LocaleMgr::getSystemLocaleMgr()->getDefaultLocaleName());
 	setVersificationSystem("KJV");
 }
 
@@ -157,7 +153,7 @@
 	chapter = ikey.Chapter();
 	verse = ikey.Verse();
 	suffix = ikey.getSuffix();
-	setLocale(getLocale());
+	setLocale(ikey.getLocale());
 	if (ikey.isBoundSet()) {
 		LowerBound(ikey.LowerBound());
 		UpperBound(ikey.UpperBound());
@@ -212,7 +208,6 @@
 
 VerseKey::~VerseKey() {
 
-	delete [] locale;
 	delete tmpClone;
 
 	--instance;
@@ -233,24 +228,8 @@
 
 const char *VerseKey::getVersificationSystem() const { return refSys->getName(); }
 
-void VerseKey::setLocale(const char *name) {
-	bool useCache = false;
 
-	if (localeCache.name)
-		useCache = (!strcmp(localeCache.name, name));
 
-	if (!useCache)	{	// if we're setting params for a new locale
-		stdstr(&(localeCache.name), name);
-	}
-
-	SWLocale *locale = (useCache) ? localeCache.locale : LocaleMgr::getSystemLocaleMgr()->getLocale(name);
-	localeCache.locale = locale;
-
-//	setBookAbbrevs((locale)?locale->getBookAbbrevs():builtin_abbrevs, localeCache.abbrevsCnt);
-	stdstr(&(this->locale), localeCache.name);
-}
-
-
 /******************************************************************************
  * VerseKey::parse - parses keytext into testament|book|chapter|verse
  *
@@ -350,24 +329,16 @@
  * RET:	book number or < 0 = not valid
  */
 
-int VerseKey::getBookAbbrev(const char *iabbr)
+int VerseKey::getBookAbbrev(const char *iabbr) const
 {
 	int diff, abLen, min, max, target, retVal = -1;
 
 	char *abbr = 0;
 
 	int abbrevsCnt;
-	bool useCache = false;
 
-	useCache = (localeCache.name && localeCache.locale);
+	const struct abbrev *abbrevs = getPrivateLocale()->getBookAbbrevs(&abbrevsCnt);
 
-	if (!useCache)	{	// if we're setting params for a new locale
-		stdstr(&(localeCache.name), locale);
-		localeCache.locale = LocaleMgr::getSystemLocaleMgr()->getLocale(locale);
-	}
-
-	const struct abbrev *abbrevs = localeCache.locale->getBookAbbrevs(&abbrevsCnt);
-
 	StringMgr* stringMgr = StringMgr::getSystemStringMgr();
 	const bool hasUTF8Support = StringMgr::hasUTF8Support();
 
@@ -424,6 +395,42 @@
 
 
 /******************************************************************************
+ * VerseKey::validateCurrentLocale - be sure a locale book abbrevs set is complete
+ *
+ */
+void VerseKey::validateCurrentLocale() const {
+	if (SWLog::getSystemLog()->getLogLevel() >= SWLog::LOG_DEBUG) { //make sure log is wanted, this loop stuff costs a lot of time
+		for (int t = 0; t < 2; t++) {
+			for (int i = 0; i < BMAX[t]; i++) {
+				const int bn = getBookAbbrev(
+						getPrivateLocale()->translate(refSys->getBook(((t>1)?BMAX[0]:0)+i-1)->getLongName())
+					);
+				if ((bn)%39 != i) {
+					char *abbr = 0;
+					stdstr(&abbr, 
+						getPrivateLocale()->translate(refSys->getBook(((t>1)?BMAX[0]:0)+i-1)->getLongName()), 2);
+					strstrip(abbr);
+					SWLog::getSystemLog()->logDebug("VerseKey::Book: %s does not have a matching toupper abbrevs entry! book number returned was: %d(%d). Required entry should be:",
+						abbr, bn, i);
+
+					StringMgr* stringMgr = StringMgr::getSystemStringMgr();
+					const bool hasUTF8Support = StringMgr::hasUTF8Support();
+					if (hasUTF8Support) { //we have support for UTF-8 handling; we expect UTF-8 encoded locales
+						stringMgr->upperUTF8(abbr, strlen(abbr)*2);
+					}
+					else {
+						stringMgr->upperLatin1(abbr);
+					}
+					SWLog::getSystemLog()->logDebug("%s=%d", abbr, (t*39)+i+1);
+					delete [] abbr;
+				}
+			}
+		}
+	}
+}
+
+
+/******************************************************************************
  * VerseKey::ParseVerseList - Attempts to parse a buffer into separate
  *				verse entries returned in a ListKey
  *
@@ -1015,15 +1022,7 @@
 
 
 const char *VerseKey::getBookName() const {
-	bool useCache = false;
-
-	useCache = (localeCache.name && localeCache.locale);
-
-	if (!useCache)	{	// if we're setting params for a new locale
-		stdstr(&(localeCache.name), locale);
-		localeCache.locale = LocaleMgr::getSystemLocaleMgr()->getLocale(locale);
-	}
-	return localeCache.locale->translate(refSys->getBook(((testament>1)?BMAX[0]:0)+book-1)->getLongName());
+	return getPrivateLocale()->translate(refSys->getBook(((testament>1)?BMAX[0]:0)+book-1)->getLongName());
 }
 
 

Modified: trunk/src/mgr/swlocale.cpp
===================================================================
--- trunk/src/mgr/swlocale.cpp	2009-02-19 01:50:49 UTC (rev 2268)
+++ trunk/src/mgr/swlocale.cpp	2009-02-24 06:48:40 UTC (rev 2269)
@@ -151,33 +151,21 @@
 	*localeSource += *addFrom.localeSource;
 }
 
-//#define NONNUMERICLOCALETHING 1
 
 const struct abbrev *SWLocale::getBookAbbrevs(int *retSize) {
 	static const char *nullstr = "";
 	if (!bookAbbrevs) {
 		ConfigEntMap::iterator it;
-		int i, j;
+		int i;
 		int size = localeSource->Sections["Book Abbrevs"].size();
 		bookAbbrevs = new struct abbrev[size + 1];
-		for (i = 0, j = 0, it = localeSource->Sections["Book Abbrevs"].begin(); it != localeSource->Sections["Book Abbrevs"].end(); it++, i++) {
-			#ifdef NONNUMERICLOCALETHING
-			int booknum = VerseKey::getOSISBookNum((*it).second.c_str());
-			if (booknum != -1) {
-				bookAbbrevs[j].ab = (*it).first.c_str();
-				bookAbbrevs[j].book = booknum;
-				j++;
-			}
-			#else
+		for (i = 0, it = localeSource->Sections["Book Abbrevs"].begin(); it != localeSource->Sections["Book Abbrevs"].end(); it++, i++) {
 			bookAbbrevs[i].ab = (*it).first.c_str();
 			bookAbbrevs[i].osis = (*it).second.c_str();
-			j++;
-			#endif
-			//printf("SWLocale::getBookAbbrevs %s:%s %d\n",bookAbbrevs[i].ab,
-			//	(*it).second.c_str(), bookAbbrevs[i].book); 
 		}
-		bookAbbrevs[j].ab = nullstr;
-		bookAbbrevs[j].osis = nullstr;
+
+		bookAbbrevs[i].ab = nullstr;
+		bookAbbrevs[i].osis = nullstr;
 		abbrevsCnt = size;
 	}
 		

Modified: trunk/src/utilfuns/utilstr.cpp
===================================================================
--- trunk/src/utilfuns/utilstr.cpp	2009-02-19 01:50:49 UTC (rev 2268)
+++ trunk/src/utilfuns/utilstr.cpp	2009-02-24 06:48:40 UTC (rev 2269)
@@ -84,13 +84,14 @@
  */
 
 char *stdstr(char **ipstr, const char *istr, unsigned int memPadFactor) {
+	if (*ipstr)
+		delete [] *ipstr;
 	if (istr) {
-		if (*ipstr)
-			delete [] *ipstr;
 		int len = strlen(istr) + 1;
 		*ipstr = new char [ len * memPadFactor ];
 		memcpy(*ipstr, istr, len);
 	}
+	else *ipstr = 0;
 	return *ipstr;
 }
 




More information about the sword-cvs mailing list