Simple tools for networking / objects in plain C Snapshot
bf.c
Go to the documentation of this file.
00001 #include "bf.h"
00002 #include <string.h>
00003 
00004 
00005 int BF_compact( BF * bf )
00006 {
00007     size_t bs;
00008 
00009     if (bf->get_pos == bf->put_pos) {
00010       bf->get_pos = bf->put_pos = bf->start;
00011       return 1;
00012     }
00013     if (bf->get_pos != bf->start) {
00014       bs = bf->put_pos - bf->get_pos;     
00015       memmove( bf->start, bf->get_pos, bs );
00016       bf->get_pos = bf->start;
00017       bf->put_pos = bf->start + bs;
00018       return 0;
00019     } 
00020     return 1;
00021 }
00022 
00023 char *  BF_get_line( BF * bf, int eof)
00024 {  
00025    char *eof_line, *ret;
00026 
00027    eof_line =  memchr(bf->get_pos, eof, bf->put_pos - bf->get_pos );
00028    if (eof_line) {
00029       ret = (char *) bf->get_pos;
00030       *eof_line = '\0';
00031       bf->get_pos = (uint8_t *) eof_line + 1;
00032       return ret;
00033    }
00034    return 0;
00035 }
00036 
00037 char *  BF_get_line_ext( BF * bf, const char *eof_line, size_t eof_line_size)
00038 {
00039    char *pos,*next,*ret;
00040    size_t nsearch;
00041    
00042    nsearch = bf->put_pos - bf->get_pos;
00043 
00044    if (nsearch  < eof_line_size) {
00045      return 0;
00046    }
00047    
00048    nsearch -= eof_line_size - 1; 
00049    pos = (char *) bf->get_pos;
00050 
00051    do { 
00052      next = (char *) memchr( pos, (int) *eof_line, nsearch);
00053      if (!next) {
00054        return 0;
00055      }
00056      if (memcmp(next, eof_line, eof_line_size) == 0) {
00057         ret = (char *) bf->get_pos;
00058         *next = '\0';
00059         bf->get_pos = (uint8_t *) (next + eof_line_size);
00060         return (char *) ret;
00061      }
00062      pos = next + 1;
00063    } while( pos < (char *) bf->put_pos );
00064 
00065    return 0;
00066 }
00067 
00068  int BF_check( BF * bf )
00069 {
00070     uint8_t * eof;
00071     
00072     if (bf->bf > bf->start) {
00073       return 1;
00074     }
00075     if (bf->start > bf->get_pos)  {
00076       return 2;
00077     }
00078     if (bf->get_pos > bf->put_pos) {
00079       return 3;
00080     }
00081     if (bf->put_pos > bf->end) {
00082       return 4;
00083     }
00084     eof = bf->bf + bf->bf_size;
00085     if (bf->end > eof) {
00086       return 5;
00087     }
00088     return 0;
00089 }
00090 
00091 
00092 int BF_set_start( BF * bf, size_t pos )
00093 {
00094   if (pos > bf->bf_size) {
00095     return -1;
00096   }
00097   bf->start = bf->bf + pos;
00098 
00099   if (bf->start > bf->get_pos) {
00100      bf->get_pos = bf->start;
00101   }
00102   if (bf->start > bf->put_pos) {
00103      bf->put_pos = bf->start;
00104   }
00105   if (bf->start > bf->end) {
00106     bf->end = bf->start; 
00107   }
00108   return 0;
00109 }
00110 
00111 int BF_set_end( BF * bf, size_t pos )
00112 {
00113   if (pos > bf->bf_size) {
00114     return -1;
00115   }
00116   bf->end = bf->bf + pos;
00117 
00118   if (bf->end < bf->get_pos) {
00119      bf->get_pos = bf->end;
00120   }
00121   if (bf->end < bf->put_pos) {
00122      bf->put_pos = bf->end;
00123   }
00124   if (bf->end <  bf->start) {
00125     bf->start = bf->end; 
00126   }
00127   return 0;
00128 }
00129 
00130 int BF_set_get( BF * bf, size_t pos )
00131 {
00132   size_t bfs = bf->end - bf->start; 
00133   if (pos > bfs) {
00134     return -1;
00135   }
00136   bf->get_pos = bf->start + pos;
00137   if (bf->get_pos > bf->put_pos) {
00138      bf->put_pos = bf->get_pos;
00139   }
00140   return 0;
00141 }
00142 
00143 int BF_set_put( BF * bf, size_t pos )
00144 {
00145   size_t bfs = bf->end - bf->start; 
00146   if (pos > bfs) {
00147     return -1;
00148   }
00149   bf->put_pos = bf->start + pos;
00150   if (bf->put_pos < bf->get_pos) {
00151      bf->get_pos = bf->put_pos;
00152   }
00153   return 0;
00154 }
00155 
00156