Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_SYSTEM_FILE_H
#define BALL_SYSTEM_FILE_H
#ifndef BALL_DATATYPE_STRING_H
# include <BALL/DATATYPE/string.h>
#endif
#ifndef BALL_SYSTEM_FILESYSTEM_H
# include <BALL/SYSTEM/fileSystem.h>
#endif
#include <BALL/DATATYPE/hashSet.h>
#include <cstdlib> // 'getenv'
#include <sys/stat.h> // 'stat', 'lstat'
#include <cstdio> // 'rename'
#include <algorithm> // 'reverse'
#ifdef BALL_COMPILER_MSVC
#ifndef S_ISREG
# define S_ISREG _S_ISREG
#endif
#ifndef S_ISDIR
# define S_ISDIR _S_ISDIR
#endif
# define S_ISCHR _S_ISCHR
# define S_ISBLK _S_ISBLK
# define S_ISFIFO _S_ISFIFO
#endif
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <map>
#include <algorithm>
#ifdef BALL_HAS_UNISTD_H
# include <unistd.h> // 'access', 'rename', 'truncate'
#endif
#ifdef BALL_COMPILER_MSVC
# include <fcntl.h>
# include <io.h>
# include <share.h> // shflags for _sopen_s
// Define the missing symbols from <unistd.h>,
// which M$, in its infinite wisdom, was unable to provide.
# define F_OK 0
# define W_OK 2
# define R_OK 4
#endif
namespace BALL
{
class BALL_EXPORT TransformationManager
{
public:
TransformationManager();
~TransformationManager();
void registerTransformation(const String& pattern, const String& command);
void unregisterTransformation(const String& pattern);
String findTransformation(const String& name) const;
String transform(const String& name);
protected:
std::map<String, String> transformation_methods_;
};
class BALL_EXPORT File
: public std::fstream
{
public:
class BALL_EXPORT CannotWrite
: public Exception::GeneralException
{
public:
CannotWrite(const char* file, int line, const String& filename);
~CannotWrite()
throw();
String getFilename() const;
protected:
std::string filename_;
};
typedef std::ios::openmode OpenMode;
static const OpenMode MODE_IN;
static const OpenMode MODE_OUT;
static const OpenMode MODE_APP;
static const OpenMode MODE_BINARY;
static const OpenMode MODE_ATE;
static const OpenMode MODE_TRUNC;
enum Transformation
{
TRANSFORMATION__EXEC = 1,
TRANSFORMATION__FILTER = 2,
TRANSFORMATION__URL = 3
};
enum Type
{
TYPE__UNKNOWN = 0,
TYPE__DIRECTORY = 1,
TYPE__CHAR_SPECIAL_FILE = 2,
TYPE__BLOCK_SPECIAL_FILE = 3,
TYPE__REGULAR_FILE = 4,
TYPE__SYMBOLIC_LINK = 5,
TYPE__SOCKET = 6,
TYPE__FIFO_SPECIAL_FILE = 7
};
static const String TRANSFORMATION_EXEC_PREFIX;
static const String TRANSFORMATION_FILE_PREFIX;
static const String TRANSFORMATION_FTP_PREFIX;
static const String TRANSFORMATION_HTTP_PREFIX;
File();
File(const String& name, OpenMode open_mode = std::ios::in);
virtual ~File();
virtual void clear();
// const File& operator = (const File& file);
bool open(const String& name, File::OpenMode open_mode = std::ios::in);
bool reopen();
bool reopen(File::OpenMode open_mode);
void close();
const String& getName() const;
void setName(const String& name);
const String& getOriginalName() const;
Size getSize();
static Size getSize(String name);
File::OpenMode getOpenMode() const;
static Type getType(String name, bool trace_link);
Type getType(bool trace_link) const;
static bool copy(String source_name, String destination_name, Size buffer_size = 4096);
bool copyTo(const String& destination_name, Size buffer_size = 4096);
static bool move(const String& source_name, const String& destination_name);
bool moveTo(const String& destination_name);
static bool remove(String name);
bool remove();
static bool rename(String old_path, String new_path);
bool renameTo(const String& new_path);
static bool truncate(String path, Size size = 0);
bool truncate(Size size = 0);
static bool createTemporaryFilename(String& temporary, const String& suffix = ".TMP");
std::fstream& getFileStream();
TransformationManager& getTransformationManager();
const TransformationManager& getTransformationManager() const;
static void enableTransformation(Transformation transformation);
static void disableTransformation(Transformation transformation);
static bool isTransformationEnabled(Transformation transformation);
static void registerTransformation(const String& pattern, const String& exec);
static void unregisterTransformation(const String& pattern);
bool operator == (const File& file) const;
bool operator != (const File& file) const;
bool isOpen() const;
bool isClosed() const;
static bool isAccessible(String name);
bool isAccessible() const;
bool isCanonized() const;
static bool isReadable(String name);
bool isReadable() const;
static bool isWritable(String name);
bool isWritable() const;
static bool isExecutable(String name);
bool isExecutable() const;
bool isValid() const;
private:
const File& operator = (const File& file);
protected:
String name_;
String original_name_;
OpenMode open_mode_;
bool is_open_;
bool is_temporary_;
static HashSet<String> created_temp_filenames_;
static TransformationManager transformation_manager_;
static Size transformation_methods_;
};
# ifndef BALL_NO_INLINE_FUNCTIONS
# include <BALL/SYSTEM/file.iC>
# endif
} // namespace BALL
#endif // BALL_SYSTEM_FILE_H
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
BALL::Exception::GeneralException
Definition COMMON/exception.h:61
BALL::File::CannotWrite::filename_
std::string filename_
Definition file.h:154
BALL::File::CannotWrite::~CannotWrite
~CannotWrite()
BALL::File::CannotWrite::CannotWrite
CannotWrite(const char *file, int line, const String &filename)
BALL::File::CannotWrite::getFilename
String getFilename() const
BALL::File::original_name_
String original_name_
Definition file.h:602
BALL::File::createTemporaryFilename
static bool createTemporaryFilename(String &temporary, const String &suffix=".TMP")
BALL::File::MODE_APP
static const OpenMode MODE_APP
Append. Seek to end before each write operation.
Definition file.h:179
BALL::File::isClosed
bool isClosed() const
BALL::File::copy
static bool copy(String source_name, String destination_name, Size buffer_size=4096)
BALL::File::registerTransformation
static void registerTransformation(const String &pattern, const String &exec)
BALL::File::enableTransformation
static void enableTransformation(Transformation transformation)
BALL::File::getOpenMode
File::OpenMode getOpenMode() const
BALL::File::move
static bool move(const String &source_name, const String &destination_name)
BALL::File::remove
static bool remove(String name)
BALL::File::reopen
bool reopen(File::OpenMode open_mode)
BALL::File::File
File()
BALL::File::truncate
static bool truncate(String path, Size size=0)
BALL::File::isTransformationEnabled
static bool isTransformationEnabled(Transformation transformation)
BALL::File::isAccessible
bool isAccessible() const
BALL::File::transformation_methods_
static Size transformation_methods_
Definition file.h:609
BALL::File::isExecutable
bool isExecutable() const
BALL::File::OpenMode
std::ios::openmode OpenMode
Definition file.h:165
BALL::File::created_temp_filenames_
static HashSet< String > created_temp_filenames_
Definition file.h:606
BALL::File::disableTransformation
static void disableTransformation(Transformation transformation)
BALL::File::TRANSFORMATION_HTTP_PREFIX
static const String TRANSFORMATION_HTTP_PREFIX
Prefix for HTTP-transfer "http://".
Definition file.h:240
BALL::File::unregisterTransformation
static void unregisterTransformation(const String &pattern)
BALL::File::getName
const String & getName() const
BALL::File::isWritable
static bool isWritable(String name)
BALL::File::getType
static Type getType(String name, bool trace_link)
BALL::File::TRANSFORMATION_EXEC_PREFIX
static const String TRANSFORMATION_EXEC_PREFIX
Prefix for filenames that are created through the execution of commands "exec:".
Definition file.h:231
BALL::File::getTransformationManager
const TransformationManager & getTransformationManager() const
BALL::File::isOpen
bool isOpen() const
BALL::File::MODE_TRUNC
static const OpenMode MODE_TRUNC
Truncate an existing file.
Definition file.h:188
BALL::File::renameTo
bool renameTo(const String &new_path)
BALL::File::MODE_BINARY
static const OpenMode MODE_BINARY
Binary mode.
Definition file.h:182
BALL::File::rename
static bool rename(String old_path, String new_path)
BALL::File::isCanonized
bool isCanonized() const
BALL::File::MODE_ATE
static const OpenMode MODE_ATE
Seek to end directly after opening.
Definition file.h:185
BALL::File::open
bool open(const String &name, File::OpenMode open_mode=std::ios::in)
BALL::File::truncate
bool truncate(Size size=0)
BALL::File::~File
virtual ~File()
BALL::File::is_open_
bool is_open_
Definition file.h:604
BALL::File::isValid
bool isValid() const
BALL::File::getSize
static Size getSize(String name)
BALL::File::transformation_manager_
static TransformationManager transformation_manager_
Definition file.h:608
BALL::File::Type
Type
Definition file.h:211
BALL::File::TYPE__SOCKET
@ TYPE__SOCKET
Definition file.h:225
BALL::File::TYPE__SYMBOLIC_LINK
@ TYPE__SYMBOLIC_LINK
Definition file.h:223
BALL::File::TYPE__CHAR_SPECIAL_FILE
@ TYPE__CHAR_SPECIAL_FILE
Definition file.h:217
BALL::File::TYPE__FIFO_SPECIAL_FILE
@ TYPE__FIFO_SPECIAL_FILE
Definition file.h:227
BALL::File::TYPE__UNKNOWN
@ TYPE__UNKNOWN
Definition file.h:213
BALL::File::TYPE__REGULAR_FILE
@ TYPE__REGULAR_FILE
Definition file.h:221
BALL::File::TYPE__DIRECTORY
@ TYPE__DIRECTORY
Definition file.h:215
BALL::File::TYPE__BLOCK_SPECIAL_FILE
@ TYPE__BLOCK_SPECIAL_FILE
Definition file.h:219
BALL::File::isAccessible
static bool isAccessible(String name)
BALL::File::copyTo
bool copyTo(const String &destination_name, Size buffer_size=4096)
BALL::File::MODE_IN
static const OpenMode MODE_IN
Open for input (default).
Definition file.h:173
BALL::File::getTransformationManager
TransformationManager & getTransformationManager()
BALL::File::isWritable
bool isWritable() const
BALL::File::isReadable
bool isReadable() const
BALL::File::getOriginalName
const String & getOriginalName() const
BALL::File::close
void close()
BALL::File::getType
Type getType(bool trace_link) const
BALL::File::isExecutable
static bool isExecutable(String name)
BALL::File::clear
virtual void clear()
BALL::File::name_
String name_
Definition file.h:601
BALL::File::getFileStream
std::fstream & getFileStream()
BALL::File::MODE_OUT
static const OpenMode MODE_OUT
Open for output.
Definition file.h:176
BALL::File::getSize
Size getSize()
BALL::File::File
File(const String &name, OpenMode open_mode=std::ios::in)
BALL::File::TRANSFORMATION_FILE_PREFIX
static const String TRANSFORMATION_FILE_PREFIX
Prefix for files (to mimick URL-like behavior) "file:".
Definition file.h:234
BALL::File::setName
void setName(const String &name)
BALL::File::moveTo
bool moveTo(const String &destination_name)
BALL::File::is_temporary_
bool is_temporary_
Definition file.h:605
BALL::File::remove
bool remove()
BALL::File::open_mode_
OpenMode open_mode_
Definition file.h:603
BALL::File::Transformation
Transformation
Definition file.h:199
BALL::File::TRANSFORMATION__EXEC
@ TRANSFORMATION__EXEC
Definition file.h:201
BALL::File::TRANSFORMATION__FILTER
@ TRANSFORMATION__FILTER
Definition file.h:203
BALL::File::TRANSFORMATION__URL
@ TRANSFORMATION__URL
Definition file.h:205
BALL::File::isReadable
static bool isReadable(String name)
BALL::File::TRANSFORMATION_FTP_PREFIX
static const String TRANSFORMATION_FTP_PREFIX
Prefix for FTP-transfers "ftp://".
Definition file.h:237
BALL::File::reopen
bool reopen()
BALL::HashSet
Definition hashSet.h:46
BALL::String
Definition string.h:57
BALL::TransformationManager
Definition file.h:78
BALL::TransformationManager::~TransformationManager
~TransformationManager()
Destructor.
BALL::TransformationManager::transform
String transform(const String &name)
BALL::TransformationManager::findTransformation
String findTransformation(const String &name) const
Find a transformation matching a given file name.
BALL::TransformationManager::registerTransformation
void registerTransformation(const String &pattern, const String &command)
Add a new transformation.
BALL::TransformationManager::transformation_methods_
std::map< String, String > transformation_methods_
The map containing all transformation methods.
Definition file.h:128
BALL::TransformationManager::unregisterTransformation
void unregisterTransformation(const String &pattern)
Delete a transformation.
BALL::TransformationManager::TransformationManager
TransformationManager()
Default constructor.
fileSystem.h
hashSet.h
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
std
STL namespace.
string.h