00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef _VRFILESOURCE_H_
00038 #define _VRFILESOURCE_H_
00039
00040 #include <VrSource.h>
00041 #include <fstream>
00042
00043
00044 #include <sys/types.h>
00045 #include <sys/stat.h>
00046 #include <fcntl.h>
00047
00048
00049 #ifdef O_LARGEFILE
00050 #define OUR_O_LARGEFILE O_LARGEFILE
00051 #else
00052 #define OUR_O_LARGEFILE 0
00053 #endif
00054
00055 template<class oType>
00056 class VrFileSource : public VrSource<oType> {
00057 protected:
00058 FILE* fp;
00059 bool repeat;
00060 public:
00061 virtual const char *name() { return "VrFileSource"; }
00062 virtual int work2(VrSampleRange output, void *o[]);
00063
00064 VrFileSource (double sampling_freq, const char *file, bool repeat = false);
00065 virtual ~VrFileSource();
00066 };
00067
00068 template<class oType> int
00069 VrFileSource<oType>::work2(VrSampleRange output, void *ao[])
00070 {
00071 oType *o= ((oType **)ao)[0];
00072 int i;
00073 int size = output.size;
00074 int index;
00075
00076 sync (output.index);
00077
00078 index = 0;
00079 while (size) {
00080 i = fread(&o[index], sizeof(oType), size, fp);
00081
00082 size -= i;
00083 index += i;
00084
00085 if (size == 0)
00086 break;
00087
00088 if (i > 0)
00089 continue;
00090
00091
00092
00093
00094
00095 if (!repeat)
00096 break;
00097
00098 if (fseek(fp, 0, SEEK_SET) == -1) {
00099 fprintf(stderr, "[%s] fseek failed\n", __FILE__);
00100 exit(-1);
00101 }
00102 }
00103
00104 if (size > 0){
00105 cerr << "end of file, exiting\n";
00106 exit(0);
00107 }
00108
00109
00110 return output.size;
00111 }
00112
00113 template<class oType>
00114 VrFileSource<oType>::VrFileSource(double sampling_freq,
00115 const char* filename, bool arg_repeat)
00116 {
00117 setSamplingFrequency (sampling_freq);
00118 repeat = arg_repeat;
00119 int fd = -1;
00120 if ((fd = open (filename, O_RDONLY | OUR_O_LARGEFILE)) < 0){
00121 fprintf(stderr, "Could not open %s\n", filename);
00122 exit(1);
00123 }
00124
00125 if((fp = fdopen (fd,"rb"))==NULL) {
00126 fprintf(stderr, "Could not open %s\n", filename);
00127 exit(1);
00128 }
00129 }
00130
00131 template<class oType>
00132 VrFileSource<oType>::~VrFileSource()
00133 {
00134 fclose(fp);
00135 }
00136
00137 #undef OUR_O_LARGEFILE
00138 #endif