/* Copyright (C) 2006 db4objects Inc. http://www.db4o.com */ #include "Stdafx.h" #include "NativeIoAdapter.h" using namespace System; using namespace System::IO; using namespace System::Runtime::InteropServices; using namespace cli; #define LPCWSTR_FROM_MSTR(x) ((LPCWSTR) Marshal::StringToHGlobalUni(x).ToPointer()) #define FREE_LPCWSTR(x) (Marshal::FreeHGlobal(IntPtr((void *)x))) #define SEEK(h,p) (SetFilePointerEx(h, *reinterpret_cast(&p), 0, FILE_BEGIN)) namespace Db4objects { namespace Db4o { namespace IO { 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, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS | FILE_FLAG_SEQUENTIAL_SCAN, NULL); FREE_LPCWSTR(file); if (m_handle == INVALID_HANDLE_VALUE) throw gcnew IOException(String::Concat("Failed to get an handle to ", path)); if (initialLength > 0) { Seek(initialLength - 1); Write (gcnew array {0}, 1); } 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) { SEEK(m_handle, position); } 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::Copy(long long oldAddress, long long newAddress, int length) //{ // DWORD cbRead = 0; // void* buffer = LocalAlloc(LMEM_FIXED, length); // if (NULL != buffer) // { // SEEK(m_handle, oldAddress); // ReadFile(m_handle, buffer, length, &cbRead, NULL); // if (cbRead == length) // { // DWORD cbWritten = 0; // SEEK(m_handle, newAddress); // WriteFile(m_handle, buffer, length, &cbWritten, NULL); // } // // LocalFree(buffer); // } //} void NativeIoAdapter::Close() { CloseHandle(m_handle); } void NativeIoAdapter::Delete(String ^path) { File::Delete(path); } bool NativeIoAdapter::Exists(String ^path) { return File::Exists(path); } } } }