?? posix.h
字號:
//// Copyright (c) 2002 by Istv醤 V醨adi//// This file is part of dxr3Player, a DVD player written specifically // for the DXR3 (aka Hollywood+) decoder card.// 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; either version 2 of the License, or// (at your option) any later version.//// 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 for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#ifndef DXR3PLAYER_POSIX_H#define DXR3PLAYER_POSIX_H//------------------------------------------------------------------------------#include <cstdarg>#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <sys/socket.h>#include <sys/types.h>#include <sys/ioctl.h>//------------------------------------------------------------------------------//------------------------------------------------------------------------------/** * Wrappers for POSIX functions to handle errors uniformly. */class POSIX{public: /** * Wrapper for the open() system call which aborts the program if * fails. */ static int open(const char* pathname, int flags, const char* errorMessage = "open", ...); /** * Wrapper for the pipe system call which aborts the program if * fails. */ static void pipe(int filedes[2], const char* errorMessage = "pipe", ...); /** * Wrapper for the ioctl() system call which aborts the program if * fails. */ static int ioctl(int d, int request, void* arg, const char* errorMessage = "ioctl", ...); /** * Wrapper for the ioctl() system call which aborts the program if * fails. */ static int ioctl(int d, int request, void* arg, const char* errorMessage, va_list& ap); /** * Wrapper for the fcntl() system call which aborts the program if * fails. */ static int fcntl(int fd, int cmd, const char* errorMessage = "fcntl", ...); /** * Wrapper for the fcntl() system call which aborts the program if * fails. */ static int fcntl(int fd, int cmd, long arg, const char* errorMessage = "fcntl", ...); /** * Wrapper for the fcntl() system call which aborts the program if * fails. */ static int fcntl(int fd, int cmd, void* arg, const char* errorMessage = "fcntl", ...); /** * Wrapper for the select system call which aborts the program if * fails. */ static int select(int n, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout, const char* errorMessage = "select", ...); /** * Wrapper for the read() system call which aborts the program if * fails. */ static ssize_t read(int fd, void* buf, size_t count, const char* errorMessage = "read", ...); /** * Wrapper for the write() system call which aborts the program if * fails. */ static ssize_t write(int fd, const void* buf, size_t count, const char* errorMessage = "write", ...); /** * Wrapper for the lseek() system call which aborts the program if * fails. */ static off_t lseek(int fd, off_t offset, int whence, const char* errorMessage = "lseek", ...); /** * Wrapper for the socket() system call which aborts the program if * fails. */ static int socket(int domain, int type, int protocol, const char* errorMessage = "socket", ...); /** * Wrapper for the connect() system call which aborts the program if * fails. */ static int connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen, const char* errorMessage = "connect", ...); /** * Wrapper for the close() system call which aborts the program if * fails. */ static void close(int fd, const char* errorMessage = "close", ...);private: /** * Handle the given integer result. If -1, the program is aborted * with error. */ static int handleResult(int result, const char* errorMessage, va_list& ap, bool nonblocking = false); /** * Abort the program. The current error message is printed beforehand. */ static void abort(const char* errorMessage, va_list& ap);};//------------------------------------------------------------------------------// Inline definitions//------------------------------------------------------------------------------inline int POSIX::handleResult(int result, const char* errorMessage, va_list& ap, bool nonblocking){ if (result==-1) { if (nonblocking && errno==EAGAIN) { result = 0; } else { abort(errorMessage, ap); } } va_end(ap); return result;}//------------------------------------------------------------------------------//------------------------------------------------------------------------------inline int POSIX::open(const char* pathname, int flags, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::open(pathname, flags), errorMessage, ap);}//------------------------------------------------------------------------------inline void POSIX::pipe(int filedes[2], const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); handleResult(::pipe(filedes), errorMessage, ap);}//------------------------------------------------------------------------------inline int POSIX::ioctl(int d, int request, void* arg, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return ioctl(d, request, arg, errorMessage, ap);}//------------------------------------------------------------------------------inline int POSIX::ioctl(int d, int request, void* arg, const char* errorMessage, va_list& ap){ return handleResult(::ioctl(d, request, arg), errorMessage, ap);}//------------------------------------------------------------------------------inline int POSIX::fcntl(int fd, int cmd, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::fcntl(fd, cmd), errorMessage, ap);}//------------------------------------------------------------------------------inline int POSIX::fcntl(int fd, int cmd, long arg, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::fcntl(fd, cmd, arg), errorMessage, ap);}//------------------------------------------------------------------------------inline int POSIX::fcntl(int fd, int cmd, void* arg, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::fcntl(fd, cmd, arg), errorMessage, ap);}//------------------------------------------------------------------------------inline ssize_t POSIX::read(int fd, void* buf, size_t count, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::read(fd, buf, count), errorMessage, ap);}//------------------------------------------------------------------------------inline ssize_t POSIX::write(int fd, const void* buf, size_t count, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::write(fd, buf, count), errorMessage, ap, true);}//------------------------------------------------------------------------------inline off_t POSIX::lseek(int fd, off_t offset, int whence, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::lseek(fd, offset, whence), errorMessage, ap);}//------------------------------------------------------------------------------inline int POSIX::socket(int domain, int type, int protocol, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::socket(domain, type, protocol), errorMessage, ap);}//------------------------------------------------------------------------------inline int POSIX::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); return handleResult(::connect(sockfd, serv_addr, addrlen), errorMessage, ap);}//------------------------------------------------------------------------------inline void POSIX::close(int fd, const char* errorMessage, ...){ va_list ap; va_start(ap, errorMessage); handleResult(::close(fd), errorMessage, ap);}//------------------------------------------------------------------------------#endif // DXR3PLAYER_POSIX_H// Local variables:// mode: c++// End:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -