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

VrFileSink.h

Go to the documentation of this file.
00001 /* -*- Mode: c++ -*- */
00002 /*
00003  * Copyright 2001,2003 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 _VRFILESINK_H_
00038 #define _VRFILESINK_H_
00039 
00040 #include <VrSink.h>
00041 #include <fstream>
00042 #include <string.h>
00043 
00044 // This really ought to get split into a .cc file
00045 #include <sys/types.h>
00046 #include <sys/stat.h>
00047 #include <fcntl.h>
00048 
00049 // should be handled via configure
00050 #ifdef O_LARGEFILE
00051 #define OUR_O_LARGEFILE O_LARGEFILE
00052 #else
00053 #define OUR_O_LARGEFILE 0
00054 #endif
00055 
00056 //VrFileSink appends raw data to a file.
00057 //  default filename is "tmp"
00058 
00059 template<class iType> 
00060 class VrFileSink : public VrSink<iType> {
00061 protected:
00062   FILE* fp;
00063   char* filename;
00064 
00065 public:
00066   virtual const char *name() { return "VrFileSink"; }
00067 
00068   virtual int work3(VrSampleRange output, 
00069                     VrSampleRange inputs[], void *i[]);
00070 
00071   virtual void initialize() {
00072     if(fp != NULL) fclose(fp);
00073     int fd = -1;
00074     if ((fd = open (filename,
00075                     O_WRONLY | O_CREAT | O_TRUNC | OUR_O_LARGEFILE,
00076                     0664)) < 0){
00077       perror (filename);
00078       exit (1);
00079     }
00080     if((fp = fdopen(fd,"wb")) == 0) {
00081       perror (filename);
00082       // cout << "Error opening file, errno = " << errno << endl;
00083       exit(1);
00084     }
00085   }
00086 
00087   VrFileSink(char* file) {
00088     filename=new char[strlen(file) + 1];
00089     strcpy(filename,file);
00090     fp = NULL;
00091     //fp = fopen(file,"w");    
00092   }
00093 
00094   virtual ~VrFileSink() {
00095     if(fp!=NULL) fclose(fp);
00096     delete filename;
00097   }
00098 
00099 };
00100 
00101 template<class iType> int
00102 VrFileSink<iType>::work3(VrSampleRange output, 
00103                          VrSampleRange inputs[], void *ai[]) 
00104 {
00105   iType **i = (iType **)ai;
00106 
00107   sync (output.index);
00108 
00109   int count = 0;
00110 
00111   count =  fwrite(i[0],sizeof(iType),output.size,fp);
00112   while ((count < 0) && (errno == EAGAIN)) {
00113     YIELD();
00114     count =  fwrite(i[0],sizeof(iType),output.size,fp);
00115   }
00116 
00117   if(count<0) {
00118     fprintf(stderr, "VrConnect.fileWrite(): error writing to file\n");
00119     exit(1);
00120   } else {
00121     if((unsigned int) count!=output.size)
00122       fprintf(stderr, "VrConnect.fileWrite(): warning: not all bytes written\n");
00123     
00124   }
00125   return count;
00126 
00127 }
00128 
00129 #undef OUR_O_LARGEFILE
00130 #endif

Generated on Tue Mar 15 23:46:36 2005 for GNU Radio by  doxygen 1.4.0