2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* simple POSIX file wrapper.
|
2009-04-18 18:14:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_FILE
|
|
|
|
#define INCLUDED_FILE
|
|
|
|
|
2011-03-23 15:43:35 +01:00
|
|
|
#include "lib/os_path.h"
|
2011-04-29 21:10:34 +02:00
|
|
|
#include "lib/posix/posix_aio.h" // opcode: LIO_READ or LIO_WRITE
|
2011-03-21 18:53:13 +01:00
|
|
|
|
2009-04-18 18:14:48 +02:00
|
|
|
namespace ERR
|
|
|
|
{
|
2011-05-03 14:38:42 +02:00
|
|
|
const Status FILE_ACCESS = -110300;
|
|
|
|
const Status FILE_NOT_FOUND = -110301;
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
LIB_API Status FileOpen(const OsPath& pathname, int opcode, int& fd);
|
2011-04-29 21:10:34 +02:00
|
|
|
LIB_API void FileClose(int& fd);
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
class File
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
File()
|
2011-04-29 21:10:34 +02:00
|
|
|
: pathname(), fd(-1)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
File(const OsPath& pathname, int opcode)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
(void)Open(pathname, opcode);
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
~File()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status Open(const OsPath& pathname, int opcode)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(FileOpen(pathname, opcode, fd));
|
2011-04-29 21:10:34 +02:00
|
|
|
this->pathname = pathname;
|
|
|
|
this->opcode = opcode;
|
|
|
|
return INFO::OK;
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
void Close()
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
FileClose(fd);
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
const OsPath& Pathname() const
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
return pathname;
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
int Descriptor() const
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
return fd;
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
int Opcode() const
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
return opcode;
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-04-29 21:10:34 +02:00
|
|
|
OsPath pathname;
|
|
|
|
int fd;
|
|
|
|
int opcode;
|
2009-08-24 23:04:01 +02:00
|
|
|
};
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
typedef shared_ptr<File> PFile;
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
#endif // #ifndef INCLUDED_FILE
|