fileapi.cpp

来自「天之炼狱1服务器端源文件游戏服务端不完整」· C++ 代码 · 共 698 行 · 第 1/2 页

CPP
698
字号
////////////////////////////////////////////////////////////////////////// FileAPI.cpp//// by Reiot//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// include files//////////////////////////////////////////////////#include "FileAPI.h"#include "Assert.h"#if __WINDOWS__#include <io.h>			// for _open()#include <fcntl.h>		// for _open()/_close()/_read()/_write()...#include <string.h>		// for memcpy()#elif __LINUX__#include <sys/types.h>	// for open()#include <sys/stat.h>	// for open()#include <unistd.h>		// for fcntl()#include <fcntl.h>		// for fcntl()#include <sys/ioctl.h>	// for ioctl()#include <errno.h>		// for errno#endif//////////////////////////////////////////////////// external variables//////////////////////////////////////////////////#if __LINUX__extern int errno;#endif////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int FileAPI::open_ex ( const char * filename , int flags )     throw ( IOException , Error ){	__BEGIN_TRY#if __LINUX__	int fd = open(filename,flags);#elif __WINDOWS__	int fd = _open(filename,flags);#endif	if ( fd < 0 ) {#if __LINUX__		switch ( errno ) {		case EEXIST : 			throw FileAlreadyExistException(filename);		case ENOENT  : 			throw FileNotExistException(filename);		case EISDIR : 			throw Error("pathname refers to a directory and the access requested involved writing.");		case EACCES : 			throw FileNotOpenedException(filename);		case ENAMETOOLONG : 			throw Error("pathname was too long.");		case ENOTDIR : 			throw Error("A component used as a directory in pathname is not, in fact, a  directory, or O_DIRECTORY was specified and pathname was not a directory.");		case ENXIO   : 			throw Error("O_NONBLOCK | O_WRONLY is set, the named file id a FIFO and no process has the file open for reading. Or, the file is a device special file and no corresponding device exists.");		case ENODEV  : 			throw Error("pathname refers to a device special file and no corresponding device exists.");		case EROFS   : 			throw Error("pathname refers to a file on a read-only filesystem and write access was requested.");		case ETXTBSY : 			throw Error("pathname refers to an executable image which is currently being executed and write access was requested.");		case EFAULT  : 			throw Error("pathname points outside your accessible address space.");		case ELOOP   : 			throw Error("Too many symbolic links were encountered in resolving pathname, or O_NOFOLLOW was specified but pathname was a symbolic link.");		case ENOSPC  : 			throw Error("pathname was to be created but the device containing pathname has no room for the new file.");		case ENOMEM  : 			throw Error("Insufficient kernel memory was available.");		case EMFILE  : 			throw Error("The process already has the maximum number of files open.");		case ENFILE  : 			throw Error("The limit on the total number of files open on the system has been reached.");		default :			throw UnknownError(strerror(errno),errno);		}//end of switch#elif __WINDOWS__	// ...#endif	}	return fd;	__END_CATCH}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int FileAPI::open_ex ( const char * filename , int flags , int mode )     throw ( IOException , Error ){	__BEGIN_TRY#if __LINUX__	int fd = open(filename,flags,mode);#elif __WINDOWS__	int fd = _open(filename,flags,mode);#endif	if ( fd < 0 ) {#if __LINUX__		switch ( errno ) {		case EEXIST : 			throw FileAlreadyExistException("pathname already exists and O_CREAT and O_EXCL were used.");		case EISDIR : 			throw Error("pathname refers to a directory and the access requested involved writing.");		case EACCES : 			throw FileNotOpenedException("The requested access to the file is not allowed, or one of the directories in pathname did not allow search (execute) permission, or the file did not exist yet and write access to the parent directory is not allowed.");		case ENAMETOOLONG : 			throw Error("pathname was too long.");		case ENOENT  : 			throw Error("A directory component in pathname does not exist or is a dangling symbolic link.");		case ENOTDIR : 			throw Error("A component used as a directory in pathname is not, in fact, a  directory, or O_DIRECTORY was specified and pathname was not a directory.");		case ENXIO   : 			throw Error("O_NONBLOCK | O_WRONLY is set, the named file id a FIFO and no process has the file open for reading. Or, the file is a device special file and no corresponding device exists.");		case ENODEV  : 			throw Error("pathname refers to a device special file and no corresponding device exists.");		case EROFS   : 			throw Error("pathname refers to a file on a read-only filesystem and write access was requested.");		case ETXTBSY : 			throw Error("pathname refers to an executable image which is currently being executed and write access was requested.");		case EFAULT  : 			throw Error("pathname points outside your accessible address space.");		case ELOOP   : 			throw Error("Too many symbolic links were encountered in resolving pathname, or O_NOFOLLOW was specified but pathname was a symbolic link.");		case ENOSPC  : 			throw Error("pathname was to be created but the device containing pathname has no room for the new file.");		case ENOMEM  : 			throw Error("Insufficient kernel memory was available.");		case EMFILE  : 			throw Error("The process already has the maximum number of files open.");		case ENFILE  : 			throw Error("The limit on the total number of files open on the system has been reached.");		default :			throw UnknownError(strerror(errno),errno);		}//end of switch#elif __WINDOWS__	// ...#endif	}	return fd;	__END_CATCH}////////////////////////////////////////////////////////////////////////// uint FileAPI::read_ex ( int fd , void * buf , uint len ) //      throw ( IOException , Error );//// exception version of read()//// Parameters //     fd  - file descriptor//     buf - reading buffer//     len - reading length//// Return//     length of reading bytes//// Exceptions//     ...////////////////////////////////////////////////////////////////////////uint FileAPI::read_ex ( int fd , void * buf , uint len ) 	throw ( IOException , Error ){	__BEGIN_TRY#if __LINUX__	int result = read ( fd , buf , len );#elif __WINDOWS__	int result = _read ( fd , buf , len );#endif	if ( result < 0 ) {#if __LINUX__		switch ( errno ) {			case EINTR : 				throw InterruptedIOException("The call was interrupted by a signal before any data was read.");			case EAGAIN : 				throw NonBlockingIOException("Non-blocking I/O has been selected using O_NONBLOCK and no data was immediately available for reading.");			case EBADF : 				throw Error("fd is not a valid file descriptor or is not open for reading.");			case EIO : 				throw Error("I/O error."); 			case EISDIR : 				throw Error("fd refers to a directory.");			case EINVAL : 				throw Error("fd is attached to an object which is unsuitable for reading.");			case EFAULT : 				throw Error("buf is outside your accessible address space.");			case ECONNRESET :				throw ConnectException("Connection reset by peer.");			default : 				throw UnknownError(strerror(errno),errno);		}#elif __WINDOWS__	// ...#endif	} else if ( result == 0 ) {		throw EOFException();	}	return result;	__END_CATCH}////////////////////////////////////////////////////////////////////////// uint FileAPI::write_ex ( int fd , void * buf , uint len ) //      throw ( IOException );//// exception version of write()//// Parameters //     fd  - file descriptor//     buf - writing buffer//     len - writing length//// Return//     length of reading bytes//// Exceptions//     ...////////////////////////////////////////////////////////////////////////uint FileAPI::write_ex ( int fd , const void * buf , uint len )      throw ( IOException , Error ){	__BEGIN_TRY#if __LINUX__	int result = write ( fd , buf , len );#elif __WINDOWS__	int result = _write ( fd , buf , len );#endif	if ( result < 0 ) {		#if __LINUX__		switch ( errno ) {			case EAGAIN : 				throw NonBlockingIOException("Non-blocking I/O has been selected using O_NONBLOCK and there was no room in the pipe or socket connected to fd to write the data immediately.");			case EINTR : 				throw InterruptedIOException("The call was interrupted by a signal before any data was written. ");			case EBADF : 				throw Error("fd is not a valid file descriptor or is not open for writing.");			case EPIPE : 				throw Error("fd is connected to a pipe or socket whose reading end is closed."); 			case EINVAL: 				throw Error("fd is attached to an object which is unsuitable for writing.");			case EFAULT: 				throw Error("buf is outside your accessible address space.");			case ENOSPC : 				throw Error("The device containing the file referred to by fd has no room for the data. ");			case EIO : 				throw Error("A low-level I/O error occurred while modifying the inode.");			case ECONNRESET :				throw ConnectException("Connection reset by peer.");			default : 				throw UnknownError(strerror(errno),errno);		}#elif __WINDOWS__	//...#endif	}	return result;	__END_CATCH}////////////////////////////////////////////////////////////////////////// void FileAPI::close_ex ( int fd ) //      throw ( FileNotOpenedException , Error )//// exception version of close()//// Parameters//     fd - file descriptor//// Return//     none//// Exceptions//     FileNotOpenedException - thrown when try to close file already closed//     Error////////////////////////////////////////////////////////////////////////void FileAPI::close_ex ( int fd )      throw ( FileNotOpenedException , Error ){	__BEGIN_TRY	if ( close(fd) < 0 ) {#if __LINUX__		switch ( errno ) {			case EBADF : 				throw FileNotOpenedException("fd isn't a valid open file descriptor.");			default :				throw UnknownError(strerror(errno),errno);		}#elif __WINDOWS__#endif	}	__END_CATCH}////////////////////////////////////////////////////////////////////////// int FileAPI::fcntl_ex ( int fd , int cmd ) //     throw ( Error );//// Parameters//     fd  - file descriptor//     cmd - file control command//// Return//     various according to cmd//// Exceptions//     Error////////////////////////////////////////////////////////////////////////int FileAPI::fcntl_ex ( int fd , int cmd ) 	throw ( Error ){	__BEGIN_TRY

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?