fixed SF# 2164227

This commit is contained in:
Guenter Obiltschnig
2009-06-17 11:47:37 +00:00
parent db077087b1
commit b17b771b10

View File

@@ -1,223 +1,226 @@
// //
// FileStream.cpp // FileStream.cpp
// //
// $Id: //poco/svn/Foundation/src/FileStream_WIN32.cpp#2 $ // $Id: //poco/1.3/Foundation/src/FileStream_WIN32.cpp#4 $
// //
// Library: Foundation // Library: Foundation
// Package: Streams // Package: Streams
// Module: FileStream // Module: FileStream
// //
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by // obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute, // this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the // execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to // Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following: // do so, all subject to the following:
// //
// The copyright notices in the Software and this entire statement, including // The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer, // the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and // must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative // all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by // works are solely in the form of machine-executable object code generated by
// a source language processor. // a source language processor.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#include "Poco/FileStream.h" #include "Poco/FileStream.h"
#include "Poco/File.h" #include "Poco/File.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#if defined (POCO_WIN32_UTF8) #if defined (POCO_WIN32_UTF8)
#include "Poco/UnicodeConverter.h" #include "Poco/UnicodeConverter.h"
#endif #endif
namespace Poco { namespace Poco {
FileStreamBuf::FileStreamBuf(): FileStreamBuf::FileStreamBuf():
BufferedBidirectionalStreamBuf(BUFFER_SIZE, std::ios::in | std::ios::out), BufferedBidirectionalStreamBuf(BUFFER_SIZE, std::ios::in | std::ios::out),
_handle(INVALID_HANDLE_VALUE), _handle(INVALID_HANDLE_VALUE),
_pos(0) _pos(0)
{ {
} }
FileStreamBuf::~FileStreamBuf() FileStreamBuf::~FileStreamBuf()
{ {
try try
{ {
close(); close();
} }
catch (...) catch (...)
{ {
} }
} }
void FileStreamBuf::open(const std::string& path, std::ios::openmode mode) void FileStreamBuf::open(const std::string& path, std::ios::openmode mode)
{ {
poco_assert (_handle == INVALID_HANDLE_VALUE); poco_assert (_handle == INVALID_HANDLE_VALUE);
_path = path; _path = path;
setMode(mode); setMode(mode);
DWORD access = 0; DWORD access = 0;
if (mode & std::ios::in) if (mode & std::ios::in)
access |= GENERIC_READ; access |= GENERIC_READ;
if (mode & std::ios::out) if (mode & std::ios::out)
access |= GENERIC_WRITE; access |= GENERIC_WRITE;
DWORD shareMode = FILE_SHARE_READ; DWORD shareMode = FILE_SHARE_READ;
DWORD creationDisp = OPEN_EXISTING; if (!(mode & std::ios::out))
if (mode & std::ios::trunc) shareMode |= FILE_SHARE_WRITE;
creationDisp = CREATE_ALWAYS;
else if (mode & std::ios::out) DWORD creationDisp = OPEN_EXISTING;
creationDisp = OPEN_ALWAYS; if (mode & std::ios::trunc)
creationDisp = CREATE_ALWAYS;
DWORD flags = FILE_ATTRIBUTE_NORMAL; else if (mode & std::ios::out)
creationDisp = OPEN_ALWAYS;
#if defined (POCO_WIN32_UTF8)
std::wstring utf16Path; DWORD flags = FILE_ATTRIBUTE_NORMAL;
UnicodeConverter::toUTF16(path, utf16Path);
_handle = CreateFileW(utf16Path.c_str(), access, shareMode, NULL, creationDisp, flags, NULL); #if defined (POCO_WIN32_UTF8)
#else std::wstring utf16Path;
_handle = CreateFileA(path.c_str(), access, shareMode, NULL, creationDisp, flags, NULL); UnicodeConverter::toUTF16(path, utf16Path);
#endif _handle = CreateFileW(utf16Path.c_str(), access, shareMode, NULL, creationDisp, flags, NULL);
#else
if (_handle == INVALID_HANDLE_VALUE) _handle = CreateFileA(path.c_str(), access, shareMode, NULL, creationDisp, flags, NULL);
File::handleLastError(_path); #endif
if ((mode & std::ios::ate) || (mode & std::ios::app)) if (_handle == INVALID_HANDLE_VALUE)
seekoff(0, std::ios::end, mode); File::handleLastError(_path);
}
if ((mode & std::ios::ate) || (mode & std::ios::app))
seekoff(0, std::ios::end, mode);
int FileStreamBuf::readFromDevice(char* buffer, std::streamsize length) }
{
if (INVALID_HANDLE_VALUE == _handle || !(getMode() & std::ios::in))
return -1; int FileStreamBuf::readFromDevice(char* buffer, std::streamsize length)
{
if (getMode() & std::ios::out) if (INVALID_HANDLE_VALUE == _handle || !(getMode() & std::ios::in))
sync(); return -1;
DWORD bytesRead(0); if (getMode() & std::ios::out)
BOOL rc = ReadFile(_handle, buffer, static_cast<DWORD>(length), &bytesRead, NULL); sync();
if (rc == 0)
File::handleLastError(_path); DWORD bytesRead(0);
BOOL rc = ReadFile(_handle, buffer, static_cast<DWORD>(length), &bytesRead, NULL);
_pos += bytesRead; if (rc == 0)
File::handleLastError(_path);
return static_cast<int>(bytesRead);
} _pos += bytesRead;
return static_cast<int>(bytesRead);
int FileStreamBuf::writeToDevice(const char* buffer, std::streamsize length) }
{
if (INVALID_HANDLE_VALUE == _handle || !(getMode() & std::ios::out))
return -1; int FileStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
{
if (getMode() & std::ios::app) if (INVALID_HANDLE_VALUE == _handle || !(getMode() & std::ios::out))
{ return -1;
LARGE_INTEGER li;
li.QuadPart = 0; if (getMode() & std::ios::app)
li.LowPart = SetFilePointer(_handle, li.LowPart, &li.HighPart, FILE_END); {
if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) LARGE_INTEGER li;
File::handleLastError(_path); li.QuadPart = 0;
_pos = li.QuadPart; li.LowPart = SetFilePointer(_handle, li.LowPart, &li.HighPart, FILE_END);
} if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
File::handleLastError(_path);
DWORD bytesWritten(0); _pos = li.QuadPart;
BOOL rc = WriteFile(_handle, buffer, static_cast<DWORD>(length), &bytesWritten, NULL); }
if (rc == 0)
File::handleLastError(_path); DWORD bytesWritten(0);
BOOL rc = WriteFile(_handle, buffer, static_cast<DWORD>(length), &bytesWritten, NULL);
_pos += bytesWritten; if (rc == 0)
File::handleLastError(_path);
return static_cast<int>(bytesWritten);
} _pos += bytesWritten;
return static_cast<int>(bytesWritten);
void FileStreamBuf::close() }
{
if (_handle != INVALID_HANDLE_VALUE)
{ void FileStreamBuf::close()
if (getMode() & std::ios::out) {
sync(); if (_handle != INVALID_HANDLE_VALUE)
{
CloseHandle(_handle); if (getMode() & std::ios::out)
_handle = INVALID_HANDLE_VALUE; sync();
}
} CloseHandle(_handle);
_handle = INVALID_HANDLE_VALUE;
}
std::streampos FileStreamBuf::seekoff(std::streamoff off, std::ios::seekdir dir, std::ios::openmode mode) }
{
if (INVALID_HANDLE_VALUE == _handle || !(getMode() & mode))
return -1; std::streampos FileStreamBuf::seekoff(std::streamoff off, std::ios::seekdir dir, std::ios::openmode mode)
{
if (getMode() & std::ios::out) if (INVALID_HANDLE_VALUE == _handle || !(getMode() & mode))
sync(); return -1;
std::streamoff adj; if (getMode() & std::ios::out)
if (mode & std::ios::in) sync();
adj = static_cast<std::streamoff>(egptr() - gptr());
else std::streamoff adj;
adj = 0; if (mode & std::ios::in)
adj = static_cast<std::streamoff>(egptr() - gptr());
resetBuffers(); else
adj = 0;
DWORD offset = FILE_BEGIN;
if (dir == std::ios::cur) resetBuffers();
{
offset = FILE_CURRENT; DWORD offset = FILE_BEGIN;
off -= adj; if (dir == std::ios::cur)
} {
else if (dir == std::ios::end) offset = FILE_CURRENT;
{ off -= adj;
offset = FILE_END; }
} else if (dir == std::ios::end)
{
LARGE_INTEGER li; offset = FILE_END;
li.QuadPart = off; }
li.LowPart = SetFilePointer(_handle, li.LowPart, &li.HighPart, offset);
LARGE_INTEGER li;
if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) li.QuadPart = off;
File::handleLastError(_path); li.LowPart = SetFilePointer(_handle, li.LowPart, &li.HighPart, offset);
_pos = li.QuadPart;
return std::streampos(static_cast<std::streamoff>(_pos)); if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
} File::handleLastError(_path);
_pos = li.QuadPart;
return std::streampos(static_cast<std::streamoff>(_pos));
std::streampos FileStreamBuf::seekpos(std::streampos pos, std::ios::openmode mode) }
{
if (INVALID_HANDLE_VALUE == _handle || !(getMode() & mode))
return -1; std::streampos FileStreamBuf::seekpos(std::streampos pos, std::ios::openmode mode)
{
if (getMode() & std::ios::out) if (INVALID_HANDLE_VALUE == _handle || !(getMode() & mode))
sync(); return -1;
resetBuffers(); if (getMode() & std::ios::out)
sync();
LARGE_INTEGER li;
li.QuadPart = pos; resetBuffers();
li.LowPart = SetFilePointer(_handle, li.LowPart, &li.HighPart, FILE_BEGIN);
LARGE_INTEGER li;
if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) li.QuadPart = pos;
File::handleLastError(_path); li.LowPart = SetFilePointer(_handle, li.LowPart, &li.HighPart, FILE_BEGIN);
_pos = li.QuadPart;
return std::streampos(static_cast<std::streamoff>(_pos)); if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
} File::handleLastError(_path);
_pos = li.QuadPart;
return std::streampos(static_cast<std::streamoff>(_pos));
} // namespace Poco }
} // namespace Poco