RZ

001: // RZIO.hh # common C-style IO inlines
002:
003: #ifndef _RZIO_HH
004: #define _RZIO_HH 1
005:
006: #include <cerrno>
007: #include <cstdio>
008: #include <unistd.h>
009: #include <sys/uio.h>
010:
011: #include "RZError.hh"
012:
013: namespace RZ {
014:     inline std::FILE *fdopen_in(int fd)
015:     {
016:         std::FILE *FDFile = fdopen(fd, "r");
017:         BadDescriptor::what_if("Can't read from input file!");
018:         int s = lseek(fd, 0, SEEK_CUR);
019:         if (errno != ESPIPE) IOError::what_if("Can't reset file!", s != 0);
020:         return FDFile;
021:     }
022:
023:     inline std::FILE *fdopen_out(int fd)
024:     {
025:         std::FILE *FDFile = fdopen(fd, "a");
026:         BadDescriptor::what_if("Can't append to output file!");
027:         return FDFile;
028:     }
029:
030:     inline unsigned int read_part(int fd, char *base, unsigned int len)
031:     {
032:         int r;
033:         do r = read(fd, base, len);
034:         while (r == -1 && errno == EINTR);
035:         IOError::what_if("Can't read!");
036:         return r;
037:     }
038:
039:     inline unsigned int readv_part(int fd,
040:         struct iovec *vbase, unsigned int vlen)
041:     {
042:         int r;
043:         do r = readv(fd, vbase, vlen);
044:         while (r == -1 && errno == EINTR);
045:         IOError::what_if("Can't read!");
046:         return r;
047:     }
048:
049:     inline void fd_read(int fd, char *base, unsigned int len)
050:     {
051:         while (len) {
052:             int r = read_part(fd, base, len);
053:             if (!r) throw ReadError("Truncated read!");
054:             base += r;
055:             len -= r;
056:         }
057:     }
058:
059:     inline unsigned int fd_read_some(int fd, char *base, unsigned int len)
060:     {
061:         unsigned int length = 0;
062:         while (len) {
063:             int r = read_part(fd, base, len);
064:             length += r;
065:             if (!r) return length;
066:             base += r;
067:             len -= r;
068:         }
069:         return length;
070:     }
071:
072:     inline bool fd_read_if(int fd, char *base, unsigned int len)
073:     {
074:         if (len) {
075:             int r = read_part(fd, base, len);
076:             if (!r) return false;
077:             while (len -= r) {
078:                 r = read_part(fd, base, len);
079:                 if (!r) throw ReadError("Truncated read!");
080:                 base += r;
081:             }
082:         }
083:         return true;
084:     }
085:
086:     inline void fd_write(int fd, char const *base, unsigned int len)
087:     {
088:         while (len) {
089:             int r;
090:             do r = write(fd, base, len);
091:             while (r == -1 && errno == EINTR);
092:             IOError::what_if("Can't write!");
093:             base += r;
094:             len -= r;
095:         }
096:     }
097:
098:     inline void fd_writev(int fd, struct iovec *vbase, unsigned int vlen)
099:     {
100:         while (vlen) {
101:             int r;
102:             do r = writev(fd, vbase, vlen);
103:             while (r == -1 && errno == EINTR);
104:             IOError::what_if("Can't write!");
105:             for (; vlen; ++vbase, --vlen)
106:                 if (vbase->iov_len <= (unsigned int)r)
107:                     r -= vbase->iov_len;
108:                 else {
109:                     vbase->iov_base = (char *)(vbase->iov_base) + r;
110:                     vbase->iov_len -= r;
111:                     break;
112:                 }
113:         }
114:     }
115: }
116:
117: // RZIO.hh # common C-style IO inlines
118:
119: #endif