00001
00002
00003
00004
00005 #include <windows.h>
00006 #include "file.hpp"
00007
00008
00009
00010 struct ADR_FileHandle
00011 {
00012 HANDLE handle;
00013 };
00014
00015
00016 static ADR_FILE DefaultFileOpenA(const char* filename);
00017
00018
00020
00021 ADR_FILE ADR_CALL DefaultFileOpen(void* , const char* filename)
00022 {
00023
00024
00025 int wfilename_length = MultiByteToWideChar(
00026 CP_UTF8, 0,
00027 filename, -1,
00028 0, NULL);
00029
00030
00031 WCHAR* wfilename = new WCHAR[wfilename_length + 1];
00032 wfilename[wfilename_length] = 0;
00033 int result = MultiByteToWideChar(
00034 CP_UTF8, 0,
00035 filename, -1,
00036 wfilename, wfilename_length + 1);
00037
00038 if (result == 0) {
00039 delete[] wfilename;
00040 return DefaultFileOpenA(filename);
00041 }
00042
00043 HANDLE handle = CreateFileW(
00044 wfilename, GENERIC_READ, FILE_SHARE_READ, NULL,
00045 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
00046 if (handle == INVALID_HANDLE_VALUE) {
00047 return DefaultFileOpenA(filename);
00048 }
00049
00050 delete[] wfilename;
00051
00052 ADR_FileHandle* file = new ADR_FileHandle;
00053 file->handle = handle;
00054 return file;
00055 }
00056
00058
00059 ADR_FILE DefaultFileOpenA(const char* filename)
00060 {
00061 HANDLE handle = CreateFileA(
00062 filename, GENERIC_READ, FILE_SHARE_READ, NULL,
00063 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
00064 if (handle == INVALID_HANDLE_VALUE) {
00065 return NULL;
00066 }
00067
00068 ADR_FileHandle* file = new ADR_FileHandle;
00069 file->handle = handle;
00070 return file;
00071 }
00072
00074
00075 void ADR_CALL DefaultFileClose(ADR_FILE file)
00076 {
00077 CloseHandle(file->handle);
00078 delete file;
00079 }
00080
00082
00083 int ADR_CALL DefaultFileRead(ADR_FILE file, void* buffer, int size)
00084 {
00085 DWORD read;
00086 BOOL result = ReadFile(file->handle, buffer, size, &read, NULL);
00087 return (result ? read : 0);
00088 }
00089
00091
00092 int ADR_CALL DefaultFileSeek(ADR_FILE file, int destination)
00093 {
00094 int d = SetFilePointer(file->handle, destination, NULL, FILE_BEGIN);
00095 return (d != destination);
00096 }
00097
00099
00100 int ADR_CALL DefaultFileTell(ADR_FILE file)
00101 {
00102 return SetFilePointer(file->handle, 0, NULL, FILE_CURRENT);
00103 }
00104