fastbuffer.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  $RCSfile$
00003                              -------------------
00004     cvs         : $Id: crypttoken.h 1113 2007-01-10 09:14:16Z martin $
00005     begin       : Wed Mar 16 2005
00006     copyright   : (C) 2005 by Martin Preuss
00007     email       : martin@libchipcard.de
00008 
00009  ***************************************************************************
00010  *          Please see toplevel file COPYING for license details           *
00011  ***************************************************************************/
00012 
00013 #ifdef HAVE_CONFIG_H
00014 # include <config.h>
00015 #endif
00016 
00017 
00018 #include "fastbuffer.h"
00019 
00020 #include "i18n_l.h"
00021 #include <gwenhywfar/misc.h>
00022 #include <gwenhywfar/debug.h>
00023 
00024 #include <assert.h>
00025 
00026 
00027 GWEN_FAST_BUFFER *GWEN_FastBuffer_new(uint32_t bsize,
00028                                       GWEN_IO_LAYER *io, uint32_t guiid, int msecs) {
00029   GWEN_FAST_BUFFER *fb;
00030 
00031   assert(bsize);
00032 
00033   fb=(GWEN_FAST_BUFFER*) malloc(sizeof(GWEN_FAST_BUFFER)+bsize);
00034   assert(fb);
00035   memset(fb, 0, sizeof(GWEN_FAST_BUFFER)+bsize);
00036 
00037   fb->bufferSize=bsize;
00038 
00039   fb->io=io;
00040   fb->guiid=guiid;
00041   fb->msecs=msecs;
00042 
00043   return fb;
00044 }
00045 
00046 
00047 
00048 void GWEN_FastBuffer_free(GWEN_FAST_BUFFER *fb) {
00049   if (fb) {
00050     GWEN_FREE_OBJECT(fb);
00051   }
00052 }
00053 
00054 
00055 
00056 int GWEN_FastBuffer_ReadLine(GWEN_FAST_BUFFER *fb, uint8_t *p, int len) {
00057   int bytes;
00058   int copied=0;
00059 
00060   if (fb->bufferReadPos>=fb->bufferWritePos) {
00061     int rv;
00062 
00063     rv=GWEN_Io_Layer_ReadBytes(fb->io, fb->buffer, fb->bufferSize, 0, fb->guiid, fb->msecs);
00064     if (rv<0) {
00065       DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", rv);
00066       return rv;
00067     }
00068   }
00069 
00070   bytes=fb->bufferWritePos-fb->bufferReadPos;
00071   if (bytes>len)
00072     bytes=len;
00073   while(bytes) {
00074     uint8_t c;
00075 
00076     c=fb->buffer[fb->bufferReadPos++];
00077     fb->bytesRead++;
00078     if (c==10) {
00079       *(p++)=c;
00080       copied++;
00081       break;
00082     }
00083     else if (c!=13) {
00084       *(p++)=c;
00085       copied++;
00086     }
00087     bytes--;
00088   } /* while */
00089 
00090   return copied;
00091 }
00092 
00093 
00094 
00095 int GWEN_FastBuffer_ReadLineToBuffer(GWEN_FAST_BUFFER *fb, GWEN_BUFFER *buf) {
00096   int lineComplete=0;
00097   int hadSome=0;
00098 
00099   while (!lineComplete) {
00100     int bytes;
00101     int copied=0;
00102     uint8_t *p;
00103 
00104     if (fb->bufferReadPos>=fb->bufferWritePos) {
00105       int rv;
00106   
00107       rv=GWEN_Io_Layer_ReadBytes(fb->io, fb->buffer, fb->bufferSize, 0, fb->guiid, fb->msecs);
00108       if (rv<0) {
00109         if (rv==GWEN_ERROR_EOF && hadSome) {
00110           /* done */
00111           return 0;
00112         }
00113         DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00114         return rv;
00115       }
00116       else if (rv==0) {
00117         DBG_INFO(GWEN_LOGDOMAIN, "EOF met");
00118         return GWEN_ERROR_EOF;
00119       }
00120       else {
00121         fb->bufferWritePos=rv;
00122         fb->bufferReadPos=0;
00123       }
00124     }
00125   
00126     bytes=fb->bufferWritePos-fb->bufferReadPos;
00127     p=fb->buffer+fb->bufferReadPos;
00128     while(bytes) {
00129       uint8_t c;
00130 
00131       hadSome=1;
00132 
00133       c=fb->buffer[fb->bufferReadPos++];
00134       fb->bytesRead++;
00135       bytes--;
00136       if (c==10) {
00137         lineComplete=1;
00138         /* don't include this character */
00139         break;
00140       }
00141       else if (c==13) {
00142         break;
00143       }
00144       else {
00145         copied++;
00146       }
00147     } /* while */
00148 
00149     if (copied)
00150       GWEN_Buffer_AppendBytes(buf, (const char*)p, copied);
00151   }
00152 
00153   return 0;
00154 }
00155 
00156 
00157 
00158 uint32_t GWEN_FastBuffer_GetFlags(const GWEN_FAST_BUFFER *fb) {
00159   assert(fb);
00160   return fb->flags;
00161 }
00162 
00163 
00164 
00165 void GWEN_FastBuffer_SetFlags(GWEN_FAST_BUFFER *fb, uint32_t fl) {
00166   assert(fb);
00167   fb->flags=fl;
00168 }
00169 
00170 
00171 
00172 void GWEN_FastBuffer_AddFlags(GWEN_FAST_BUFFER *fb, uint32_t fl) {
00173   assert(fb);
00174   fb->flags|=fl;
00175 }
00176 
00177 
00178 
00179 void GWEN_FastBuffer_SubFlags(GWEN_FAST_BUFFER *fb, uint32_t fl) {
00180   assert(fb);
00181   fb->flags&=~fl;
00182 }
00183 
00184 
00185 
00186 uint32_t GWEN_FastBuffer_GetBytesWritten(const GWEN_FAST_BUFFER *fb) {
00187   assert(fb);
00188   return fb->bytesWritten;
00189 }
00190 
00191 
00192 
00193 uint32_t GWEN_FastBuffer_GetBytesRead(const GWEN_FAST_BUFFER *fb) {
00194   assert(fb);
00195   return fb->bytesRead;
00196 }
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 
00205 
00206 
00207 

Generated by  doxygen 1.6.2