1 2%{ 3#include "wisebase.h" 4 5 6typedef struct Wise2_Stream_Read_Interface { 7 char* (*read_buffer)(void *,char*,int); 8 boolean (*is_end)(void*); 9 void (*close_and_free_handle)(void *); 10 void * handle; 11} Wise2ReadStreamInterface; 12 13 14typedef struct Wise2_Stream_Write_Interface { 15 void (*write_buffer)(void *,char*); 16 void (*write_bufferf)(void *,char*,...); 17 void (*close_and_free_handle)(void *); 18 void * handle; 19} Wise2WriteStreamInterface; 20 21 22#define WISE2_READ_BUFFER(buffer,length,ri) ((*ri->read_buffer)(ri->handle,buffer,length)) 23#define WISE2_WRITE_STRING(string,wi) ((*wi->write_buffer)(wi->handle,string)) 24 25 26%} 27 28 29 30 31 32%{ 33#include "wisestreaminterface.h" 34 35 36 37%func 38Implementation function for normal files for reading 39%% 40char * read_buffer_FILE_impl(void * handle,char * input_buffer,int maxsize) 41{ 42 char * ret; 43 FILE * ifp = (FILE *)handle; 44 45 ret = fgets(input_buffer,maxsize,ifp); 46 47 return ret; 48} 49 50%func 51Implementation function for normal files for end flag 52%% 53boolean is_end_FILE_impl(void * handle) 54{ 55 FILE * ifp = (FILE *)handle; 56 57 if( feof(ifp) ) { 58 return TRUE; 59 } else { 60 return FALSE; 61 } 62} 63 64%func 65Implementation function for normal files for closing. Works for both 66reading and writing 67%% 68void close_and_free_FILE_impl(void * handle) 69{ 70 FILE * ifp = (FILE *)handle; 71 72 fclose(ifp); 73} 74 75%func 76Makes a Wise2ReadStream interface from a normal C filehandle 77%% 78Wise2ReadStreamInterface * ReadStream_from_FILE(FILE * ifp) 79{ 80 Wise2ReadStreamInterface * out; 81 82 out = malloc(sizeof(Wise2ReadStreamInterface)); 83 out->read_buffer = read_buffer_FILE_impl; 84 out->is_end = is_end_FILE_impl; 85 out->close_and_free_handle = close_and_free_FILE_impl; 86 out->handle = (void*) ifp; 87 88 return out; 89} 90 91%func 92opens a file from filename and gives back a ReadStream, 93NULL if unable to return 94%% 95Wise2ReadStreamInterface * ReadStream_openfile(char * filename) 96{ 97 FILE * ifp; 98 assert(filename != NULL); 99 100 ifp = openfile(filename,"r"); 101 if( ifp == NULL ) { 102 warn("Unable to open file %s",filename); 103 return NULL; 104 } 105 106 return ReadStream_from_FILE(ifp); 107} 108 109 110%func 111implementation for normal C files for string writing 112%% 113void write_buffer_FILE_impl(void * handle,char * string) 114{ 115 FILE * ifp = (FILE *)handle; 116 117 fputs(string,ifp); 118 119 return; 120} 121 122%func 123implementation for normal C files for formatted writing 124%% 125void write_bufferf_FILE_impl(void * handle,char * format,...) 126{ 127 FILE * ifp = (FILE *)handle; 128 char buffer[1024]; 129 va_list ap; 130 131 132 va_start(ap,format); 133 vsprintf(buffer,format,ap); 134 135 fputs(buffer,ifp); 136 137 return; 138} 139 140%func 141makes a WriteStream from a normal C FILE structure 142%% 143Wise2WriteStreamInterface * WriteStream_from_FILE(FILE * ofp) 144{ 145 Wise2WriteStreamInterface * out; 146 147 out = malloc(sizeof(Wise2WriteStreamInterface)); 148 out->write_buffer = write_buffer_FILE_impl; 149 out->write_bufferf = write_bufferf_FILE_impl; 150 out->close_and_free_handle = close_and_free_FILE_impl; 151 out->handle = (void*) ofp; 152 153 return out; 154} 155 156%func 157opens a file from filename and gives back a WriteStream, 158NULL if unable to return 159%% 160Wise2WriteStreamInterface * WriteStream_openfile(char * filename) 161{ 162 FILE * ofp; 163 assert(filename != NULL); 164 165 ofp = openfile(filename,"w"); 166 if( ofp == NULL ) { 167 warn("Unable to open file %s",filename); 168 return NULL; 169 } 170 171 return WriteStream_from_FILE(ofp); 172} 173 174%func 175helper function - cats one stream into another 176%% 177void cat_ReadStream_into_WriteStream(Wise2ReadStreamInterface * rs,Wise2WriteStreamInterface * ws) 178{ 179 char str[1024]; 180 181 while( (*rs->is_end)(rs->handle) != TRUE ) { 182 if( (*rs->read_buffer)(rs->handle,str,1024) == NULL ) { 183 return; 184 } 185 (*ws->write_buffer)(ws->handle,str); 186 } 187 188 189} 190 191 192%} 193