Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

165 lines (131 sloc) 3.753 kB
/*
* Copyright (C) 2010 Ajax.org BV
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this library; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef O3_C_STREAM_WIN32_H
#define O3_C_STREAM_WIN32_H
#include <tools_win32.h>
namespace o3 {
struct cStream : cStreamBase {
cStream() : m_handle(0)
{
}
cStream(void* handle)
: m_handle(handle)
{
}
virtual ~cStream()
{
o3_trace_scrfun("~cStream");
if (m_handle)
::CloseHandle(m_handle);
m_handle = 0;
}
o3_begin_class(cStreamBase)
o3_add_iface(iStream)
o3_end_class()
o3_glue_gen()
HANDLE m_handle;
public:
static siStream create(void* handle) {
o3_trace_scrfun("create");
return o3_new(cStream)(handle);
}
void setHandle(void* handle) {
o3_trace_scrfun("setHandle");
m_handle = (HANDLE) handle;
}
size_t write(const void* buf, size_t nbytes) {
o3_trace_scrfun("write");
DWORD length;
if ( ! ::WriteFile( m_handle, buf, DWORD(nbytes), &length, NULL ) )
return 0;
return (DWORD) length;
}
size_t read(void* buf, size_t nbytes) {
o3_trace_scrfun("read");
DWORD length;
if ( ! ::ReadFile( m_handle, buf, DWORD(nbytes), &length, NULL ) )
return 0;
return (size_t) length;
}
virtual size_t write(const char* data) {
o3_trace_scrfun("write");
return write(data, strLen(data));
}
o3_get size_t size() {
o3_trace_scrfun("size");
DWORD high = 0;
DWORD low = ::GetFileSize(m_handle, &high);
if(INVALID_FILE_SIZE == low)
return 0;
ULARGE_INTEGER ret;
ret.HighPart = high;
ret.LowPart = low;
return (size_t) ret.QuadPart;
}
size_t pos() {
o3_trace_scrfun("pos");
return ::SetFilePointer(m_handle,0,0,FILE_CURRENT);
}
size_t setPos(size_t pos) {
o3_trace_scrfun("setPos");
LARGE_INTEGER li;
li.QuadPart = pos;
li.LowPart = ::SetFilePointer(m_handle,li.LowPart,&li.HighPart,FILE_BEGIN);
if ( INVALID_SET_FILE_POINTER == li.LowPart )
return this->pos();
return pos;
}
bool eof() {
o3_trace_scrfun("eof");
unsigned char buf[2];
if (0 == read(buf,1))
return true;
LARGE_INTEGER li;
li.QuadPart = -1;
::SetFilePointer(m_handle,li.LowPart,&li.HighPart,FILE_CURRENT);
return false;
}
bool close() {
o3_trace_scrfun("close");
BOOL res(TRUE);
if (m_handle)
res = ::CloseHandle(m_handle);
m_handle = 0;
return res ? true : false;
}
bool flush() {
o3_trace_scrfun("flush");
return true;
}
static siScr factory() {
o3_trace_scrfun("factory");
siScr ret = o3_new(cStream);
return ret;
}
bool error(){
o3_trace_scrfun("error");
return false;
}
void* unwrap()
{
o3_trace_scrfun("unwrap");
return m_handle;
}
};
}
#endif // O3_C_STREAM_WIN32_H
Jump to Line
Something went wrong with that request. Please try again.