Skip to content

Commit

Permalink
#4241: Poco::FileInputStream broken in 1.12.5 and 1.11.8.
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Nov 2, 2023
1 parent 3da8ee6 commit 743da56
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
53 changes: 36 additions & 17 deletions Foundation/include/Poco/FileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,12 @@ class Foundation_API FileIOS: public virtual std::ios
/// On Windows platforms, UTF-8 encoded Unicode paths are correctly handled.
{
public:
FileIOS(std::ios::openmode defaultMode);
FileIOS();
/// Creates the basic stream.

~FileIOS();
/// 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();
/// Closes the file stream.
///
Expand All @@ -80,7 +65,6 @@ class Foundation_API FileIOS: public virtual std::ios

protected:
FileStreamBuf _buf;
std::ios::openmode _defaultMode;
};


Expand Down Expand Up @@ -111,6 +95,14 @@ class Foundation_API FileInputStream: public FileIOS, public std::istream

~FileInputStream();
/// 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.
};


Expand Down Expand Up @@ -139,9 +131,25 @@ class Foundation_API FileOutputStream: public FileIOS, public std::ostream
/// 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::ofstream, which is std::ios::out only. This is for backwards compatibility
/// with earlier POCO versions.

~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.
};


Expand All @@ -168,9 +176,20 @@ class Foundation_API FileStream: public FileIOS, public std::iostream
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
/// 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();
/// 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.
};


Expand Down
44 changes: 22 additions & 22 deletions Foundation/src/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
namespace Poco {


FileIOS::FileIOS(std::ios::openmode defaultMode):
_defaultMode(defaultMode)
FileIOS::FileIOS()
{
poco_ios_init(&_buf);
}
Expand All @@ -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()
{
if (!_buf.close())
Expand All @@ -66,14 +51,12 @@ FileStreamBuf* FileIOS::rdbuf()


FileInputStream::FileInputStream():
FileIOS(std::ios::in),
std::istream(&_buf)
{
}


FileInputStream::FileInputStream(const std::string& path, std::ios::openmode mode):
FileIOS(mode | std::ios::in),
std::istream(&_buf)
{
open(path, mode | std::ios::in);
Expand All @@ -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():
FileIOS(std::ios::out),
std::ostream(&_buf)
{
}


FileOutputStream::FileOutputStream(const std::string& path, std::ios::openmode mode):
FileIOS(mode | std::ios::out),
std::ostream(&_buf)
{
open(path, mode | std::ios::out);
Expand All @@ -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():
FileIOS(std::ios::in | std::ios::out),
std::iostream(&_buf)
{
}


FileStream::FileStream(const std::string& path, std::ios::openmode mode):
FileIOS(mode),
std::iostream(&_buf)
{
open(path, mode);
Expand All @@ -125,4 +118,11 @@ FileStream::~FileStream()
}


void FileStream::open(const std::string& path, std::ios::openmode mode)
{
clear();
_buf.open(path, mode);
}


} // namespace Poco

0 comments on commit 743da56

Please sign in to comment.