Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

FileSink.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 
00003    Copyright (c) 2000 Tyrell Corporation. All rights reserved.
00004 
00005    Tyrell DarkIce
00006 
00007    File     : FileSink.cpp
00008    Version  : $Revision: 1.8 $
00009    Author   : $Author: darkeye $
00010    Location : $Source: /cvsroot/darkice/darkice/src/FileSink.cpp,v $
00011    
00012    Copyright notice:
00013 
00014     This program is free software; you can redistribute it and/or
00015     modify it under the terms of the GNU General Public License  
00016     as published by the Free Software Foundation; either version 2
00017     of the License, or (at your option) any later version.
00018    
00019     This program is distributed in the hope that it will be useful,
00020     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00022     GNU General Public License for more details.
00023    
00024     You should have received a copy of the GNU General Public License
00025     along with this program; if not, write to the Free Software
00026     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ------------------------------------------------------------------------------*/
00029 
00030 /* ============================================================ include files */
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035 
00036 #ifdef HAVE_UNISTD_H
00037 #include <unistd.h>
00038 #else
00039 #error need unistd.h
00040 #endif
00041 
00042 #ifdef HAVE_STDLIB_H
00043 #include <stdlib.h>
00044 #else
00045 #error need stdlib.h
00046 #endif
00047 
00048 #ifdef HAVE_SYS_TYPES_H
00049 #include <sys/types.h>
00050 #else
00051 #error need sys/types.h
00052 #endif
00053 
00054 #ifdef HAVE_ERRNO_H
00055 #include <errno.h>
00056 #else
00057 #error need errno.h
00058 #endif
00059 
00060 #ifdef HAVE_SYS_STAT_H
00061 #include <sys/stat.h>
00062 #else
00063 #error need sys/stat.h
00064 #endif
00065 
00066 #ifdef HAVE_FCNTL_H
00067 #include <fcntl.h>
00068 #else
00069 #error need fcntl.h
00070 #endif
00071 
00072 #ifdef HAVE_SYS_TIME_H
00073 #include <sys/time.h>
00074 #else
00075 #error need sys/time.h
00076 #endif
00077 
00078 #ifdef HAVE_STRING_H
00079 #include <string.h>
00080 #else
00081 #error need string.h
00082 #endif
00083 
00084 
00085 #include "Util.h"
00086 #include "Exception.h"
00087 #include "FileSink.h"
00088 
00089 
00090 /* ===================================================  local data structures */
00091 
00092 
00093 /* ================================================  local constants & macros */
00094 
00095 /*------------------------------------------------------------------------------
00096  *  File identity
00097  *----------------------------------------------------------------------------*/
00098 static const char fileid[] = "$Id: FileSink.cpp,v 1.8 2002/03/28 16:41:49 darkeye Exp $";
00099 
00100 
00101 /* ===============================================  local function prototypes */
00102 
00103 
00104 /* =============================================================  module code */
00105 
00106 /*------------------------------------------------------------------------------
00107  *  Initialize the object
00108  *----------------------------------------------------------------------------*/
00109 void
00110 FileSink :: init (  const char    * name )          throw ( Exception )
00111 {
00112     fileName = Util::strDup( name);
00113     fileDescriptor = 0;
00114 }
00115 
00116 
00117 /*------------------------------------------------------------------------------
00118  *  De-initialize the object
00119  *----------------------------------------------------------------------------*/
00120 void
00121 FileSink :: strip ( void)                           throw ( Exception )
00122 {
00123     if ( isOpen() ) {
00124         close();
00125     }
00126 
00127     delete[] fileName;
00128 }
00129 
00130 
00131 /*------------------------------------------------------------------------------
00132  *  Copy Constructor
00133  *----------------------------------------------------------------------------*/
00134 FileSink :: FileSink (  const FileSink &    fs )    throw ( Exception )
00135                 : Sink( fs )
00136 {
00137     int     fd;
00138     
00139     init( fs.fileName);
00140     
00141     if ( (fd = fs.fileDescriptor ? dup( fs.fileDescriptor) : 0) == -1 ) {
00142         strip();
00143         throw Exception( __FILE__, __LINE__, "dup failure");
00144     }
00145 
00146     fileDescriptor = fd;
00147 }
00148 
00149 
00150 /*------------------------------------------------------------------------------
00151  *  Assignment operator
00152  *----------------------------------------------------------------------------*/
00153 FileSink &
00154 FileSink :: operator= (  const FileSink &    fs )   throw ( Exception )
00155 {
00156     if ( this != &fs ) {
00157         int     fd;
00158 
00159         /* first strip */
00160         strip();
00161 
00162 
00163         /* then build up */
00164         Sink::operator=( fs );
00165         
00166         init( fs.fileName);
00167         
00168         if ( (fd = fs.fileDescriptor ? dup( fs.fileDescriptor) : 0) == -1 ) {
00169             strip();
00170             throw Exception( __FILE__, __LINE__, "dup failure");
00171         }
00172 
00173         fileDescriptor = fd;
00174     }
00175 
00176     return *this;
00177 }
00178 
00179 
00180 /*------------------------------------------------------------------------------
00181  *  Check wether a file exists and is regular file
00182  *----------------------------------------------------------------------------*/
00183 bool
00184 FileSink :: exists ( void ) const               throw ()
00185 {
00186     struct stat     st;
00187 
00188     if ( stat( (const char*)fileName, &st) == -1 ) {
00189         return false;
00190     }
00191 
00192     return S_ISREG( st.st_mode);
00193 }
00194 
00195 
00196 /*------------------------------------------------------------------------------
00197  *  Create a file, truncate if already exists
00198  *----------------------------------------------------------------------------*/
00199 bool
00200 FileSink :: create ( void )                     throw ( Exception )
00201 {
00202     int     fd;
00203     
00204     if ( isOpen() ) {
00205         return false;
00206     }
00207 
00208     if ( (fd = ::creat( fileName, S_IRUSR | S_IWUSR)) == -1 ) {
00209         reportEvent( 3, "can't create file", fileName, errno);
00210         return false;
00211     }
00212 
00213     ::close( fd);
00214     return true;
00215 }
00216 
00217 
00218 /*------------------------------------------------------------------------------
00219  *  Open the file
00220  *----------------------------------------------------------------------------*/
00221 bool
00222 FileSink :: open ( void )                       throw ( Exception )
00223 {
00224     if ( isOpen() ) {
00225         return false;
00226     }
00227 
00228     if ( (fileDescriptor = ::open( fileName, O_WRONLY | O_TRUNC, 0)) == -1 ) {
00229         fileDescriptor = 0;
00230         return false;
00231     }
00232 
00233     return true;
00234 }
00235 
00236 
00237 /*------------------------------------------------------------------------------
00238  *  Check wether the file can be written to
00239  *----------------------------------------------------------------------------*/
00240 bool
00241 FileSink :: canWrite (     unsigned int    sec,
00242                            unsigned int    usec )   throw ( Exception )
00243 {
00244     fd_set              fdset;
00245     struct timeval      tv;
00246     int                 ret;
00247 
00248     if ( !isOpen() ) {
00249         return false;
00250     }
00251 
00252     FD_ZERO( &fdset);
00253     FD_SET( fileDescriptor, &fdset);
00254     tv.tv_sec  = sec;
00255     tv.tv_usec = usec;
00256 
00257     ret = select( fileDescriptor + 1, NULL, &fdset, NULL, &tv);
00258     
00259     if ( ret == -1 ) {
00260         throw Exception( __FILE__, __LINE__, "select error");
00261     }
00262 
00263     return ret > 0;
00264 }
00265 
00266 
00267 /*------------------------------------------------------------------------------
00268  *  Write to the FileSink
00269  *----------------------------------------------------------------------------*/
00270 unsigned int
00271 FileSink :: write (    const void    * buf,
00272                        unsigned int    len )        throw ( Exception )
00273 {
00274     ssize_t     ret;
00275 
00276     if ( !isOpen() ) {
00277         return 0;
00278     }
00279 
00280     ret = ::write( fileDescriptor, buf, len);
00281 
00282     if ( ret == -1 ) {
00283         if ( errno == EAGAIN ) {
00284             ret = 0;
00285         } else {
00286             throw Exception( __FILE__, __LINE__, "write error", errno);
00287         }
00288     }
00289 
00290     return ret;
00291 }
00292 
00293 
00294 /*------------------------------------------------------------------------------
00295  *  Close the FileSink
00296  *----------------------------------------------------------------------------*/
00297 void
00298 FileSink :: close ( void )                          throw ( Exception )
00299 {
00300     if ( !isOpen() ) {
00301         return;
00302     }
00303 
00304     flush();
00305     ::close( fileDescriptor);
00306     fileDescriptor = 0;
00307 }
00308 
00309 
00310 
00311 /*------------------------------------------------------------------------------
00312  
00313   $Source: /cvsroot/darkice/darkice/src/FileSink.cpp,v $
00314 
00315   $Log: FileSink.cpp,v $
00316   Revision 1.8  2002/03/28 16:41:49  darkeye
00317   some fixes to typos in comments
00318 
00319   Revision 1.7  2002/02/20 11:54:11  darkeye
00320   added local dump file possibility
00321 
00322   Revision 1.5  2001/09/11 15:05:21  darkeye
00323   added Solaris support
00324 
00325   Revision 1.4  2001/08/26 20:44:30  darkeye
00326   removed external command-line encoder support
00327   replaced it with a shared-object support for lame with the possibility
00328   of static linkage
00329 
00330   Revision 1.3  2000/11/11 12:33:13  darkeye
00331   added kdoc-style documentation
00332 
00333   Revision 1.2  2000/11/05 14:08:27  darkeye
00334   changed builting to an automake / autoconf environment
00335 
00336   Revision 1.1.1.1  2000/11/05 10:05:51  darkeye
00337   initial version
00338 
00339   
00340 ------------------------------------------------------------------------------*/
00341 

Generated on Fri May 19 15:36:48 2006 for DarkIce by  doxygen 1.4.4