4556534 [rkeene@sledge /home/rkeene/devel/dact]$ cat -n buffer.c
  1 /*
  2  * Copyright (C) 2001, 2002, and 2003  Roy Keene
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 17  *
 18  *      email: dact@rkeene.org
 19  */
 20 
 21 
 22 #include "dact.h"
 23 #include "buffer.h"
 24 #include <stdio.h>
 25 #ifdef HAVE_STRING_H
 26 #include <string.h>
 27 #endif
 28 #ifdef HAVE_STDLIB_H
 29 #include <stdlib.h>
 30 #endif
 31 
 32 uint32_t bit_buffer_data=0;
 33 int bit_buffer_location=0;
 34 
 35 char *byte_buffer_data=NULL;
 36 int byte_buffer_location=-1;
 37 
 38 
 39 /*
 40     Func: bit_buffer_read
 41     Args: 
 42            bits     - (int) Number of bits to read from the buffer.
 43     Rets: Int, value from buffer.
 44     Meth: Bread high BITS bits from BUFFER_DATA and shift to the left.
 45     Stat: Complete
 46 
 47 */
 48 int bit_buffer_read(int bits) {
 49         int retval;
 50         if (bits>bit_buffer_location) { 
 51         bits=bit_buffer_location;
 52     }
 53 
 54         retval=(bit_buffer_data>>((sizeof(bit_buffer_data)*8)-bits));
 55         bit_buffer_data=(bit_buffer_data<<bits);
 56     bit_buffer_location-=bits;
 57 #ifdef BUFFER_VERBOSE
 58 #endif
 59         return(retval);
 60 }
 61 
 62 /*
 63     Func: bit_buffer_write
 64     Args:
 65            val      - (uint) Value to write
 66            bits     - (uint) Number of bits to allocate
 67     Rets: Nothing
 68     Meth: Add to right of current BUFFER_DATA[BUFFER_LOCATION], incremint
 69             BUFFER_LOCATION
 70     Stat: Complete
 71 */
 72 void bit_buffer_write(unsigned int val, unsigned int bits) {
 73         while ((val>>bits)!=0) {
 74         bits++;
 75         }
 76 
 77         if ((bits+bit_buffer_location)>(sizeof(bit_buffer_data)*8)) {
 78                 return;
 79         }
 80         bit_buffer_location+=bits;
 81         bit_buffer_data+=(val<<((sizeof(bit_buffer_data)*8)-bit_buffer_location));
 82 #ifdef BUFFER_VERBOSE
 83 #endif
 84         return;
 85 }
 86 
 87 
 88 /*
 89     Func: bit_buffer_size
 90     Args: None
 91     Rets: Int, number of bits in buffer
 92     Meth: buffer_location
 93     Stat: Complete
 94 */
 95 int bit_buffer_size(void) {
 96 #ifdef BUFFER_VERBOSE
 97 #endif
 98         return(bit_buffer_location);
 99 }
100 
101 
102 /*
103     Func: bit_buffer_purge
104     Args: None
105     Rets: Nothing
106     Meth: Clear BUFFER_DATA and set BUFFER_LOCATION to 0
107 */
108 void bit_buffer_purge(void) {
109         bit_buffer_location=0;
110         bit_buffer_data=0;
111 #ifdef BUFFER_VERBOSE
112 #endif
113         return;
114 }
115 
116 
117 char *byte_buffer_read(int bytes) {
118     char *retval;
119     if (byte_buffer_location==-1) return(NULL);
120     if (bytes>byte_buffer_location) {
121         bytes=byte_buffer_location;
122     }
123     if (!(retval=malloc(bytes))) {
124         memcpy(retval,byte_buffer_data,bytes);
125         memmove(byte_buffer_data,byte_buffer_data+bytes,(byte_buffer_location-=bytes));
126         return(retval);
127     } else {
128         return(NULL);
129     }
130 }
131 
132 void byte_buffer_write(char *val, unsigned int size) {
133     if (byte_buffer_location==-1) {
134         byte_buffer_purge();
135     }
136     if ((byte_buffer_location+size)>(BYTE_BUFF_SIZE)) {
137         return;
138     }
139     memcpy(byte_buffer_data+byte_buffer_location,val,size);
140     byte_buffer_location+=size;
141 }
142 
143 char byte_buffer_read_1(void) {
144     char *buffread;
145     char retval;
146     buffread=byte_buffer_read(1);
147     retval=buffread[0];
148     free(buffread);
149     return(retval);
150 }
151 
152 int byte_buffer_size(void) {
153     return(byte_buffer_location);
154 }
155 
156 void byte_buffer_purge(void) {
157     int i;
158 
159 #if 0
160     if (byte_buffer_data!=NULL) free(byte_buffer_data);
161     byte_buffer_data=calloc(BYTE_BUFF_SIZE, 1);
162 #endif
163     if (byte_buffer_data==NULL) {
164         if (!(byte_buffer_data=malloc(BYTE_BUFF_SIZE))) return;
165     }
166     if (byte_buffer_location==-1) {
167         for (i=0;i<(BYTE_BUFF_SIZE);i++) byte_buffer_data[i]=0;
168     } else {
169         for (i=0;i<(byte_buffer_location+1);i++) byte_buffer_data[i]=0;
170     }
171     byte_buffer_location=0;
172     return;
173 }
4556535 [rkeene@sledge /home/rkeene/devel/dact]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2004-04-04 07:01:48