Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef IBRCOMMON_FILE_H_
00009 #define IBRCOMMON_FILE_H_
00010
00011
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 #ifdef USE_FILE_LOCKING
00024 #include <ext/stdio_filebuf.h>
00025 #else
00026 #include <fstream>
00027 #endif
00028
00029 using namespace std;
00030
00031 namespace ibrcommon
00032 {
00037 class File
00038 {
00039 public:
00043 File();
00044
00050 File(const string path);
00051
00055 virtual ~File();
00056
00062 unsigned char getType() const;
00063
00070 int getFiles(list<File> &files);
00071
00076 bool isSystem() const;
00077
00082 bool isDirectory() const;
00083
00088 std::string getPath() 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
00131 static void createDirectory(File &path);
00132
00133 private:
00134 File(const string path, const unsigned char t);
00135 void resolveAbsolutePath();
00136 void removeSlash();
00137 string _path;
00138 unsigned char _type;
00139 };
00140
00144 class FileNotExistsException : public ibrcommon::IOException
00145 {
00146 public:
00147 FileNotExistsException(ibrcommon::File f) throw() : IOException("The file " + f.getPath() + " does not exists.")
00148 {
00149 };
00150 };
00151
00155 class TemporaryFile : public File
00156 {
00157 public:
00158 TemporaryFile(const File &path, const std::string prefix = "file");
00159 virtual ~TemporaryFile();
00160
00161 private:
00162 static std::string tmpname(const File &path, const std::string prefix);
00163 };
00164
00165 class locked_ifstream
00166 {
00167 public:
00168 locked_ifstream(File &file, ios_base::openmode mode = ios_base::in);
00169 ~locked_ifstream();
00170
00171 void open(File &file, ios_base::openmode mode = ios_base::in);
00172 bool is_open();
00173 void close();
00174
00175 std::istream& operator*();
00176
00177 private:
00178 #ifdef USE_FILE_LOCKING
00179 __gnu_cxx::stdio_filebuf<char> *_buf;
00180 std::istream *_stream;
00181 int _fd;
00182 #else
00183 std::ifstream _stream;
00184 #endif
00185 };
00186
00187 class locked_ofstream
00188 {
00189 public:
00190 locked_ofstream(File &file, ios_base::openmode mode = ios_base::out);
00191 ~locked_ofstream();
00192
00193 void open(File &file, ios_base::openmode mode = ios_base::out);
00194 bool is_open();
00195 void close();
00196
00197 std::ostream& operator*();
00198
00199 private:
00200 #ifdef USE_FILE_LOCKING
00201 __gnu_cxx::stdio_filebuf<char> *_buf;
00202 std::ostream *_stream;
00203 int _fd;
00204 #else
00205 std::ofstream _stream;
00206 #endif
00207 };
00208
00209 class locked_fstream
00210 {
00211 public:
00212 locked_fstream();
00213 locked_fstream(File &file, ios_base::openmode mode = ios_base::in | ios_base::out);
00214 ~locked_fstream();
00215
00216 void open(File &file, ios_base::openmode mode = ios_base::in | ios_base::out);
00217 bool is_open();
00218 void close();
00219
00220 std::iostream& operator*();
00221
00222 #ifdef USE_FILE_LOCKING
00223 static unsigned int getopts(ios_base::openmode mode);
00224 #endif
00225
00226 private:
00227 #ifdef USE_FILE_LOCKING
00228 __gnu_cxx::stdio_filebuf<char> *_buf;
00229 std::iostream *_stream;
00230 int _fd;
00231 #else
00232 std::fstream _stream;
00233 #endif
00234 };
00235 }
00236
00237 #endif