2012-01-21 21:57:41 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include "File.hpp"
|
|
|
|
|
|
|
|
CFile::CFile()
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd = NULL;
|
|
|
|
mem_file = NULL;
|
|
|
|
filesize = 0;
|
|
|
|
Pos = 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CFile::CFile(const char * filepath, const char * mode)
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd = NULL;
|
|
|
|
open(filepath, mode);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CFile::CFile(const u8 * mem, int size)
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd = NULL;
|
|
|
|
open(mem, size);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CFile::~CFile()
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
close();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFile::open(const char * filepath, const char * mode)
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
close();
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd = fopen(filepath, mode);
|
|
|
|
if(!file_fd)
|
|
|
|
return -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
fseek(file_fd, 0, SEEK_END);
|
|
|
|
filesize = ftell(file_fd);
|
|
|
|
rewind();
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
return 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFile::open(const u8 * mem, int size)
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
close();
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
mem_file = mem;
|
|
|
|
filesize = size;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
return 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFile::close()
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
if(file_fd)
|
|
|
|
fclose(file_fd);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd = NULL;
|
|
|
|
mem_file = NULL;
|
|
|
|
filesize = 0;
|
|
|
|
Pos = 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFile::read(u8 * ptr, size_t size)
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
if(file_fd)
|
|
|
|
{
|
|
|
|
int ret = fread(ptr, 1, size, file_fd);
|
|
|
|
if(ret > 0)
|
|
|
|
Pos += ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int readsize = size;
|
|
|
|
|
|
|
|
if(readsize > (long int) filesize-Pos)
|
|
|
|
readsize = filesize-Pos;
|
|
|
|
|
|
|
|
if(readsize <= 0)
|
|
|
|
return readsize;
|
|
|
|
|
|
|
|
if(mem_file != NULL)
|
|
|
|
{
|
|
|
|
memcpy(ptr, mem_file+Pos, readsize);
|
|
|
|
Pos += readsize;
|
|
|
|
return readsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFile::write(const u8 * ptr, size_t size)
|
|
|
|
{
|
|
|
|
/*
|
2012-07-26 00:12:17 +02:00
|
|
|
if(size < 0)
|
|
|
|
return size;
|
2012-01-21 21:57:41 +01:00
|
|
|
*/
|
2012-07-26 00:12:17 +02:00
|
|
|
if(file_fd)
|
|
|
|
{
|
|
|
|
int ret = fwrite(ptr, 1, size, file_fd);
|
|
|
|
if(ret > 0)
|
|
|
|
Pos += ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFile::seek(long int offset, int origin)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
if(origin == SEEK_SET)
|
|
|
|
Pos = offset;
|
|
|
|
else if(origin == SEEK_CUR)
|
|
|
|
Pos += offset;
|
|
|
|
else if(origin == SEEK_END)
|
|
|
|
Pos = filesize+offset;
|
|
|
|
if(Pos < 0)
|
|
|
|
{
|
|
|
|
Pos = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(file_fd)
|
|
|
|
ret = fseek(file_fd, Pos, SEEK_SET);
|
|
|
|
|
|
|
|
if(mem_file != NULL && Pos > (long int)filesize)
|
|
|
|
{
|
|
|
|
Pos = filesize;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|