r3511 - in trunk/libmutil: . include include/libmutil source
erik at minisip.org
erik at minisip.org
Fri Nov 23 00:27:02 CET 2007
Author: erik
Date: 2007-11-23 00:27:01 +0100 (Fri, 23 Nov 2007)
New Revision: 3511
Added:
trunk/libmutil/include/libmutil/FileSystem.h
trunk/libmutil/source/FileSystem.cxx
Modified:
trunk/libmutil/Makefile.am
trunk/libmutil/include/Makefile.am
Log:
* Added a FileSystem class that makes file i/o platform independent. Note
that the most important feature is that the class also makes the user
of a "file system" not having to know if the file is stored locally, or
if it is accessed over the network (for example on a web/webdav server). See
next commit for more details.
TODO: Merge the functions in FileSystemUtils into this file.
Modified: trunk/libmutil/Makefile.am
===================================================================
--- trunk/libmutil/Makefile.am 2007-11-22 16:35:40 UTC (rev 3510)
+++ trunk/libmutil/Makefile.am 2007-11-22 23:27:01 UTC (rev 3511)
@@ -65,6 +65,7 @@
source/SipUri.cxx \
source/CacheItem.cxx \
source/FileSystemUtils.cxx \
+ source/FileSystem.cxx \
$(thread_src)
# maintainer rules
Modified: trunk/libmutil/include/Makefile.am
===================================================================
--- trunk/libmutil/include/Makefile.am 2007-11-22 16:35:40 UTC (rev 3510)
+++ trunk/libmutil/include/Makefile.am 2007-11-22 23:27:01 UTC (rev 3511)
@@ -31,6 +31,7 @@
libmutil/CircularBuffer.h \
libmutil/SipUri.h \
libmutil/CacheItem.h \
+ libmutil/FileSystem.h \
libmutil/FileSystemUtils.h
noinst_HEADERS = config.h
Added: trunk/libmutil/include/libmutil/FileSystem.h
===================================================================
--- trunk/libmutil/include/libmutil/FileSystem.h (rev 0)
+++ trunk/libmutil/include/libmutil/FileSystem.h 2007-11-22 23:27:01 UTC (rev 3511)
@@ -0,0 +1,105 @@
+/*
+ Copyright (C) 2006 the Minisip Team
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+/* Copyright (C) 2007
+ *
+ * Authors: Erik Eliasson <ere at kth.se>
+ */
+
+#ifndef MFILESYSTEM_H
+#define MFILESYSTEM_H
+
+#include <libmutil/libmutil_config.h>
+#include <libmutil/Exception.h>
+#include <libmutil/MemObject.h>
+
+LIBMUTIL_API class FileException : public Exception{
+ public:
+ FileException(const std::string why);
+};
+
+LIBMUTIL_API class FileSystemException : public Exception{
+ public:
+ FileSystemException(const std::string why);
+};
+
+/**
+ * Defines an API for how to manipulate files. A file object
+ * is created using the FileSystem::open() method.
+ *
+ * Note that File objects may represent remote files, and
+ * the flush() is more likely to fail on such files than when
+ * doing traditional file I/O.
+ */
+LIBMUTIL_API class File : public MObject {
+ public:
+ virtual void read(void *buf, int32_t count) = 0;
+ virtual void write(void *buf, int32_t count) = 0;
+ virtual void seek(int32_t pos ) = 0;
+ virtual int64_t size() = 0;
+ virtual void flush() = 0;
+};
+
+/**
+ * Defines the API to a file storage area (file system) where
+ * the user can create directories, open files, and set default
+ * paths.
+ */
+LIBMUTIL_API class FileSystem : public MObject {
+ public:
+ virtual void mkdir( const std::string & name ) = 0;
+ virtual MRef<File*> open( const std::string& path, bool createIfNotExist=false ) = 0;
+
+ /**
+ * If a path does not start with a forward slash, then a
+ * string is prepended to it. This is similar to a
+ * "cd" (change current working directory) command.
+ *
+ * For local file systems, not setting the default path at
+ * all means using the applications current working
+ * directory.
+ */
+ virtual void setDefaultPath(std::string);
+
+ /**
+ * Returns the prefix added to all paths not starting with
+ * a forward slash that are given as parameters to open().
+ * @see setDefaultPath()
+ */
+ virtual std::string getDefaultPath();
+
+ protected:
+ /**
+ * Prefix that should be added to paths not starting with
+ * a forward slash character.
+ *
+ */
+ std::string defPrefix;
+};
+
+LIBMUTIL_API class LocalFileSystem : public FileSystem {
+ public:
+ virtual void mkdir( const std::string & name );
+ virtual MRef<File*> open( const std::string& path, bool createIfNotExist=false );
+
+ private:
+
+};
+
+#endif // MFILESYSTEM_H
+
Added: trunk/libmutil/source/FileSystem.cxx
===================================================================
--- trunk/libmutil/source/FileSystem.cxx (rev 0)
+++ trunk/libmutil/source/FileSystem.cxx 2007-11-22 23:27:01 UTC (rev 3511)
@@ -0,0 +1,92 @@
+
+
+#include <config.h>
+
+#include<libmutil/FileSystem.h>
+#include<fstream>
+
+#include<sys/stat.h>
+#include<sys/types.h>
+
+
+using namespace std;
+
+class LocalFile : public File {
+ public:
+ LocalFile(string path);
+ virtual void read(void *buf, int32_t count);
+ virtual void write(void *buf, int32_t count);
+ virtual void seek(int32_t pos );
+ virtual int64_t size();
+ virtual void flush();
+
+ private:
+ fstream file;
+
+};
+
+
+LocalFile::LocalFile(string path) {
+ cerr << "EEEE: trying to open <"<<path<<">"<<endl;
+ file.open( path.c_str(), ios::out | ios::in | ios::binary );
+ if (!file.is_open()){
+ throw FileException("Could not open file");
+ }
+}
+
+void LocalFile::read(void *buf, int32_t count){
+ file.read( (char*)buf, count );
+}
+
+void LocalFile::write(void *buf, int32_t count){
+ file.write( (char*)buf, count);
+}
+
+void LocalFile::seek(int32_t pos ){
+ file.seekg( pos, ios::beg );
+}
+
+int64_t LocalFile::size(){
+ int64_t tmp = file.tellg(); // save position
+ file.seekg(0, std::ios_base::beg);
+ std::ifstream::pos_type begin_pos = file.tellg();
+ file.seekg(0, std::ios_base::end);
+ int64_t s = static_cast<int64_t>( file.tellg() - begin_pos );
+ file.seekg( tmp, ios::beg ); // restore position
+ return s;
+}
+
+void LocalFile::flush(){
+ file.flush();
+}
+
+void LocalFileSystem::mkdir( const std::string & name ){
+ if ( ::mkdir( name.c_str(), 0 ) != 0 ){
+ throw FileSystemException("Could not create directory");
+ }
+}
+
+MRef<File*> LocalFileSystem::open( const std::string & name, bool createIfNotExist ){
+ string tmp=name;
+ if ( name.size()>0 && name[0]!='/' ){
+ tmp = defPrefix+name;
+ }
+ return new LocalFile(tmp);
+}
+
+FileException::FileException( string why ) : Exception(why){ }
+
+FileSystemException::FileSystemException( string why ) : Exception(why){ }
+
+void FileSystem::setDefaultPath(std::string dp){
+ if (dp[dp.size()-1]=='/')
+ defPrefix = dp;
+ else
+ defPrefix = dp+'/';
+
+}
+
+std::string FileSystem::getDefaultPath(){
+ return defPrefix;
+}
+
More information about the Minisip-devel
mailing list