/* Copyright (C) 2006 db4objects Inc. http://www.db4o.com */ #include "stdafx.h" #include "NativeIoAdapter.h" using namespace System; using namespace System::Runtime::InteropServices; using namespace Db4objects::Db4o::IO; #define LPCWSTR_FROM_MSTR(x) ((LPCWSTR) Marshal::StringToHGlobalUni(x).ToPointer()) #define FREE_LPCWSTR(x) (Marshal::FreeHGlobal(IntPtr((void *)x))) IoAdapter ^NativeIoAdapter::Open(String ^path, bool lock, long long initialLength) { LPCWSTR file = LPCWSTR_FROM_MSTR(path); m_handle = CreateFile( file, GENERIC_READ | GENERIC_WRITE, lock ? NULL : FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, NULL, NULL); FREE_LPCWSTR(file); return this; } long long NativeIoAdapter::GetLength() { return (long long)GetFileSize(m_handle, NULL); } int NativeIoAdapter::Read(array ^bytes, int length) { DWORD read; pin_ptr pb = &bytes[0]; ReadFile(m_handle, pb, length, &read, NULL); return (int)read; } void NativeIoAdapter::Seek(long long position) { SetFilePointer(m_handle, position, 0, FILE_BEGIN); } void NativeIoAdapter::Sync() { FlushFileBuffers(m_handle); } void NativeIoAdapter::Write(array ^bytes, int length) { pin_ptr pb = &bytes[0]; WriteFile(m_handle, pb, length, NULL, NULL); } void NativeIoAdapter::Close() { CloseHandle(m_handle); } void NativeIoAdapter::Delete(String ^path) { LPCWSTR file = LPCWSTR_FROM_MSTR(path); DeleteFileW(file); FREE_LPCWSTR(file); } bool NativeIoAdapter::Exists(String ^path) { bool exists; LPCWSTR file = LPCWSTR_FROM_MSTR(path); HANDLE handle = CreateFile( file, GENERIC_READ, NULL, NULL, OPEN_EXISTING, NULL, NULL); FREE_LPCWSTR(file); exists = handle != INVALID_HANDLE_VALUE; CloseHandle(handle); return exists; }