#4241: Poco::FileInputStream broken in 1.12.5 and 1.11.8.

This commit is contained in:
Günter Obiltschnig
2023-11-02 12:10:21 +01:00
parent 3da8ee633e
commit 743da564eb
2 changed files with 58 additions and 39 deletions

View File

@@ -47,27 +47,12 @@ class Foundation_API FileIOS: public virtual std::ios
/// On Windows platforms, UTF-8 encoded Unicode paths are correctly handled. /// On Windows platforms, UTF-8 encoded Unicode paths are correctly handled.
{ {
public: public:
FileIOS(std::ios::openmode defaultMode); FileIOS();
/// Creates the basic stream. /// Creates the basic stream.
~FileIOS(); ~FileIOS();
/// Destroys the stream. /// Destroys the stream.
void open(const std::string& path, std::ios::openmode mode);
/// Opens the file specified by path, using the given mode.
///
/// Throws a FileException (or a similar exception) if the file
/// does not exist or is not accessible for other reasons and
/// a new file cannot be created.
void open(const std::string& path);
/// Opens the file specified by path, using the default mode given
/// in the constructor.
///
/// Throws a FileException (or a similar exception) if the file
/// does not exist or is not accessible for other reasons and
/// a new file cannot be created.
void close(); void close();
/// Closes the file stream. /// Closes the file stream.
/// ///
@@ -80,7 +65,6 @@ public:
protected: protected:
FileStreamBuf _buf; FileStreamBuf _buf;
std::ios::openmode _defaultMode;
}; };
@@ -111,6 +95,14 @@ public:
~FileInputStream(); ~FileInputStream();
/// Destroys the stream. /// Destroys the stream.
void open(const std::string& path, std::ios::openmode mode = std::ios::in);
/// Opens the file specified by path, using the given mode, which
/// will always include std::ios::in (even if not specified).
///
/// Throws a FileException (or a similar exception) if the file
/// does not exist or is not accessible for other reasons and
/// a new file cannot be created.
}; };
@@ -139,9 +131,25 @@ public:
/// Throws a FileException (or a similar exception) if the file /// Throws a FileException (or a similar exception) if the file
/// does not exist or is not accessible for other reasons and /// does not exist or is not accessible for other reasons and
/// a new file cannot be created. /// a new file cannot be created.
///
/// NOTE: The default mode std::ios::out | std::ios::trunc is different from the default
/// for std::ofstream, which is std::ios::out only. This is for backwards compatibility
/// with earlier POCO versions.
~FileOutputStream(); ~FileOutputStream();
/// Destroys the FileOutputStream. /// Destroys the FileOutputStream.
void open(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::trunc);
/// Opens the file specified by path, using the given mode, which
/// always includes std::ios::out, even if not specified.
///
/// Throws a FileException (or a similar exception) if the file
/// does not exist or is not accessible for other reasons and
/// a new file cannot be created.
///
/// NOTE: The default mode std::ios::out | std::ios::trunc is different from the default
/// for std::ostream, which is std::ios::out only. This is for backwards compatibility
/// with earlier POCO versions.
}; };
@@ -168,9 +176,20 @@ public:
FileStream(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::in); FileStream(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::in);
/// Creates the FileStream for the file given by path, using /// Creates the FileStream for the file given by path, using
/// the given mode. /// the given mode.
///
/// NOTE: The default mode std::ios::in | std::ios::out is different from the default
/// for std::fstream, which is std::ios::out only. This is for backwards compatibility
/// with earlier POCO versions.
~FileStream(); ~FileStream();
/// Destroys the FileOutputStream. /// Destroys the FileOutputStream.
void open(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::in);
/// Opens the file specified by path, using the given mode.
///
/// Throws a FileException (or a similar exception) if the file
/// does not exist or is not accessible for other reasons and
/// a new file cannot be created.
}; };

View File

@@ -24,8 +24,7 @@
namespace Poco { namespace Poco {
FileIOS::FileIOS(std::ios::openmode defaultMode): FileIOS::FileIOS()
_defaultMode(defaultMode)
{ {
poco_ios_init(&_buf); poco_ios_init(&_buf);
} }
@@ -36,20 +35,6 @@ FileIOS::~FileIOS()
} }
void FileIOS::open(const std::string& path, std::ios::openmode mode)
{
clear();
_buf.open(path, mode);
}
void FileIOS::open(const std::string& path)
{
clear();
_buf.open(path, _defaultMode);
}
void FileIOS::close() void FileIOS::close()
{ {
if (!_buf.close()) if (!_buf.close())
@@ -66,14 +51,12 @@ FileStreamBuf* FileIOS::rdbuf()
FileInputStream::FileInputStream(): FileInputStream::FileInputStream():
FileIOS(std::ios::in),
std::istream(&_buf) std::istream(&_buf)
{ {
} }
FileInputStream::FileInputStream(const std::string& path, std::ios::openmode mode): FileInputStream::FileInputStream(const std::string& path, std::ios::openmode mode):
FileIOS(mode | std::ios::in),
std::istream(&_buf) std::istream(&_buf)
{ {
open(path, mode | std::ios::in); open(path, mode | std::ios::in);
@@ -85,15 +68,20 @@ FileInputStream::~FileInputStream()
} }
void FileInputStream::open(const std::string& path, std::ios::openmode mode)
{
clear();
_buf.open(path, mode | std::ios::in);
}
FileOutputStream::FileOutputStream(): FileOutputStream::FileOutputStream():
FileIOS(std::ios::out),
std::ostream(&_buf) std::ostream(&_buf)
{ {
} }
FileOutputStream::FileOutputStream(const std::string& path, std::ios::openmode mode): FileOutputStream::FileOutputStream(const std::string& path, std::ios::openmode mode):
FileIOS(mode | std::ios::out),
std::ostream(&_buf) std::ostream(&_buf)
{ {
open(path, mode | std::ios::out); open(path, mode | std::ios::out);
@@ -105,15 +93,20 @@ FileOutputStream::~FileOutputStream()
} }
void FileOutputStream::open(const std::string& path, std::ios::openmode mode)
{
clear();
_buf.open(path, mode | std::ios::out);
}
FileStream::FileStream(): FileStream::FileStream():
FileIOS(std::ios::in | std::ios::out),
std::iostream(&_buf) std::iostream(&_buf)
{ {
} }
FileStream::FileStream(const std::string& path, std::ios::openmode mode): FileStream::FileStream(const std::string& path, std::ios::openmode mode):
FileIOS(mode),
std::iostream(&_buf) std::iostream(&_buf)
{ {
open(path, mode); open(path, mode);
@@ -125,4 +118,11 @@ FileStream::~FileStream()
} }
void FileStream::open(const std::string& path, std::ios::openmode mode)
{
clear();
_buf.open(path, mode);
}
} // namespace Poco } // namespace Poco