dolphin/Source/Plugins/Plugin_VideoOGL/Src/Windows/File.cpp

165 lines
3.2 KiB
C++

// Copyright (C) 2003-2008 Dolphin Project.
// This program 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, version 2.0.
// This program 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 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <stdio.h>
#include <stdlib.h>
#include "File.h"
namespace W32Util
{
File::File()
{
fileHandle = INVALID_HANDLE_VALUE;
isOpen=false;
}
File::~File()
{
}
bool File::Open(const TCHAR *filename, eFileMode _mode)
{
mode = _mode;
//it's time to open the file
fileHandle = CreateFile(filename,
mode==FILE_READ ? GENERIC_READ : GENERIC_WRITE, //open mode
mode == FILE_READ ? FILE_SHARE_READ : NULL, //sharemode
NULL, //security
mode==FILE_READ ? OPEN_EXISTING : CREATE_ALWAYS, //create mode
FILE_ATTRIBUTE_NORMAL, //atrributes
NULL); //template
if (fileHandle == INVALID_HANDLE_VALUE)
isOpen=false;
else
isOpen=true;
return isOpen;
}
void File::Close()
{
if (isOpen)
{
//close the file and reset variables
CloseHandle(fileHandle);
fileHandle=INVALID_HANDLE_VALUE;
isOpen=false;
}
}
int File::GetSize()
{
if (!isOpen) //of course
return 0;
else
return GetFileSize(fileHandle,0);
}
int File::Write(void *data, int size) //let's do some writing
{
if (isOpen)
{
DWORD written;
WriteFile(fileHandle, data, size, &written,0);
return written; //we return the number of bytes that actually got written
}
else
{
return 0;
}
}
int File::Read(void *data, int size)
{
if (isOpen)
{
DWORD wasRead;
ReadFile(fileHandle, data, size, &wasRead,0);
return wasRead; //we return the number of bytes that actually was read
}
else
{
return 0;
}
}
int File::WR(void *data, int size)
{
if (mode==FILE_READ)
return Read(data,size);
else
return Write(data,size);
}
bool File::MagicCookie(int cookie)
{
if (mode==FILE_READ)
{
if (ReadInt()!=cookie)
{
char mojs[5],temp[256];
mojs[4]=0;
*(int*)mojs=cookie;
sprintf(temp,"W32Util::File: Magic Cookie %s is bad!",mojs);
MessageBox(0,temp,"Error reading file",MB_ICONERROR);
return false;
}
else
return true;
}
else if (mode==FILE_WRITE)
{
WriteInt(cookie);
return true;
}
return false;
}
int File::ReadInt()
{
int temp;
if (Read(&temp, sizeof(int)))
return temp;
else
return 0;
}
void File::WriteInt(int i)
{
Write(&i,sizeof(int));
}
char File::ReadChar()
{
char temp;
if (Read(&temp, sizeof(char)))
return temp;
else
return 0;
}
void File::WriteChar(char i)
{
Write(&i,sizeof(char));
}
}