bio_file.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  $RCSfile$
00003  -------------------
00004  cvs         : $Id$
00005  begin       : Fri Feb 07 2003
00006  copyright   : (C) 2003 by Martin Preuss
00007  email       : martin@libchipcard.de
00008 
00009  ***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00024  *   MA  02111-1307  USA                                                   *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 
00029 #ifdef HAVE_CONFIG_H
00030 # include <config.h>
00031 #endif
00032 
00033 #define DISABLE_DEBUGLOG
00034 
00035 
00036 #include "bio_file_p.h"
00037 #include <gwenhywfar/misc.h>
00038 #include <gwenhywfar/text.h>
00039 #include <stdlib.h>
00040 #ifdef HAVE_UNISTD_H
00041 # include <unistd.h>
00042 #endif
00043 #include <string.h>
00044 #include <errno.h>
00045 
00046 #include <gwenhywfar/debug.h>
00047 
00048 
00049 GWEN_INHERIT(GWEN_BUFFEREDIO, GWEN_BUFFEREDIO_FILE)
00050 /* No trailing semicolon here because this is a macro call */
00051 
00052 
00053 
00054 GWEN_BUFFEREDIO_FILE *GWEN_BufferedIO_File_Table__new() {
00055   GWEN_BUFFEREDIO_FILE *bft;
00056 
00057   GWEN_NEW_OBJECT(GWEN_BUFFEREDIO_FILE, bft);
00058   bft->fd=-1;
00059   return bft;
00060 }
00061 
00062 
00063 
00064 void GWEN_BufferedIO_File_Table__free(GWEN_BUFFEREDIO_FILE *bft) {
00065   GWEN_FREE_OBJECT(bft);
00066 }
00067 
00068 
00069 
00070 int GWEN_BufferedIO_File__Read(GWEN_BUFFEREDIO *dm,
00071                                char *buffer,
00072                                int *size,
00073                                GWEN_UNUSED int timeout){
00074   int rv;
00075   GWEN_BUFFEREDIO_FILE *bft;
00076 
00077   assert(dm);
00078   bft=GWEN_INHERIT_GETDATA(GWEN_BUFFEREDIO, GWEN_BUFFEREDIO_FILE, dm);
00079   assert(bft);
00080 
00081   if (*size<1) {
00082     DBG_WARN(GWEN_LOGDOMAIN, "Nothing to read");
00083     *size=0;
00084     return 0;
00085   }
00086   rv=read(bft->fd, buffer, *size);
00087   if (rv==0) {
00088     DBG_DEBUG(GWEN_LOGDOMAIN, "EOF met");
00089     *size=0;
00090     return 0;
00091   }
00092   if (rv<0) {
00093     DBG_ERROR(GWEN_LOGDOMAIN, "Could not read (%s)",
00094               strerror(errno));
00095     return GWEN_ERROR_READ;
00096   }
00097   *size=rv;
00098   return 0;
00099 }
00100 
00101 
00102 
00103 int GWEN_BufferedIO_File__Write(GWEN_BUFFEREDIO *dm,
00104                                 const char *buffer,
00105                                 int *size,
00106                                 GWEN_UNUSED int timeout){
00107   int rv;
00108   GWEN_BUFFEREDIO_FILE *bft;
00109 
00110   assert(dm);
00111   bft=GWEN_INHERIT_GETDATA(GWEN_BUFFEREDIO, GWEN_BUFFEREDIO_FILE, dm);
00112   assert(bft);
00113 
00114   if (*size<1) {
00115     DBG_WARN(GWEN_LOGDOMAIN, "Nothing to write");
00116     *size=0;
00117     return 0;
00118   }
00119 
00120   rv=write(bft->fd, buffer, *size);
00121   if (rv<1) {
00122     DBG_ERROR(GWEN_LOGDOMAIN, "Could not write (%s)",
00123               strerror(errno));
00124     return GWEN_ERROR_WRITE;
00125   }
00126   *size=rv;
00127   return 0;
00128 }
00129 
00130 
00131 
00132 int GWEN_BufferedIO_File__Close(GWEN_BUFFEREDIO *dm){
00133   GWEN_BUFFEREDIO_FILE *bft;
00134 
00135   assert(dm);
00136   bft=GWEN_INHERIT_GETDATA(GWEN_BUFFEREDIO, GWEN_BUFFEREDIO_FILE, dm);
00137   assert(bft);
00138   if (bft->fd<3) {
00139     /* one of the console IO sockets, do not close */
00140     return 0;
00141   }
00142   if (close(bft->fd)) {
00143     DBG_ERROR(GWEN_LOGDOMAIN, "Could not close (%s)",
00144               strerror(errno));
00145     return GWEN_ERROR_CLOSE;
00146   }
00147   return 0;
00148 }
00149 
00150 
00151 
00152 void GWENHYWFAR_CB GWEN_BufferedIO_File_FreeData(GWEN_UNUSED void *bp, void *p) {
00153   GWEN_BUFFEREDIO_FILE *bft;
00154 
00155   bft=(GWEN_BUFFEREDIO_FILE*)p;
00156   GWEN_BufferedIO_File_Table__free(bft);
00157 }
00158 
00159 
00160 
00161 GWEN_BUFFEREDIO *GWEN_BufferedIO_File_new(int fd){
00162   GWEN_BUFFEREDIO *bt;
00163   GWEN_BUFFEREDIO_FILE *bft;
00164 
00165   bt=GWEN_BufferedIO_new();
00166   bft=GWEN_BufferedIO_File_Table__new();
00167   bft->fd=fd;
00168 
00169   GWEN_INHERIT_SETDATA(GWEN_BUFFEREDIO, GWEN_BUFFEREDIO_FILE,
00170                        bt, bft,
00171                        GWEN_BufferedIO_File_FreeData);
00172   GWEN_BufferedIO_SetReadFn(bt, GWEN_BufferedIO_File__Read);
00173   GWEN_BufferedIO_SetWriteFn(bt, GWEN_BufferedIO_File__Write);
00174   GWEN_BufferedIO_SetCloseFn(bt, GWEN_BufferedIO_File__Close);
00175   GWEN_BufferedIO_SetTimeout(bt, GWEN_BUFFEREDIO_FILE_TIMEOUT);
00176 
00177   return bt;
00178 }
00179 
00180 
00181 
00182 
00183 

Generated on Thu Aug 20 13:54:37 2009 for gwenhywfar by  doxygen 1.5.9