[sword-cvs] sword/src/mgr filemgr.cpp,1.33,1.34 swconfig.cpp,1.13,1.14

sword@www.crosswire.org sword@www.crosswire.org
Fri, 16 Jan 2004 21:33:27 -0700


Update of /cvs/core/sword/src/mgr
In directory www:/tmp/cvs-serv14607/src/mgr

Modified Files:
	filemgr.cpp swconfig.cpp 
Log Message:
removed fopen and related functions from swconfig and added dependency of
FileMgr
Added a getLine function to filemgr


Index: filemgr.cpp
===================================================================
RCS file: /cvs/core/sword/src/mgr/filemgr.cpp,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -d -r1.33 -r1.34
--- filemgr.cpp	23 Dec 2003 01:36:12 -0000	1.33
+++ filemgr.cpp	17 Jan 2004 04:33:25 -0000	1.34
@@ -352,6 +352,56 @@
 	return ::remove(fName);
 }
 
+char FileMgr::getLine(FileDesc *fDesc, SWBuf &line) {
+	char ch;
+	int len;
+	char *buf;
+	bool more = true;
+
+	line = "";
+	while (more) {
+		more = false;
+		long index = lseek(fDesc->getFd(), 0, SEEK_CUR);
+		// clean up any preceding white space
+		while ((len = read(fDesc->getFd(), &ch, 1)) == 1) {
+			if ((ch != 13) && (ch != ' ') && (ch != '\t'))
+				break;
+			else index++;
+		}
+
+
+		while (ch != 10) {
+		   if ((len = read(fDesc->getFd(), &ch, 1)) != 1)
+				break;
+		}
+		
+		int size = (lseek(fDesc->getFd(), 0, SEEK_CUR) - index) - 1;
+
+		buf = new char [ size + 1 ];
+
+		if (size > 0) {
+			lseek(fDesc->getFd(), index, SEEK_SET);
+			read(fDesc->getFd(), buf, size);
+			read(fDesc->getFd(), &ch, 1);   //pop terminating char
+			buf[size] = 0;
+
+			more = (line[line.length()-1] == '\\');
+			// clean up any trailing junk on buf
+			for (char *it = buf+(strlen(buf)-1); it > buf; it--) {
+				if ((*it != 10) && (*it != 13) && (*it != ' ') && (*it != '\t')) {
+					if (*it == '\\')
+						more = true;
+					else break;
+				}
+				*it = 0;
+			}
+		}
+		else *buf = 0;
+		line += buf;
+		delete [] buf;
+	}
+	return (len || line.length());
+}
 
 
 SWORD_NAMESPACE_END

Index: swconfig.cpp
===================================================================
RCS file: /cvs/core/sword/src/mgr/swconfig.cpp,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- swconfig.cpp	27 Jun 2003 01:41:07 -0000	1.13
+++ swconfig.cpp	17 Jan 2004 04:33:25 -0000	1.14
@@ -22,6 +22,13 @@
 
 #include <swconfig.h>
 #include <utilfuns.h>
+#include <filemgr.h>
+#include <fcntl.h>
+#ifndef __GNUC__
+#include <io.h>
+#else
+#include <unistd.h>
+#endif
 
 SWORD_NAMESPACE_START
 
@@ -35,6 +42,7 @@
 }
 
 
+/*
 char SWConfig::getline(FILE *fp, SWBuf &line)
 {
 	char retval = 0;
@@ -64,10 +72,13 @@
 	}
 	return retval;
 }
+*/
+
+
 
 
 void SWConfig::Load() {
-	FILE *cfile;
+	FileDesc *cfile;
 	char *buf, *data;
 	SWBuf line;
 	ConfigEntMap cursect;
@@ -76,8 +87,9 @@
 	
 	Sections.erase(Sections.begin(), Sections.end());
 	
-	if ((cfile = fopen(filename.c_str(), "r"))) {
-		while (getline(cfile, line)) {
+	cfile = FileMgr::systemFileMgr.open(filename.c_str(), O_RDONLY|O_BINARY);
+	if (cfile->getFd() > 0) {
+		while (FileMgr::getLine(cfile, line)) {
 			buf = new char [ line.length() + 1 ];
 			strcpy(buf, line.c_str());
 			if (*strstrip(buf) == '[') {
@@ -103,35 +115,37 @@
 		if (!first)
 			Sections.insert(SectionMap::value_type(sectname, cursect));
 
-		fclose(cfile);
+		FileMgr::systemFileMgr.close(cfile);
 	}
 }
 
 
 void SWConfig::Save() {
-	FILE *cfile;
+	FileDesc *cfile;
 	SWBuf buf;
 	SectionMap::iterator sit;
 	ConfigEntMap::iterator entry;
 	SWBuf sectname;
 	
-	if ((cfile = fopen(filename.c_str(), "w"))) {
+	cfile = FileMgr::systemFileMgr.open(filename.c_str(), O_BINARY|O_RDWR|O_CREAT|O_TRUNC);
+	if (cfile->getFd() > 0) {
 		
 		for (sit = Sections.begin(); sit != Sections.end(); sit++) {
 			buf =  "\n[";
 			buf += (*sit).first.c_str();
 			buf += "]\n";
-			fputs(buf.c_str(), cfile);
+			write(cfile->getFd(), buf.c_str(), buf.length());
 			for (entry = (*sit).second.begin(); entry != (*sit).second.end(); entry++) {
 				buf = (*entry).first.c_str();
 				buf += "=";
 				buf += (*entry).second.c_str();
 				buf += "\n";
-				fputs(buf.c_str(), cfile);
+				write(cfile->getFd(), buf.c_str(), buf.length());
 			}
 		}
-		fputs("\n", cfile);	// so getline will find last line
-		fclose(cfile);
+		buf = "\n";
+		write(cfile->getFd(), buf.c_str(), buf.length());
+		FileMgr::systemFileMgr.close(cfile);
 	}
 }