r3535 - in trunk: libmnetutil/source libmutil/include/libmutil libmutil/source
erik at minisip.org
erik at minisip.org
Wed Nov 28 17:01:59 CET 2007
Author: erik
Date: 2007-11-28 17:01:58 +0100 (Wed, 28 Nov 2007)
New Revision: 3535
Modified:
trunk/libmnetutil/source/HttpFileSystem.cxx
trunk/libmutil/include/libmutil/FileSystem.h
trunk/libmutil/include/libmutil/XMLParser.h
trunk/libmutil/source/FileSystem.cxx
trunk/libmutil/source/XMLParser.cxx
Log:
libmutil
* Updated FileSystem API
* Made XMLParser able to use files on any FS type.
libmnetutil:
* Updated HttpFileSystem. Note that this FS needs testing
Modified: trunk/libmnetutil/source/HttpFileSystem.cxx
===================================================================
--- trunk/libmnetutil/source/HttpFileSystem.cxx 2007-11-28 15:38:40 UTC (rev 3534)
+++ trunk/libmnetutil/source/HttpFileSystem.cxx 2007-11-28 16:01:58 UTC (rev 3535)
@@ -9,9 +9,11 @@
class FileString : public File {
public:
FileString(char *data, int len, MRef<StreamSocket*> ssock);
- virtual void read(void *buf, int32_t count);
+ virtual int32_t read(void *buf, int32_t count);
virtual void write(void *buf, int32_t count);
- virtual void seek(int32_t pos );
+ virtual bool eof();
+ virtual void seek(int64_t pos);
+ virtual int64_t offset();
virtual int64_t size();
virtual void flush();
@@ -30,12 +32,13 @@
allocSize(len), ssock(ssock_) { }
-void FileString::read(void *buf, int32_t count){
- if ( count+curpos > len ){
- //count = len-curpos;
- throw FileException("Read beyond end of file");
+int32_t FileString::read(void *buf, int32_t count){
+ if ( count+curpos > len ) {
+ count = len-curpos;
}
memcpy(buf, &data[curpos], count);
+ curpos+=count;
+ return count;
}
void FileString::write(void *buf, int32_t count){
@@ -51,18 +54,27 @@
len = curpos;
}
+bool FileString::eof(){
+ return curpos>=len;
+}
-void FileString::seek(int32_t pos ){
+
+void FileString::seek(int64_t pos ){
curpos=pos;
if (curpos>len)
curpos=len;
}
+int64_t FileString::offset(){
+ return curpos;
+}
+
int64_t FileString::size(){
return len;
}
void FileString::flush(){
+ //TODO: FIXME: upload to Webdav/HTTP server not implemented
}
HttpFileSystem::HttpFileSystem(MRef<StreamSocket*> conn_, string prefix_) :
@@ -75,7 +87,7 @@
MRef<File*> HttpFileSystem::open( const std::string& path, bool createIfNotExist ){
char *data=NULL;
int len=0;
- HttpDownloader dl( "www.minisip.org/~erik/data.bin", conn );
+ HttpDownloader dl( path , conn ); //FIXME: Check if "path" makes sense to send here
data = dl.getChars(&len);
return new FileString( data, len, conn);
}
Modified: trunk/libmutil/include/libmutil/FileSystem.h
===================================================================
--- trunk/libmutil/include/libmutil/FileSystem.h 2007-11-28 15:38:40 UTC (rev 3534)
+++ trunk/libmutil/include/libmutil/FileSystem.h 2007-11-28 16:01:58 UTC (rev 3535)
@@ -48,9 +48,26 @@
*/
class LIBMUTIL_API File : public MObject {
public:
- virtual void read(void *buf, int32_t count) = 0;
+ /**
+ * @return Number of bytes read. This can be less than the
+ * requested number of bytes in case EOF is reached.
+ */
+ virtual int32_t read(void *buf, int32_t count) = 0;
virtual void write(void *buf, int32_t count) = 0;
- virtual void seek(int32_t pos ) = 0;
+
+ virtual bool eof() = 0;
+
+ /**
+ * Sets the read position offset from the beginning of the
+ * file.
+ */
+ virtual void seek(int64_t pos ) = 0;
+
+ /**
+ * @return Current read position from the start of the file
+ * in bytes.
+ */
+ virtual int64_t offset() = 0;
virtual int64_t size() = 0;
virtual void flush() = 0;
};
Modified: trunk/libmutil/include/libmutil/XMLParser.h
===================================================================
--- trunk/libmutil/include/libmutil/XMLParser.h 2007-11-28 15:38:40 UTC (rev 3534)
+++ trunk/libmutil/include/libmutil/XMLParser.h 2007-11-28 16:01:58 UTC (rev 3535)
@@ -30,6 +30,7 @@
#include<libmutil/mtypes.h>
#include<libmutil/MemObject.h>
#include<libmutil/Exception.h>
+#include<libmutil/FileSystem.h>
#include<list>
@@ -120,9 +121,13 @@
class LIBMUTIL_API XMLFileParser : public XMLParser{
public:
XMLFileParser(std::string filename="", XMLParserCallback *cb=NULL);
+ XMLFileParser(std::string filename, MRef<FileSystem*> fs, XMLParserCallback *cb=NULL);
void saveToFile(std::string file="");
private:
+ void init();
+
std::string filename;
+ MRef<FileSystem*> fs;
};
class LIBMUTIL_API XMLstringParser : public XMLParser{
Modified: trunk/libmutil/source/FileSystem.cxx
===================================================================
--- trunk/libmutil/source/FileSystem.cxx 2007-11-28 15:38:40 UTC (rev 3534)
+++ trunk/libmutil/source/FileSystem.cxx 2007-11-28 16:01:58 UTC (rev 3535)
@@ -18,9 +18,11 @@
class LocalFile : public File {
public:
LocalFile(string path);
- virtual void read(void *buf, int32_t count);
+ virtual int32_t read(void *buf, int32_t count);
virtual void write(void *buf, int32_t count);
- virtual void seek(int32_t pos );
+ virtual bool eof();
+ virtual void seek(int64_t pos );
+ virtual int64_t offset();
virtual int64_t size();
virtual void flush();
@@ -38,18 +40,28 @@
}
}
-void LocalFile::read(void *buf, int32_t count){
+int32_t LocalFile::read(void *buf, int32_t count){
+ int64_t spos = offset();
file.read( (char*)buf, count );
+ return (int32_t) ( offset() - spos );
}
void LocalFile::write(void *buf, int32_t count){
file.write( (char*)buf, count);
}
-void LocalFile::seek(int32_t pos ){
+bool LocalFile::eof(){
+ return !file;
+}
+
+void LocalFile::seek(int64_t pos ){
file.seekg( pos, ios::beg );
}
+int64_t LocalFile::offset(){
+ return file.tellg();
+}
+
int64_t LocalFile::size(){
int64_t tmp = file.tellg(); // save position
file.seekg(0, std::ios_base::beg);
Modified: trunk/libmutil/source/XMLParser.cxx
===================================================================
--- trunk/libmutil/source/XMLParser.cxx 2007-11-28 15:38:40 UTC (rev 3534)
+++ trunk/libmutil/source/XMLParser.cxx 2007-11-28 16:01:58 UTC (rev 3535)
@@ -202,6 +202,10 @@
}
XMLFileParser::XMLFileParser(string filename_, XMLParserCallback *cb):XMLParser(cb), filename(filename_){
+
+ fs = new LocalFileSystem;
+
+ init();
string s = "";
if (filename != ""){
@@ -225,6 +229,44 @@
parsestring(s);
}
+XMLFileParser::XMLFileParser(string filename_, MRef<FileSystem*> fs_, XMLParserCallback *cb) :
+ XMLParser(cb),
+ filename(filename_), fs(fs_)
+{
+ init();
+}
+
+void XMLFileParser::init(){
+
+ string s = "";
+ if (filename != ""){
+ MRef<File*> file;
+ try{
+ file = fs->open(filename);
+ ifstream file(filename.c_str());
+ }catch(const FileException &e){
+ throw XMLFileNotFound( "Could not read file " + filename );
+ }
+
+
+ int32_t bufsize=20;
+ char *buf = (char *)calloc(bufsize,1);
+ do{
+ for (int32_t i=0; i<bufsize; i++)
+ buf[i]=0;
+ //file.read(buf,bufsize-1);
+ file->read(buf, bufsize-1);
+ s = s+string(buf);
+ }while(/*!(!file)*/ !file->eof());
+ free(buf);
+
+ }
+ parsestring(s);
+
+
+}
+
+
static XMLNode *parseAttribute(const char *s, int32_t &i){
// cerr << "parseAttribute with s="<<s+i<<" and i=" << i <<endl;
i = skipws(s,i);
More information about the Minisip-devel
mailing list