00001 /* 00002 * Copyright (C) 2006 SRI International 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2.1 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public 00015 * License along with this library; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00017 * 00018 * SRI International: 333 Ravenswood Ave, Menlo Park, CA 94025 00019 */ 00020 00021 #include "stringbuffer.h" 00022 #include "stringbuffer_private.h" 00023 #include <stdio.h> 00024 00025 stringbuffer_t* stringbuffer_new(char* data, size_t len) 00026 { 00027 stringbuffer_t* newBuf = (stringbuffer_t*)malloc(sizeof(stringbuffer_t)); 00028 newBuf->data = data; 00029 newBuf->len = len; 00030 newBuf->index = 0; 00031 return newBuf; 00032 } 00033 00034 stringbuffer_t* stringbuffer_reset(stringbuffer_t* buf, char* data, size_t len) 00035 { 00036 buf->data = data; 00037 buf->len = len; 00038 buf->index = 0; 00039 return buf; 00040 } 00041 00042 int stringbuffer_read(stringbuffer_t* buf) 00043 { 00044 /* 00045 { 00046 char* data = (char*)malloc(buf->len + 1); 00047 memset(data, 0, buf->len + 1); 00048 memcpy(data, buf->data, buf->len); 00049 printf("stringbuffer_read current buffer contains [%s]\n", data); 00050 free(data); 00051 } 00052 */ 00053 if(buf->index >= buf->len) { 00054 /* 00055 fprintf(stdout, "stringbuffer_read returning EOF\n"); 00056 */ 00057 return EOF; 00058 } 00059 /* 00060 fprintf(stdout, "stringbuffer_read returning %o\n", (unsigned char)(buf->data[buf->index])); 00061 */ 00062 return(buf->data[buf->index++]); 00063 } 00064 00065 stringbuffer_t* stringbuffer_rewind(stringbuffer_t* buf, size_t rewindLen) 00066 { 00067 if(buf->index > rewindLen) { 00068 buf->index -= rewindLen; 00069 } 00070 else { 00071 buf->index = 0; 00072 } 00073 00074 return buf; 00075 } 00076 00077 stringbuffer_t* stringbuffer_setIndex(stringbuffer_t* buf, size_t newIndex) 00078 { 00079 buf->index = newIndex; 00080 return buf; 00081 } 00082 00083 void stringbuffer_delete(stringbuffer_t* buf) 00084 { 00085 free(buf); 00086 } 00087 00088 char* stringbuffer_getData(stringbuffer_t* buf) 00089 { 00090 return buf->data; 00091 } 00092 00093 size_t stringbuffer_getLen(stringbuffer_t* buf) 00094 { 00095 return buf->len; 00096 } 00097 00098 size_t stringbuffer_getIndex(stringbuffer_t* buf) 00099 { 00100 return buf->index; 00101 } 00102 00103 stringbuffer_t* stringbuffer_filter(void* state, stringbuffer_filter_t filterFunction, stringbuffer_t* toFilter) 00104 { 00105 int nextChar; 00106 size_t writeIndex = 0; 00107 size_t readIndex = 0; 00108 int filteredChar; 00109 00110 /* 00111 printf("stringbuffer_filter length is %lu\n", stringbuffer_getLen(toFilter)); 00112 */ 00113 for(readIndex = 0; readIndex < stringbuffer_getLen(toFilter); ++readIndex) { 00114 nextChar = stringbuffer_read(toFilter); 00115 if(nextChar == EOF) { 00116 /* 00117 printf("stringbuffer_filter hit EOF\n"); 00118 */ 00119 break; 00120 } 00121 if(filterFunction(state, (char)nextChar, &filteredChar)) { 00122 if(filteredChar == EOF) { 00123 /* 00124 printf("stringbuffer_filter filterFunction returned EOF\n"); 00125 */ 00126 break; 00127 } 00128 else { 00129 /* 00130 printf("stringbuffer_filter filterFunction returned %o\n", filteredChar); 00131 */ 00132 toFilter->data[writeIndex++] = (char)filteredChar; 00133 } 00134 } 00135 else { 00136 /* 00137 printf("stringbuffer_filter filterFunction returned 0\n"); 00138 */ 00139 } 00140 } 00141 toFilter->len = writeIndex; 00142 toFilter->index = 0; 00143 00144 return toFilter; 00145 } 00146