00001
00002 #ifndef INCLUDED_FileSystemCommPort_h_
00003 #define INCLUDED_FileSystemCommPort_h_
00004
00005 #include "local/CommPort.h"
00006 #include "Shared/plist.h"
00007 #include <fstream>
00008 #include <ios>
00009
00010
00011
00012 class FileSystemCommPort : public CommPort, public virtual plist::PrimitiveListener {
00013 public:
00014
00015 explicit FileSystemCommPort(const std::string& name)
00016 : CommPort(autoRegisterFileSystemCommPort,name),
00017 path(), mode(std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
00018 rbuf(), wbuf(), curloc(), curmode(), opened(0)
00019 {
00020 addEntry("Path",path,"Path of file system object being accessed");
00021 addEntry("Mode",mode,"Mode bitmask to pass to the open() call, defaults to 'w+b': in|out|trunc|binary (see std::ios_base::openmode)");
00022 }
00023
00024
00025 virtual ~FileSystemCommPort() {
00026 if(opened>0)
00027 connectionError("File descriptor still open in FileSystemCommPort destructor",true);
00028 }
00029
00030 virtual std::string getClassName() const { return autoRegisterFileSystemCommPort; }
00031
00032 virtual std::basic_filebuf<std::ios::char_type>& getReadStreambuf() { return rbuf; }
00033 virtual std::basic_filebuf<std::ios::char_type>& getWriteStreambuf() { return wbuf; }
00034 virtual bool isReadable() { return rbuf.is_open(); }
00035 virtual bool isWriteable() { return wbuf.is_open(); }
00036
00037
00038 virtual bool open();
00039
00040
00041 virtual bool close();
00042
00043
00044 virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00045
00046 plist::Primitive<std::string> path;
00047 plist::Primitive<int> mode;
00048
00049 protected:
00050 explicit FileSystemCommPort(const std::string& classname, const std::string& instancename)
00051 : CommPort(classname,instancename),
00052 path(), mode(std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
00053 rbuf(), wbuf(), curloc(), curmode(), opened(0)
00054 {
00055 addEntry("Path",path,"Path of file system object being accessed");
00056 addEntry("Mode",mode,"Mode bitmask to pass to the open() call, defaults to 'w+b': in|out|trunc|binary (see std::ios_base::openmode)");
00057 }
00058
00059
00060 virtual void connectionError(const std::string& msg, bool fatal) {
00061 std::cerr << msg << std::endl;
00062 if(fatal && (rbuf.is_open() || wbuf.is_open())) {
00063 opened=1;
00064 close();
00065 }
00066 }
00067
00068 std::basic_filebuf<std::ios::char_type> rbuf;
00069 std::basic_filebuf<std::ios::char_type> wbuf;
00070 std::string curloc;
00071 std::ios_base::openmode curmode;
00072 unsigned int opened;
00073
00074
00075 static const std::string autoRegisterFileSystemCommPort;
00076 };
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #endif