PLearn 0.1
|
#include <IntStream.h>
Public Member Functions | |
FilesIntStream (int nfiles, const char *files[]) | |
virtual void | seek (long position) |
move to given position | |
virtual int | next () |
return next available int from the stream and increment position | |
virtual int | current () |
return next available int from the stream | |
virtual long | size () |
total length of the stream | |
virtual void | reopen () |
virtual | ~FilesIntStream () |
Protected Member Functions | |
FilesIntStream (FilesIntStream &x) | |
disallow a copy | |
void | read_current () |
read from current current_file at next_pos_in_current_file into current_value | |
Protected Attributes | |
int | n_files |
number of files | |
const char * | file_names |
names of each of the files; | |
FILE ** | fp |
pointers to each of the files; | |
int | current_file |
between 0 and n_files-1 | |
int | next_pos_in_current_file |
position within current file | |
int * | sizes |
int | total_size |
sum_i sizes[i] | |
int | current_value |
just read at current position |
Definition at line 121 of file IntStream.h.
PLearn::FilesIntStream::FilesIntStream | ( | FilesIntStream & | x | ) | [inline, protected] |
disallow a copy
Definition at line 133 of file IntStream.h.
References PLERROR.
{ PLERROR("FilesIntStream can't be copied"); }
PLearn::FilesIntStream::FilesIntStream | ( | int | nfiles, |
const char * | files[] | ||
) |
Definition at line 123 of file IntStream.cc.
References file_names, fp, i, n_files, PLERROR, read_current(), sizes, and total_size.
: IntStream(-1), n_files(nfiles), file_names(files), current_file(0), next_pos_in_current_file(0) { fp=(FILE**)malloc(n_files*sizeof(FILE*)); sizes=(int*)calloc(n_files,sizeof(int)); total_size=0; for (int i=0;i<n_files;i++) { fp[i]=fopen(file_names[i],"rb"); if (!fp[i]) PLERROR("FilesIntStream::FilesIntStream, can't open file %s\n",file_names[i]); if (fseek(fp[i],0,SEEK_END)) PLERROR("In FileIntStream constructor: fseek(%s,0,SEEK_END) failed\n",file_names[i]); total_size+=(sizes[i] = (ftell(fp[i])/sizeof(int))); fseek(fp[i],0,SEEK_SET); } read_current(); }
PLearn::FilesIntStream::~FilesIntStream | ( | ) | [virtual] |
int PLearn::FilesIntStream::current | ( | ) | [virtual] |
return next available int from the stream
Reimplemented from PLearn::IntStream.
Definition at line 226 of file IntStream.cc.
References current_value.
{ return current_value; }
int PLearn::FilesIntStream::next | ( | ) | [virtual] |
return next available int from the stream and increment position
Reimplemented from PLearn::IntStream.
Definition at line 218 of file IntStream.cc.
References c, current_value, and read_current().
{ int c=current_value; read_current(); return c; }
void PLearn::FilesIntStream::read_current | ( | ) | [protected] |
read from current current_file at next_pos_in_current_file into current_value
Definition at line 157 of file IntStream.cc.
References current_file, current_value, file_names, fp, n_files, next_pos_in_current_file, PLERROR, PLearn::IntStream::pos, PLearn::reverse_int(), seek(), sizes, and total_size.
Referenced by FilesIntStream(), next(), and seek().
{ if (n_files<1) PLERROR("FilesIntStream::read_current(): no file opened"); if (pos==total_size) { seek(0); return; } if (next_pos_in_current_file==sizes[current_file]) { next_pos_in_current_file = 0; current_file++; if (current_file==n_files) { seek(0); return; } } if (fread(¤t_value,sizeof(int),1,fp[current_file])!=1) { int posit=ftell(fp[current_file]); // norman: added check. Can be done better #ifdef WIN32 fprintf(stderr,"process could not read 1 int from %s at position %d, ftell=%d\nerrno=%d,%s", file_names[current_file],next_pos_in_current_file+1, posit,errno,strerror(errno)); #else int pid=getpid(); fprintf(stderr,"process %d could not read 1 int from %s at position %d, ftell=%d\nerrno=%d,%s", pid,file_names[current_file],next_pos_in_current_file+1, posit,errno,strerror(errno)); #endif exit(1); } #ifdef BIGENDIAN reverse_int(¤t_value,1); #endif next_pos_in_current_file++; pos++; }
void PLearn::FilesIntStream::reopen | ( | ) | [virtual] |
re-open all the file pointers and seek(position()) this is useful when a forked child process wants access to the same stream without interfering with parent
Reimplemented from PLearn::IntStream.
Definition at line 142 of file IntStream.cc.
References file_names, fp, i, n_files, PLERROR, PLearn::IntStream::pos, and seek().
{ // re-open all the file pointers for (int i=0;i<n_files;i++) { fp[i]=fopen(file_names[i],"rb"); if (!fp[i]) PLERROR("FilesIntStream::reopen, can't open file %s\n",file_names[i]); fseek(fp[i],0,SEEK_SET); } // return to same position as previously seek(pos); }
void PLearn::FilesIntStream::seek | ( | long | position | ) | [virtual] |
move to given position
Reimplemented from PLearn::IntStream.
Definition at line 195 of file IntStream.cc.
References current_file, file_names, fp, i, j, n_files, next_pos_in_current_file, PLERROR, PLearn::IntStream::pos, read_current(), sizes, and total_size.
Referenced by read_current(), and reopen().
{ if (position<0 || position>=total_size) { fprintf(stderr,"FilesIntStream::seek(%ld), argument must be in [0,%d)\n", position,total_size); exit(1); } pos=0; int i; for (i=0;i<n_files-1 && position>=pos+sizes[i];i++) pos+=sizes[i]; next_pos_in_current_file=position-pos; for (int j=0;j<n_files;j++) { int p = (i==j)?next_pos_in_current_file*sizeof(int):0; if (fseek(fp[j],p,SEEK_SET)) PLERROR("In FileIntStream::seek fseek(%s,%d,SEEK_SET) failed\n",file_names[j],next_pos_in_current_file); } current_file=i; // pos will be incremented in read_current() pos=position-1; read_current(); }
long PLearn::FilesIntStream::size | ( | ) | [virtual] |
total length of the stream
Reimplemented from PLearn::IntStream.
Definition at line 232 of file IntStream.cc.
References total_size.
{ return total_size; }
int PLearn::FilesIntStream::current_file [protected] |
between 0 and n_files-1
Definition at line 127 of file IntStream.h.
Referenced by read_current(), and seek().
int PLearn::FilesIntStream::current_value [protected] |
just read at current position
Definition at line 131 of file IntStream.h.
Referenced by current(), next(), and read_current().
const char* PLearn::FilesIntStream::file_names [protected] |
names of each of the files;
Definition at line 125 of file IntStream.h.
Referenced by FilesIntStream(), read_current(), reopen(), and seek().
FILE** PLearn::FilesIntStream::fp [protected] |
pointers to each of the files;
Definition at line 126 of file IntStream.h.
Referenced by FilesIntStream(), read_current(), reopen(), seek(), and ~FilesIntStream().
int PLearn::FilesIntStream::n_files [protected] |
number of files
Definition at line 124 of file IntStream.h.
Referenced by FilesIntStream(), read_current(), reopen(), seek(), and ~FilesIntStream().
int PLearn::FilesIntStream::next_pos_in_current_file [protected] |
position within current file
Definition at line 128 of file IntStream.h.
Referenced by read_current(), and seek().
int* PLearn::FilesIntStream::sizes [protected] |
Definition at line 129 of file IntStream.h.
Referenced by FilesIntStream(), read_current(), seek(), and ~FilesIntStream().
int PLearn::FilesIntStream::total_size [protected] |
sum_i sizes[i]
Definition at line 130 of file IntStream.h.
Referenced by FilesIntStream(), read_current(), seek(), and size().