[sword-svn] r97 - in trunk/src: Dll1/winceSword/include Dll1/winceSword/src gui

dtrotzjr at www.crosswire.org dtrotzjr at www.crosswire.org
Sat Mar 15 14:21:59 MST 2008


Author: dtrotzjr
Date: 2008-03-15 14:21:57 -0700 (Sat, 15 Mar 2008)
New Revision: 97

Modified:
   trunk/src/Dll1/winceSword/include/swordce.h
   trunk/src/Dll1/winceSword/src/dirent.cpp
   trunk/src/Dll1/winceSword/src/swordce.cpp
   trunk/src/Dll1/winceSword/src/swwinlog.cpp
   trunk/src/Dll1/winceSword/src/unistd.cpp
   trunk/src/gui/ApplicationInterface.cpp
   trunk/src/gui/NavRenderText.cpp
   trunk/src/gui/SimpleNavigator.cpp
   trunk/src/gui/SwordIndex.cpp
Log:
Fixed a memory leak in wstrtostr and strtowstr.

Modified: trunk/src/Dll1/winceSword/include/swordce.h
===================================================================
--- trunk/src/Dll1/winceSword/include/swordce.h	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/Dll1/winceSword/include/swordce.h	2008-03-15 21:21:57 UTC (rev 97)
@@ -5,8 +5,8 @@
 #include <ctype.h>
 #include <defs.h>
 
-SWDLLEXPORT wchar_t *strtowstr(const char *str);
-SWDLLEXPORT char *wstrtostr(const wchar_t *str);
+SWDLLEXPORT wchar_t *strtowstr(wchar_t *dst, const char *src, int size);
+SWDLLEXPORT char *wstrtostr(char *dst, const wchar_t *src, int size);
 SWDLLEXPORT const char *getWorkingDirectory();
 SWDLLEXPORT const char *windizePath(const char *path);
 //int stricmp(const char *s1, const char *s2);

Modified: trunk/src/Dll1/winceSword/src/dirent.cpp
===================================================================
--- trunk/src/Dll1/winceSword/src/dirent.cpp	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/Dll1/winceSword/src/dirent.cpp	2008-03-15 21:21:57 UTC (rev 97)
@@ -9,9 +9,10 @@
 DIR* opendir(const char *pSpec) {
 	DIR *pDir = new DIR;
 	pDir->dirPath = new SWBuf();
+    wchar_t buf[MAX_PATH + 1];
 	*(pDir->dirPath) = windizePath(pSpec);
 	*(pDir->dirPath) += "\\*";
-	pDir->hFind = FindFirstFile(strtowstr(pDir->dirPath->c_str()), &(pDir->wfd));
+	pDir->hFind = FindFirstFile(strtowstr(buf, pDir->dirPath->c_str(), MAX_PATH + 1), &(pDir->wfd));
 	return pDir;
 }
 
