Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

VrFileSource.h

Go to the documentation of this file.
00001 /* -*- Mode: c++ -*- */
00002 /*
00003  * Copyright 2001,2002 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 /*
00023  *  Copyright 1997 Massachusetts Institute of Technology
00024  *
00025  *  Permission to use, copy, modify, distribute, and sell this software and its
00026  *  documentation for any purpose is hereby granted without fee, provided that
00027  *  the above copyright notice appear in all copies and that both that
00028  *  copyright notice and this permission notice appear in supporting
00029  *  documentation, and that the name of M.I.T. not be used in advertising or
00030  *  publicity pertaining to distribution of the software without specific,
00031  *  written prior permission.  M.I.T. makes no representations about the
00032  *  suitability of this software for any purpose.  It is provided "as is"
00033  *  without express or implied warranty.
00034  *
00035  */
00036 
00037 #ifndef _VRFILESOURCE_H_
00038 #define _VRFILESOURCE_H_
00039 
00040 #include <VrSource.h>
00041 #include <fstream>
00042 
00043 // This really ought to get split into a .cc file
00044 #include <sys/types.h>
00045 #include <sys/stat.h>
00046 #include <fcntl.h>
00047 
00048 // should be handled via configure
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)              // done
00086       break;
00087 
00088     if (i > 0)                  // short read, try again
00089       continue;
00090 
00091     // We got a zero from fread.  This is either EOF or error.  In
00092     // any event, if we're in repeat mode, seek back to the beginning
00093     // of the file and try again, else break
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){        // EOF or error
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

Generated on Tue Mar 30 21:31:52 2004 for GNU Radio by doxygen 1.3.2