28 #ifndef DEF_IOUTIL_HPP
29 #define DEF_IOUTIL_HPP
39 void preada(
int f, T * tbuf,
size_t nbytes,
size_t off) {
41 char * buf = (
char*)tbuf;
43 size_t a = pread(f, buf, nbytes - nread, off + nread);
45 std::cout <<
"Error, could not read: " << strerror(errno) << std::endl;
51 assert(nread <= nbytes);
55 void preada_trunc(
int f, T * tbuf,
size_t nbytes,
size_t off) {
57 char * buf = (
char*)tbuf;
59 size_t a = pread(f, buf, nbytes-nread, off+nread);
63 memset(buf, 0, nbytes-nread);
73 size_t readfull(
int f, T ** buf) {
74 off_t sz = lseek(f, 0, SEEK_END);
75 lseek(f, 0, SEEK_SET);
76 *buf = (
char*)malloc(sz);
77 preada(f, *buf, sz, 0);
81 void pwritea(
int f, T * tbuf,
size_t nbytes,
size_t off) {
84 char * buf = (
char*)tbuf;
85 while(nwritten<nbytes) {
86 size_t a = pwrite(f, buf, nbytes-nwritten, off+nwritten);
87 if (a ==
size_t(-1)) {
88 logstream(
LOG_ERROR) <<
"f:" << f <<
" nbytes: " << nbytes <<
" written: " << nwritten <<
" off:" <<
89 off <<
" f: " << f <<
" error:" << strerror(errno) << std::endl;
98 void writea(
int f, T * tbuf,
size_t nbytes) {
100 char * buf = (
char*)tbuf;
101 while(nwritten<nbytes) {
102 size_t a = write(f, buf, nbytes-nwritten);
104 if (a ==
size_t(-1)) {
105 logstream(
LOG_ERROR) <<
"Could not write " << (nbytes-nwritten) <<
" bytes!" <<
" error:" << strerror(errno) << std::endl;
114 template <
typename T>
115 void checkarray_filesize(std::string fname,
size_t nelements) {
117 int f = open(fname.c_str(), O_RDWR | O_CREAT, S_IROTH | S_IWOTH | S_IWUSR | S_IRUSR);
119 logstream(
LOG_ERROR) <<
"Error initializing the data-file: " << fname <<
" error:" << strerror(errno) << std::endl; }
121 int err = ftruncate(f, nelements *
sizeof(T));
123 logstream(
LOG_ERROR) <<
"Error in adjusting file size: " << fname <<
" to size: " << nelements *
sizeof(T)
124 <<
" error:" << strerror(errno) << std::endl;