@@ -25,11 +26,10 @@
 
 
 struct dirent* readdir(DIR *pDir) {
+    char buf[MAX_PATH + 1];
 	if (pDir->hFind) {
 //MessageBox(0,pDir->wfd.cFileName,L"readdir returning",MB_OK|MB_ICONERROR);
-//strcpy(pDir->de.d_name, "whnu.conf");
-//strcpy(pDir->de.d_name, wstrtostr(L"whnu.conf"));
-		strcpy(pDir->de.d_name, wstrtostr(pDir->wfd.cFileName));
+		strcpy(pDir->de.d_name, wstrtostr(buf, pDir->wfd.cFileName, MAX_PATH + 1));
 		if ( !FindNextFile(pDir->hFind, &(pDir->wfd)) )
 			pDir->hFind = NULL;
 
@@ -41,8 +41,9 @@
 
 
 void rewinddir(DIR* dir) {
+    wchar_t buf[MAX_PATH + 1];
 //MessageBox(0,L"rewinddir",L"STAGE",MB_OK|MB_ICONERROR);
-	dir->hFind = FindFirstFile(strtowstr(dir->dirPath->c_str()), &(dir->wfd));
+	dir->hFind = FindFirstFile(strtowstr(buf, dir->dirPath->c_str(), MAX_PATH + 1), &(dir->wfd));
 }
 
 

Modified: trunk/src/Dll1/winceSword/src/swordce.cpp
===================================================================
--- trunk/src/Dll1/winceSword/src/swordce.cpp	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/Dll1/winceSword/src/swordce.cpp	2008-03-15 21:21:57 UTC (rev 97)
@@ -26,28 +26,26 @@
 }
 
 
-wchar_t *strtowstr(const char *str) {
-  static wchar_t *c, *buffer = 0;
-  if (buffer)
-    delete [] buffer;
-  buffer = c = new wchar_t[ strlen(str) + 1 ];
-  while (*str)
-    *c++ = (wchar_t)*str++;
-  *c = 0;
-//  MessageBox(0, buffer, L"I'm leaving strtowstr()", MB_OK);
-  return buffer;
+wchar_t *strtowstr(wchar_t *dst, const char *src, int size) {
+  int i = 0;
+  while (i < (size - 1) && *src){
+    dst[i] = (wchar_t)src[i];
+    i++;
+  }
+  dst[i] = 0;
+  //MessageBoxW(0, dst, L"I'm leaving strtowstr()", MB_OK);
+  return dst;
 }
 
-char *wstrtostr(const wchar_t *str) {
-//  MessageBox(0, str, L"I'm in wstrtostr()", MB_OK);
-  static char *c, *buffer = 0;
-  if (buffer)
-    delete [] buffer;
-  buffer = c = new char[ wcslen(str) + 1 ];
-  while (*str)
-    *c++ = (char)*str++;
-  *c = 0;
-  return buffer;
+char *wstrtostr(char *dst, const wchar_t *src, int size) {
+//  MessageBox(0, src, L"I'm in wstrtostr()", MB_OK);
+  int i = 0;
+  while (i < (size - 1) && *src){
+    dst[i] = (char)src[i];
+    i++;
+  }
+  dst[i] = 0;
+  return dst;
 }
 
 

Modified: trunk/src/Dll1/winceSword/src/swwinlog.cpp
===================================================================
--- trunk/src/Dll1/winceSword/src/swwinlog.cpp	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/Dll1/winceSword/src/swwinlog.cpp	2008-03-15 21:21:57 UTC (rev 97)
@@ -28,6 +28,9 @@
 void SWWinLog::LogWarning(char *fmt, ...)
 {
 	char msg[2048];
+    const int BUF_SIZE = 512;
+    wchar_t buf[BUF_SIZE];
+
 	va_list argptr;
 
 	if (logLevel >= 2) {
@@ -35,7 +38,7 @@
 		vsprintf(msg, fmt, argptr);
 		va_end(argptr);
 
-		MessageBox(parent, strtowstr(msg), L"Warning", MB_OK);
+		MessageBox(parent, strtowstr(buf, msg, BUF_SIZE), L"Warning", MB_OK);
 	}
 }
 
@@ -43,6 +46,8 @@
 void SWWinLog::LogError(char *fmt, ...)
 {
 	char msg[2048];
+    const int BUF_SIZE = 512;
+    wchar_t buf[BUF_SIZE];
 	va_list argptr;
 
 	if (logLevel) {
@@ -50,7 +55,7 @@
 		vsprintf(msg, fmt, argptr);
 		va_end(argptr);
 
-		MessageBox(parent, strtowstr(msg), L"Error", MB_OK);
+		MessageBox(parent, strtowstr(buf, msg, BUF_SIZE), L"Error", MB_OK);
 	}
 }
 
@@ -58,6 +63,8 @@
 void SWWinLog::LogTimedInformation(char *fmt, ...)
 {
 	char msg[2048];
+    const int BUF_SIZE = 512;
+    wchar_t buf[BUF_SIZE];
 	va_list argptr;
 
 	if (logLevel >= 4) {
@@ -65,7 +72,7 @@
 		vsprintf(msg, fmt, argptr);
 		va_end(argptr);
 
-		MessageBox(parent, strtowstr(msg), L"Information...", MB_OK);
+		MessageBox(parent, strtowstr(buf, msg, BUF_SIZE), L"Information...", MB_OK);
 
 	}
 }
@@ -74,6 +81,8 @@
 void SWWinLog::LogInformation(char *fmt, ...)
 {
 	char msg[2048];
+    const int BUF_SIZE = 512;
+    wchar_t buf[BUF_SIZE];
 	va_list argptr;
 
 	if (logLevel >= 3) {
@@ -81,7 +90,7 @@
 		vsprintf(msg, fmt, argptr);
 		va_end(argptr);
 
-		MessageBox(parent, strtowstr(msg), L"Information", MB_OK);
+		MessageBox(parent, strtowstr(buf, msg, BUF_SIZE), L"Information", MB_OK);
 	}
 }
 

Modified: trunk/src/Dll1/winceSword/src/unistd.cpp
===================================================================
--- trunk/src/Dll1/winceSword/src/unistd.cpp	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/Dll1/winceSword/src/unistd.cpp	2008-03-15 21:21:57 UTC (rev 97)
@@ -25,6 +25,7 @@
 //MessageBox(0,strtowstr(path),L"open",MB_OK|MB_ICONERROR);
   bool exists = false;
   const char *winPath = windizePath(path);
+  wchar_t wbuf[MAX_PATH + 1];
   DWORD access = 0;
   DWORD create = OPEN_EXISTING;
   DWORD share = FILE_SHARE_READ;
@@ -41,7 +42,7 @@
   
   // Quick test to see if file exists, if it does not exist this call fails.
   
-  HANDLE hEx = CreateFile(strtowstr(winPath), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+  HANDLE hEx = CreateFile(strtowstr(wbuf, winPath, MAX_PATH + 1), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
   if(hEx != INVALID_HANDLE_VALUE){
 	  CloseHandle(hEx);
 	  exists = true;
@@ -60,7 +61,7 @@
 //  if (mode & O_BINARY)
 
   HANDLE *result = new HANDLE;
-  *result = CreateFile(strtowstr(winPath), access, share, NULL, create, FILE_ATTRIBUTE_NORMAL, NULL);
+  *result = CreateFile(strtowstr(wbuf, winPath, MAX_PATH + 1), access, share, NULL, create, FILE_ATTRIBUTE_NORMAL, NULL);
 
 
 //  int result=(int)fopen(winPath, cmode);
@@ -91,11 +92,11 @@
 
 int read(int fd, void *buf, size_t count) {
 	DWORD readCount;
-//__try {
+__try {
 	ReadFile(*(HANDLE *)fd, buf, (DWORD)count, &readCount, NULL);
-//} __except (EXCEPTION_EXECUTE_HANDLER) {
-//	return 0;
-//}
+} __except (EXCEPTION_EXECUTE_HANDLER) {
+	return 0;
+}
 	return (int)readCount;
 
 }
@@ -136,8 +137,9 @@
 
 int access(const char* path, int mode) {
 	int retVal = 0;
+    wchar_t buf[MAX_PATH + 1];
 
-	DWORD attribs = GetFileAttributes(strtowstr(windizePath(path)));
+	DWORD attribs = GetFileAttributes(strtowstr(buf, windizePath(path), MAX_PATH + 1));
 
 	if (attribs == 0xFFFFFFFF)
 		retVal = 1;
@@ -155,9 +157,9 @@
 
 int stat(const char *path, struct stat *s) {
 	int retVal = 0;
+    wchar_t buf[MAX_PATH + 1];
+	DWORD attribs = GetFileAttributes(strtowstr(buf,windizePath(path),MAX_PATH + 1));
 
-	DWORD attribs = GetFileAttributes(strtowstr(windizePath(path)));
-
 	if (attribs == 0xFFFFFFFF) {
 		retVal = 1;
 	}

Modified: trunk/src/gui/ApplicationInterface.cpp
===================================================================
--- trunk/src/gui/ApplicationInterface.cpp	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/gui/ApplicationInterface.cpp	2008-03-15 21:21:57 UTC (rev 97)
@@ -22,7 +22,7 @@
 	memset(&fontSpecs,0,sizeof(fontSpecs));
 	fontSpecs.lfHeight=-10;
 	fontSpecs.lfWeight=200;
-	fontSpecs.lfPitchAndFamily=FF_ROMAN;
+	fontSpecs.lfPitchAndFamily=FF_SWISS;
 	fontSpecs.lfQuality=5;
 	navFont=CreateFontIndirect(&fontSpecs);
 

Modified: trunk/src/gui/NavRenderText.cpp
===================================================================
--- trunk/src/gui/NavRenderText.cpp	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/gui/NavRenderText.cpp	2008-03-15 21:21:57 UTC (rev 97)
@@ -33,14 +33,7 @@
 	if (!(navigator->chapterCache)) {
 		textControl->clearText();
 		textControl->hide();
-		// FIX ME
-        //__try {
-			load();
-		//} __except(EXCEPTION_EXECUTE_HANDLER) { // it is not known if this still works like this
-		//	textControl->clearText();
-		//	textControl->addText(UString(L"Error: Not enough memory available"));
-		//	textControl->endOfText();
-		//}
+		load();
 		textControl->show();
 		if (verse!=1)
 			textControl->gotoAnchor(verse);

Modified: trunk/src/gui/SimpleNavigator.cpp
===================================================================
--- trunk/src/gui/SimpleNavigator.cpp	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/gui/SimpleNavigator.cpp	2008-03-15 21:21:57 UTC (rev 97)
@@ -259,7 +259,11 @@
 }
 
 void SimpleNavigator::urlClicked(const WCHAR *target) {   
-    const char * t = wstrtostr(target);
+    const int BUF_SIZE = 2048;
+    char target_cstr[BUF_SIZE];
+    const char * t = wstrtostr(target_cstr, target, BUF_SIZE);
+    wchar_t buf[BUF_SIZE];
+
 	SWKey *modKey = 0;
 	// MessageBox(0, strtowstr(t), L"This is 't'", MB_OK);
 	// SWMgr swmanager; // don't create new manager - use SwordIndex::manager
@@ -297,14 +301,14 @@
 		if(strstr(type.c_str(), "Hebrew")) 
 			mod = SwordIndex::hebrewLex;
 		if (! mod) {
-			MessageBox(0, strtowstr("No Dictionary found - install Strong's modules"), 
+			MessageBox(0, L"No Dictionary found - install Strong's modules", 
 				L"Strong's", MB_OK);
 			return;
 		}
 		modKey = mod->getKey(); 
 		modKey->setText(number.c_str()); 
 		mod->RenderText(); 
-		MessageBox(0, strtowstr(mod->StripText()), L"Strong's", MB_OK);
+		MessageBox(0, strtowstr(buf, mod->StripText(), BUF_SIZE), L"Strong's", MB_OK);
 		}
 	// Morph MessageBox
 	else if (strstr(t, "showMorph")) {
@@ -315,7 +319,7 @@
 		if(strstr(type.c_str(), "robinson")) {
 			mod = SwordIndex::greekMorph;
 			if (! mod) {
-			MessageBox(0, strtowstr("No Greek parsing Dictionary found - install Morphological modules"), 
+			MessageBox(0, L"No Greek parsing Dictionary found - install Morphological modules", 
 				L"Greek Morphological tags", MB_ICONWARNING);
 			return;
 			}
@@ -323,7 +327,7 @@
 		if(strstr(type.c_str(), "strongMorph")) {
 			mod = SwordIndex::hebrewMorph;
 			if (! mod) {
-			MessageBox(0, strtowstr("No Hebrew parsing Dictionary found - install Morphological modules"), 
+			MessageBox(0, L"No Hebrew parsing Dictionary found - install Morphological modules", 
 				L"Hebrew Morphological tags", MB_ICONWARNING);
 			return;
 			}
@@ -331,7 +335,7 @@
 		modKey = mod->getKey(); 
 		modKey->setText(number.c_str()); 
 		mod->RenderText(); 
-		MessageBox(0, strtowstr(mod->StripText()), L"Morph tags", MB_OK);
+		MessageBox(0, strtowstr(buf, mod->StripText(), BUF_SIZE), L"Morph tags", MB_OK);
 	}
 	// X-Ref MessageBox
 	else if (strstr(t, "scripRef")) {
@@ -342,7 +346,7 @@
 		SWModule *mod = SwordIndex::manager->getModule((*g_swordConf)["History"]["LastBible"]);
 		// will fail if user has never looked at a bible - possible??? so check ....
 		if (!mod) {
-			MessageBox(0, strtowstr("xRef bible not found"), 
+			MessageBox(0, L"xRef bible not found", 
 				L"Cross referencing", MB_ICONWARNING);
 			return;
 			}
@@ -352,11 +356,12 @@
 		// strip out html if present
 		SWBuf body = mod->StripText();
 		// not sure if MessageBox can display html - if it can, re-think this some day
-		MessageBox(0, strtowstr(body), L"Scripture reference", MB_OK);
+		MessageBox(0, strtowstr(buf, body, BUF_SIZE), L"Scripture reference", MB_OK);
 		}
-//comment next line out in release version
-else MessageBox(0, strtowstr(t), L"Raw entry unresolved", MB_OK); // catch all 
-modKey->setText((const char *)oldPos.c_str()); // restore the key before leaving
+        
+        else //comment next line out in release version
+            MessageBox(0, strtowstr(buf, t, BUF_SIZE), L"Raw entry unresolved", MB_OK); // catch all 
+        modKey->setText((const char *)oldPos.c_str()); // restore the key before leaving
 }
 
 

Modified: trunk/src/gui/SwordIndex.cpp
===================================================================
--- trunk/src/gui/SwordIndex.cpp	2008-03-15 19:10:41 UTC (rev 96)
+++ trunk/src/gui/SwordIndex.cpp	2008-03-15 21:21:57 UTC (rev 97)
@@ -57,43 +57,37 @@
 }
 
 void SwordIndex::initManager() {
-	if (indices==0) { //We need a manager
-		texts=new ModuleMap();
-        // FIX ME
-		//__try {
-			manager=new sword::SWMgr(new sword::MarkupFilterMgr(sword::FMT_HTMLHREF, sword::ENC_UTF16));
+    if (indices==0) { //We need a manager
+        texts=new ModuleMap();
 
-			sword::ModMap::iterator it;
-			sword::SWModule* curMod = 0;
-			for (it = manager->Modules.begin(); it != manager->Modules.end(); it++) {
-				curMod = (*it).second;
-				if (!strcmp(curMod->Type(), "Biblical Texts")) {
-					(*texts)[curMod->Name()]=curMod;
-				}
-				// next section added BD - Nov 07
-				if (!strcmp(curMod->Type(), "Commentaries")) {
-					(*texts)[curMod->Name()]=curMod;
-				}			// end of added code
-				if (curMod->getConfig().has("Feature", "GreekDef")) {
-					greekLex = curMod;
-				}
-				if (curMod->getConfig().has("Feature", "GreekParse")) {
-					greekMorph = curMod;
-				}
-				if (curMod->getConfig().has("Feature", "HebrewDef")) {
-					hebrewLex = curMod;
-				}
-				if (curMod->getConfig().has("Feature", "HebrewParse")) {
-					hebrewMorph = curMod;
-				}
-			}
-		//} 
-	//	__except (EXCEPTION_EXECUTE_HANDLER) {
-	//		manager=0;
-	//		texts->clear();
-	//	};
-	}
-	indices++;
+        manager=new sword::SWMgr(new sword::MarkupFilterMgr(sword::FMT_HTMLHREF, sword::ENC_UTF16));
+
+        sword::ModMap::iterator it;
+        sword::SWModule* curMod = 0;
+        for (it = manager->Modules.begin(); it != manager->Modules.end(); it++) {
+            curMod = (*it).second;
+            if (!strcmp(curMod->Type(), "Biblical Texts")) {
+                (*texts)[curMod->Name()]=curMod;
+            }
+            // next section added BD - Nov 07
+            if (!strcmp(curMod->Type(), "Commentaries")) {
+                (*texts)[curMod->Name()]=curMod;
+            }			// end of added code
+            if (curMod->getConfig().has("Feature", "GreekDef")) {
+                greekLex = curMod;
+            }
+            if (curMod->getConfig().has("Feature", "GreekParse")) {
+                greekMorph = curMod;
+            }
+            if (curMod->getConfig().has("Feature", "HebrewDef")) {
+                hebrewLex = curMod;
+            }
+            if (curMod->getConfig().has("Feature", "HebrewParse")) {
+                hebrewMorph = curMod;
+            }
+        }
+    }
+    indices++;
 }
 
 void SwordIndex::leaveManager() {




More information about the sword-cvs mailing list