BALL
1.4.1
|
00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 00005 #ifndef BALL_SYSTEM_FILE_H 00006 #define BALL_SYSTEM_FILE_H 00007 00008 #ifndef BALL_DATATYPE_REGULAREXPRESSION_H 00009 # include <BALL/DATATYPE/regularExpression.h> 00010 #endif 00011 00012 #ifndef BALL_DATATYPE_STRING_H 00013 # include <BALL/DATATYPE/string.h> 00014 #endif 00015 00016 #ifndef BALL_SYSTEM_FILESYSTEM_H 00017 # include <BALL/SYSTEM/fileSystem.h> 00018 #endif 00019 00020 #include <BALL/DATATYPE/hashSet.h> 00021 00022 #include <stdlib.h> // 'getenv' 00023 #include <sys/stat.h> // 'stat', 'lstat' 00024 #include <stdio.h> // 'rename' 00025 #include <algorithm> // 'reverse' 00026 00027 #ifdef BALL_COMPILER_MSVC 00028 #ifndef S_ISREG 00029 # define S_ISREG _S_ISREG 00030 #endif 00031 #ifndef S_ISDIR 00032 # define S_ISDIR _S_ISDIR 00033 #endif 00034 # define S_ISCHR _S_ISCHR 00035 # define S_ISBLK _S_ISBLK 00036 # define S_ISFIFO _S_ISFIFO 00037 # define access _access 00038 #endif 00039 00040 #include <iostream> 00041 #include <fstream> 00042 #include <sys/types.h> 00043 #include <map> 00044 #include <algorithm> 00045 00046 #ifdef BALL_HAS_UNISTD_H 00047 # include <unistd.h> // 'access', 'rename', 'truncate' 00048 #endif 00049 00050 #ifdef BALL_COMPILER_MSVC 00051 # include <fcntl.h> 00052 # include <io.h> 00053 // Define the missing symbols from <unistd.h>, 00054 // which M$, in its infinite wisdom, was unable to provide. 00055 # define F_OK 0 00056 # define W_OK 2 00057 # define R_OK 4 00058 #endif 00059 00060 00061 namespace BALL 00062 { 00080 class BALL_EXPORT TransformationManager 00081 { 00082 public: 00083 00087 00089 TransformationManager(); 00090 00092 ~TransformationManager(); 00093 00095 00098 00100 void registerTransformation(const String& pattern, const String& command); 00101 00103 void unregisterTransformation(const String& pattern); 00104 00106 String findTransformation(const String& name) const; 00107 00125 String transform(const String& name); 00127 00128 protected: 00129 00131 std::map<String, String> transformation_methods_; 00132 }; 00133 00137 class BALL_EXPORT File 00138 : public std::fstream 00139 { 00140 public: 00141 00145 class BALL_EXPORT CannotWrite 00146 : public Exception::GeneralException 00147 { 00148 public: 00149 CannotWrite(const char* file, int line, const String& filename); 00150 00151 ~CannotWrite() 00152 throw(); 00153 00154 String getFilename() const; 00155 00156 protected: 00157 std::string filename_; 00158 }; 00159 00163 00168 typedef std::ios::openmode OpenMode; 00169 00171 00175 00176 static const OpenMode MODE_IN; 00177 00179 static const OpenMode MODE_OUT; 00180 00182 static const OpenMode MODE_APP; 00183 00185 static const OpenMode MODE_BINARY; 00186 00188 static const OpenMode MODE_ATE; 00189 00191 static const OpenMode MODE_TRUNC; 00193 00197 00201 enum Transformation 00202 { 00204 TRANSFORMATION__EXEC = 1, 00206 TRANSFORMATION__FILTER = 2, 00208 TRANSFORMATION__URL = 3 00209 }; 00210 00213 enum Type 00214 { 00216 TYPE__UNKNOWN = 0, 00218 TYPE__DIRECTORY = 1, 00220 TYPE__CHAR_SPECIAL_FILE = 2, 00222 TYPE__BLOCK_SPECIAL_FILE = 3, 00224 TYPE__REGULAR_FILE = 4, 00226 TYPE__SYMBOLIC_LINK = 5, 00228 TYPE__SOCKET = 6, 00230 TYPE__FIFO_SPECIAL_FILE = 7 00231 }; 00232 00234 static const String TRANSFORMATION_EXEC_PREFIX; 00235 00237 static const String TRANSFORMATION_FILE_PREFIX; 00238 00240 static const String TRANSFORMATION_FTP_PREFIX; 00241 00243 static const String TRANSFORMATION_HTTP_PREFIX; 00244 00246 00249 00252 File(); 00253 00260 File(const String& name, OpenMode open_mode = std::ios::in); 00261 00265 virtual ~File(); 00266 00269 virtual void clear(); 00271 00275 00280 // const File& operator = (const File& file); 00281 00283 00287 00295 bool open(const String& name, File::OpenMode open_mode = std::ios::in); 00296 00302 bool reopen(); 00303 00310 bool reopen(File::OpenMode open_mode); 00311 00314 void close(); 00315 00319 const String& getName() const; 00320 00324 void setName(const String& name); 00325 00328 const String& getOriginalName() const; 00329 00335 Size getSize(); 00336 00341 static Size getSize(String name); 00342 00347 File::OpenMode getOpenMode() const; 00348 00355 static Type getType(String name, bool trace_link); 00356 00362 Type getType(bool trace_link) const; 00363 00372 static bool copy(String source_name, String destination_name, Size buffer_size = 4096); 00373 00381 bool copyTo(const String& destination_name, Size buffer_size = 4096); 00382 00390 static bool move(const String& source_name, const String& destination_name); 00391 00398 bool moveTo(const String& destination_name); 00399 00404 static bool remove(String name); 00405 00409 bool remove(); 00410 00417 static bool rename(String old_path, String new_path); 00418 00425 bool renameTo(const String& new_path); 00426 00433 static bool truncate(String path, Size size = 0); 00434 00440 bool truncate(Size size = 0); 00441 00450 static bool createTemporaryFilename(String& temporary, const String& suffix = ".TMP"); 00451 00456 std::fstream& getFileStream(); 00457 00459 00463 00469 TransformationManager& getTransformationManager(); 00470 00476 const TransformationManager& getTransformationManager() const; 00477 00480 static void enableTransformation(Transformation transformation); 00481 00484 static void disableTransformation(Transformation transformation); 00485 00488 static bool isTransformationEnabled(Transformation transformation); 00489 00492 static void registerTransformation(const String& pattern, const String& exec); 00493 00496 static void unregisterTransformation(const String& pattern); 00497 00499 00502 00506 bool operator == (const File& file) const; 00507 00511 bool operator != (const File& file) const; 00512 00517 bool isOpen() const; 00518 00523 bool isClosed() const; 00524 00529 static bool isAccessible(String name); 00530 00535 bool isAccessible() const; 00536 00544 bool isCanonized() const; 00545 00551 static bool isReadable(String name); 00552 00557 bool isReadable() const; 00558 00564 static bool isWritable(String name); 00565 00570 bool isWritable() const; 00571 00577 static bool isExecutable(String name); 00578 00583 bool isExecutable() const; 00584 00586 00589 00595 bool isValid() const; 00596 00598 00599 private: 00600 const File& operator = (const File& file); 00601 00602 protected: 00603 00604 String name_; 00605 String original_name_; 00606 OpenMode open_mode_; 00607 bool is_open_; 00608 bool is_temporary_; 00609 static HashSet<String> created_temp_filenames_; 00610 00611 static TransformationManager transformation_manager_; 00612 static Size transformation_methods_; 00613 }; 00614 00615 # ifndef BALL_NO_INLINE_FUNCTIONS 00616 # include <BALL/SYSTEM/file.iC> 00617 # endif 00618 00619 } // namespace BALL 00620 00621 #endif // BALL_SYSTEM_FILE_H