Largely rework llfile*
This commit is contained in:
@@ -38,15 +38,13 @@
|
||||
* Attempts to mostly mirror the POSIX style IO functions.
|
||||
*/
|
||||
|
||||
typedef FILE LLFILE;
|
||||
typedef FILE LLFILE;
|
||||
|
||||
#if LL_WINDOWS
|
||||
// windows version of stat function and stat data structure are called _stat
|
||||
typedef struct _stat llstat;
|
||||
#else
|
||||
typedef struct stat llstat;
|
||||
#include <ext/stdio_filebuf.h>
|
||||
#include <bits/postypes.h>
|
||||
#endif
|
||||
|
||||
#ifndef S_ISREG
|
||||
@@ -93,135 +91,20 @@ public:
|
||||
static std::string strerr();
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Provides a layer of compatibility for C/POSIX.
|
||||
*
|
||||
* This is taken from both the GNU __gnu_cxx::stdio_filebuf extension and
|
||||
* VC's basic_filebuf implementation.
|
||||
* This file buffer provides extensions for working with standard C FILE*'s
|
||||
* and POSIX file descriptors for platforms that support this.
|
||||
*/
|
||||
namespace
|
||||
{
|
||||
#if LL_WINDOWS
|
||||
//typedef std::filebuf _Myfb;
|
||||
//Singu note: Wrap around std::filebuf to override the open procedure.
|
||||
// The client encodes filepaths in UTF-8, however Windows uses UTF-16 encoding natively.
|
||||
// Need to convert paths to UTF-16 before calling std::filebuf::open.
|
||||
struct _Myfb : public std::filebuf
|
||||
{
|
||||
_Myfb() : std::filebuf() {}
|
||||
_Myfb(_Filet* file) : std::filebuf(file) {}
|
||||
_Myt *open(const char *filename, std::ios_base::openmode mode, int prot = (int)std::ios_base::_Openprot)
|
||||
{
|
||||
return std::filebuf::open(utf8str_to_utf16str(filename).c_str(),mode,prot);
|
||||
}
|
||||
};
|
||||
#if !defined(LL_WINDOWS)
|
||||
typedef std::ifstream llifstream;
|
||||
typedef std::ofstream llofstream;
|
||||
#else
|
||||
typedef __gnu_cxx::stdio_filebuf< char > _Myfb;
|
||||
typedef std::__c_file _Filet;
|
||||
#endif /* LL_WINDOWS */
|
||||
}
|
||||
|
||||
class LL_COMMON_API llstdio_filebuf : public _Myfb
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* deferred initialization / destruction
|
||||
*/
|
||||
llstdio_filebuf() : _Myfb() {}
|
||||
virtual ~llstdio_filebuf() {}
|
||||
|
||||
/**
|
||||
* @param f An open @c FILE*.
|
||||
* @param mode Same meaning as in a standard filebuf.
|
||||
* @param size Optimal or preferred size of internal buffer, in chars.
|
||||
* Defaults to system's @c BUFSIZ.
|
||||
*
|
||||
* This constructor associates a file stream buffer with an open
|
||||
* C @c FILE*. The @c FILE* will not be automatically closed when the
|
||||
* stdio_filebuf is closed/destroyed.
|
||||
*/
|
||||
llstdio_filebuf(_Filet* __f, std::ios_base::openmode __mode,
|
||||
//size_t __size = static_cast<size_t>(BUFSIZ)) :
|
||||
size_t __size = static_cast<size_t>(1)) :
|
||||
#if LL_WINDOWS
|
||||
_Myfb(__f) {}
|
||||
#else
|
||||
_Myfb(__f, __mode, __size) {}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Opens an external file.
|
||||
* @param s The name of the file.
|
||||
* @param mode The open mode flags.
|
||||
* @return @c this on success, NULL on failure
|
||||
*
|
||||
* If a file is already open, this function immediately fails.
|
||||
* Otherwise it tries to open the file named @a s using the flags
|
||||
* given in @a mode.
|
||||
*/
|
||||
//llstdio_filebuf* open(const char *_Filename,
|
||||
// std::ios_base::openmode _Mode);
|
||||
|
||||
/**
|
||||
* @param fd An open file descriptor.
|
||||
* @param mode Same meaning as in a standard filebuf.
|
||||
* @param size Optimal or preferred size of internal buffer, in chars.
|
||||
*
|
||||
* This constructor associates a file stream buffer with an open
|
||||
* POSIX file descriptor. The file descriptor will be automatically
|
||||
* closed when the stdio_filebuf is closed/destroyed.
|
||||
*/
|
||||
#if !LL_WINDOWS
|
||||
llstdio_filebuf(int __fd, std::ios_base::openmode __mode,
|
||||
//size_t __size = static_cast<size_t>(BUFSIZ)) :
|
||||
size_t __size = static_cast<size_t>(1)) :
|
||||
_Myfb(__fd, __mode, __size) {}
|
||||
#endif
|
||||
|
||||
// *TODO: Seek the underlying c stream for better cross-platform compatibility?
|
||||
#if !LL_WINDOWS
|
||||
protected:
|
||||
/** underflow() and uflow() functions are called to get the next
|
||||
* character from the real input source when the buffer is empty.
|
||||
* Buffered input uses underflow()
|
||||
*/
|
||||
/*virtual*/ int_type underflow();
|
||||
|
||||
/* Convert internal byte sequence to external, char-based
|
||||
* sequence via codecvt.
|
||||
*/
|
||||
bool _convert_to_external(char_type*, std::streamsize);
|
||||
|
||||
/** The overflow() function is called to transfer characters to the
|
||||
* real output destination when the buffer is full. A call to
|
||||
* overflow(c) outputs the contents of the buffer plus the
|
||||
* character c.
|
||||
* Consume some sequence of the characters in the pending sequence.
|
||||
*/
|
||||
/*virtual*/ int_type overflow(int_type __c = traits_type::eof());
|
||||
|
||||
/** sync() flushes the underlying @c FILE* stream.
|
||||
*/
|
||||
/*virtual*/ int sync();
|
||||
|
||||
std::streamsize xsgetn(char_type*, std::streamsize);
|
||||
std::streamsize xsputn(char_type const*, std::streamsize);
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Controlling input for files.
|
||||
*
|
||||
* This class supports reading from named files, using the inherited
|
||||
* functions from std::basic_istream. To control the associated
|
||||
* sequence, an instance of std::basic_filebuf (or a platform-specific derivative)
|
||||
* which allows construction using a pre-exisintg file stream buffer.
|
||||
* We refer to this std::basic_filebuf (or derivative) as @c sb.
|
||||
*/
|
||||
class LL_COMMON_API llifstream : public std::istream
|
||||
* functions from std::ifstream. The only added value is that our constructor
|
||||
* Does The Right Thing when passed a non-ASCII pathname. Sadly, that isn't
|
||||
* true of Microsoft's std::ifstream.
|
||||
*/
|
||||
class LL_COMMON_API llifstream : public std::ifstream
|
||||
{
|
||||
// input stream associated with a C stream
|
||||
public:
|
||||
@@ -247,56 +130,6 @@ public:
|
||||
explicit llifstream(const char* _Filename,
|
||||
ios_base::openmode _Mode = ios_base::in);
|
||||
|
||||
/**
|
||||
* @brief Create a stream using an open c file stream.
|
||||
* @param File An open @c FILE*.
|
||||
@param Mode Same meaning as in a standard filebuf.
|
||||
@param Size Optimal or preferred size of internal buffer, in chars.
|
||||
Defaults to system's @c BUFSIZ.
|
||||
*/
|
||||
explicit llifstream(_Filet *_File,
|
||||
ios_base::openmode _Mode = ios_base::in,
|
||||
//size_t _Size = static_cast<size_t>(BUFSIZ));
|
||||
size_t _Size = static_cast<size_t>(1));
|
||||
|
||||
/**
|
||||
* @brief Create a stream using an open file descriptor.
|
||||
* @param fd An open file descriptor.
|
||||
@param Mode Same meaning as in a standard filebuf.
|
||||
@param Size Optimal or preferred size of internal buffer, in chars.
|
||||
Defaults to system's @c BUFSIZ.
|
||||
*/
|
||||
#if !LL_WINDOWS
|
||||
explicit llifstream(int __fd,
|
||||
ios_base::openmode _Mode = ios_base::in,
|
||||
//size_t _Size = static_cast<size_t>(BUFSIZ));
|
||||
size_t _Size = static_cast<size_t>(1));
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief The destructor does nothing.
|
||||
*
|
||||
* The file is closed by the filebuf object, not the formatting
|
||||
* stream.
|
||||
*/
|
||||
virtual ~llifstream() {}
|
||||
|
||||
// Members:
|
||||
/**
|
||||
* @brief Accessing the underlying buffer.
|
||||
* @return The current basic_filebuf buffer.
|
||||
*
|
||||
* This hides both signatures of std::basic_ios::rdbuf().
|
||||
*/
|
||||
llstdio_filebuf* rdbuf() const
|
||||
{ return const_cast<llstdio_filebuf*>(&_M_filebuf); }
|
||||
|
||||
/**
|
||||
* @brief Wrapper to test for an open file.
|
||||
* @return @c rdbuf()->is_open()
|
||||
*/
|
||||
bool is_open() const;
|
||||
|
||||
/**
|
||||
* @brief Opens an external file.
|
||||
* @param Filename The name of the file.
|
||||
@@ -306,34 +139,21 @@ public:
|
||||
* fails, @c failbit is set in the stream's error state.
|
||||
*/
|
||||
void open(const std::string& _Filename,
|
||||
ios_base::openmode _Mode = ios_base::in)
|
||||
{ open(_Filename.c_str(), _Mode); }
|
||||
ios_base::openmode _Mode = ios_base::in);
|
||||
void open(const char* _Filename,
|
||||
ios_base::openmode _Mode = ios_base::in);
|
||||
|
||||
/**
|
||||
* @brief Close the file.
|
||||
*
|
||||
* Calls @c llstdio_filebuf::close(). If that function
|
||||
* fails, @c failbit is set in the stream's error state.
|
||||
*/
|
||||
void close();
|
||||
|
||||
private:
|
||||
llstdio_filebuf _M_filebuf;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Controlling output for files.
|
||||
*
|
||||
* This class supports writing to named files, using the inherited
|
||||
* functions from std::basic_ostream. To control the associated
|
||||
* sequence, an instance of std::basic_filebuf (or a platform-specific derivative)
|
||||
* which allows construction using a pre-exisintg file stream buffer.
|
||||
* We refer to this std::basic_filebuf (or derivative) as @c sb.
|
||||
*/
|
||||
class LL_COMMON_API llofstream : public std::ostream
|
||||
* This class supports writing to named files, using the inherited functions
|
||||
* from std::ofstream. The only added value is that our constructor Does The
|
||||
* Right Thing when passed a non-ASCII pathname. Sadly, that isn't true of
|
||||
* Microsoft's std::ofstream.
|
||||
*/
|
||||
class LL_COMMON_API llofstream : public std::ofstream
|
||||
{
|
||||
public:
|
||||
// Constructors:
|
||||
@@ -351,64 +171,13 @@ public:
|
||||
* @param Filename String specifying the filename.
|
||||
* @param Mode Open file in specified mode (see std::ios_base).
|
||||
*
|
||||
* @c ios_base::out|ios_base::trunc is automatically included in
|
||||
* @a mode.
|
||||
* @c ios_base::out is automatically included in @a mode.
|
||||
*/
|
||||
explicit llofstream(const std::string& _Filename,
|
||||
ios_base::openmode _Mode = ios_base::out|ios_base::trunc);
|
||||
explicit llofstream(const char* _Filename,
|
||||
ios_base::openmode _Mode = ios_base::out|ios_base::trunc);
|
||||
|
||||
/**
|
||||
* @brief Create a stream using an open c file stream.
|
||||
* @param File An open @c FILE*.
|
||||
@param Mode Same meaning as in a standard filebuf.
|
||||
@param Size Optimal or preferred size of internal buffer, in chars.
|
||||
Defaults to system's @c BUFSIZ.
|
||||
*/
|
||||
explicit llofstream(_Filet *_File,
|
||||
ios_base::openmode _Mode = ios_base::out,
|
||||
//size_t _Size = static_cast<size_t>(BUFSIZ));
|
||||
size_t _Size = static_cast<size_t>(1));
|
||||
|
||||
/**
|
||||
* @brief Create a stream using an open file descriptor.
|
||||
* @param fd An open file descriptor.
|
||||
@param Mode Same meaning as in a standard filebuf.
|
||||
@param Size Optimal or preferred size of internal buffer, in chars.
|
||||
Defaults to system's @c BUFSIZ.
|
||||
*/
|
||||
#if !LL_WINDOWS
|
||||
explicit llofstream(int __fd,
|
||||
ios_base::openmode _Mode = ios_base::out,
|
||||
//size_t _Size = static_cast<size_t>(BUFSIZ));
|
||||
size_t _Size = static_cast<size_t>(1));
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief The destructor does nothing.
|
||||
*
|
||||
* The file is closed by the filebuf object, not the formatting
|
||||
* stream.
|
||||
*/
|
||||
virtual ~llofstream() {}
|
||||
|
||||
// Members:
|
||||
/**
|
||||
* @brief Accessing the underlying buffer.
|
||||
* @return The current basic_filebuf buffer.
|
||||
*
|
||||
* This hides both signatures of std::basic_ios::rdbuf().
|
||||
*/
|
||||
llstdio_filebuf* rdbuf() const
|
||||
{ return const_cast<llstdio_filebuf*>(&_M_filebuf); }
|
||||
|
||||
/**
|
||||
* @brief Wrapper to test for an open file.
|
||||
* @return @c rdbuf()->is_open()
|
||||
*/
|
||||
bool is_open() const;
|
||||
|
||||
/**
|
||||
* @brief Opens an external file.
|
||||
* @param Filename The name of the file.
|
||||
@@ -418,23 +187,11 @@ public:
|
||||
* fails, @c failbit is set in the stream's error state.
|
||||
*/
|
||||
void open(const std::string& _Filename,
|
||||
ios_base::openmode _Mode = ios_base::out|ios_base::trunc)
|
||||
{ open(_Filename.c_str(), _Mode); }
|
||||
ios_base::openmode _Mode = ios_base::out | ios_base::trunc);
|
||||
void open(const char* _Filename,
|
||||
ios_base::openmode _Mode = ios_base::out|ios_base::trunc);
|
||||
|
||||
/**
|
||||
* @brief Close the file.
|
||||
*
|
||||
* Calls @c llstdio_filebuf::close(). If that function
|
||||
* fails, @c failbit is set in the stream's error state.
|
||||
*/
|
||||
void close();
|
||||
|
||||
private:
|
||||
llstdio_filebuf _M_filebuf;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @breif filesize helpers.
|
||||
|
||||
Reference in New Issue
Block a user