IBR-DTNSuite 0.6

ibrcommon/ibrcommon/data/File.h

Go to the documentation of this file.
00001 /*
00002  * File.h
00003  *
00004  *  Created on: 20.11.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef IBRCOMMON_FILE_H_
00009 #define IBRCOMMON_FILE_H_
00010 
00011 //#define USE_FILE_LOCKING 1
00012 
00013 #include <iostream>
00014 #include <map>
00015 #include <vector>
00016 #include <list>
00017 #include <dirent.h>
00018 #include <stdio.h>
00019 #include <sys/types.h>
00020 #include <sys/stat.h>
00021 #include "ibrcommon/Exceptions.h"
00022 
00023 using namespace std;
00024 
00025 namespace ibrcommon
00026 {
00031         class File
00032         {
00033         public:
00037                 File();
00038 
00044                 File(const string path);
00045 
00049                 virtual ~File();
00050 
00056                 unsigned char getType() const;
00057 
00064                 int getFiles(list<File> &files);
00065 
00070                 bool isSystem() const;
00071 
00076                 bool isDirectory() const;
00077 
00082                 std::string getPath() const;
00083 
00088                 std::string getBasename() const;
00089 
00095                 int remove(bool recursive = false);
00096 
00102                 File get(string filename) const;
00103 
00108                 File getParent() const;
00109 
00114                 bool exists() const;
00115 
00119                 void update();
00120 
00125                 size_t size() const;
00126 
00130                 time_t lastaccess() const;
00131 
00135                 time_t lastmodify() const;
00136 
00140                 time_t laststatchange() const;
00141 
00146                 static void createDirectory(File &path);
00147 
00148                 bool operator==(const ibrcommon::File &other) const;
00149 
00150         private:
00151                 File(const string path, const unsigned char t);
00152                 void resolveAbsolutePath();
00153                 void removeSlash();
00154                 string _path;
00155                 unsigned char _type;
00156         };
00157 
00161         class FileNotExistsException : public ibrcommon::IOException
00162         {
00163         public:
00164                 FileNotExistsException(ibrcommon::File f) throw() : IOException("The file " + f.getPath() + " does not exists.")
00165                 {
00166                 };
00167         };
00168 
00172         class TemporaryFile : public File
00173         {
00174         public:
00175                 TemporaryFile(const File &path, const std::string prefix = "file");
00176                 virtual ~TemporaryFile();
00177 
00178         private:
00179                 static std::string tmpname(const File &path, const std::string prefix);
00180         };
00181 
00182         class locked_fstreambuf : public std::basic_streambuf<char, std::char_traits<char> >
00183         {
00184         public:
00185                 locked_fstreambuf();
00186                 locked_fstreambuf(File &file, char mode[]);  // ios_base::openmode mode);
00187                 ~locked_fstreambuf();
00188 
00189                 void open(File &file, char mode[]); //ios_base::openmode mode);
00190                 bool is_open();
00191                 void close();
00192 
00193         protected:
00194                 virtual int sync();
00195                 virtual int overflow(int = std::char_traits<char>::eof());
00196                 virtual int underflow();
00197 
00198                 virtual traits_type::pos_type seekoff(traits_type::off_type sp, ios_base::seekdir sd, ios_base::openmode mode = ios_base::in | ios_base::out)
00199                 {
00200                         std::cerr << "SEEK NOT IMPLEMENTED: " << sp << ", " << sd << ", " << mode << std::endl;
00201                         return 0;
00202                 }
00203 
00204                 virtual traits_type::pos_type seekpos(traits_type::pos_type sp, ios_base::openmode mode = ios_base::in | ios_base::out)
00205                 {
00206                         pos_type ret = seekoff(sp, ios::beg, mode);
00207                         return ret;
00208                 }
00209 
00210         private:
00211                 // The size of the input and output buffers.
00212                 static const size_t BUFF_SIZE = 1024;
00213 
00214                 // Input buffer
00215                 char *in_buf_;
00216 
00217                 // Output buffer
00218                 char *out_buf_;
00219 
00220                 FILE *_fd;
00221         };
00222 
00223         class locked_ifstream : public std::istream
00224         {
00225         public:
00226                 locked_ifstream();
00227                 locked_ifstream(File &file, ios_base::openmode mode = ios_base::in);
00228                 ~locked_ifstream();
00229 
00230                 void open(File &file, ios_base::openmode mode = ios_base::in);
00231                 bool is_open();
00232                 void close();
00233 
00234         private:
00235                 locked_fstreambuf _buffer;
00236         };
00237 
00238         class locked_ofstream : public std::ostream
00239         {
00240         public:
00241                 locked_ofstream();
00242                 locked_ofstream(File &file, ios_base::openmode mode = ios_base::out);
00243                 ~locked_ofstream();
00244 
00245                 void open(File &file, ios_base::openmode mode = ios_base::out);
00246                 bool is_open();
00247                 void close();
00248 
00249         private:
00250                 locked_fstreambuf _buffer;
00251         };
00252 }
00253 
00254 #endif /* FILE_H_ */