diff --git a/TOOLS/cache.c b/TOOLS/cache.c new file mode 100644 index 0000000000..8c587ea9f4 --- /dev/null +++ b/TOOLS/cache.c @@ -0,0 +1,121 @@ + +#include +#include +#include +#include +#include +#include +#include + +//int open(const char *pathname, int flags); + + +#define BUFFSIZE (4*65536) +#define NUM_BUFS (16) + +unsigned char *buffer[NUM_BUFS]; + +unsigned int buf_read=0; +unsigned int buf_write=0; +unsigned int buf_read_pos=0; +unsigned int buf_write_pos=0; +int full_buffers=0; + +int main(int argc,char* argv[]){ + + fd_set rfds; + fd_set wfds; + struct timeval tv; + int retval; + int i; +// int empty=1; + int can_read=1; + int eof=0; + int in_fd=0; // stdin + + if(argc>1) in_fd=open(argv[1],O_RDONLY|O_NDELAY); + + for(i=0;i0){ + buf_read_pos+=len; + if(buf_read_pos>=BUFFSIZE){ + // block is full, find next! + buf_read=(buf_read+1)%NUM_BUFS; + ++full_buffers; + buf_read_pos=0; + fprintf(stderr,"+");fflush(stderr); + } + } else { + eof=1; + } + } + } + if(FD_ISSET(1, &wfds)){ + fprintf(stderr,"\n%d w",full_buffers);fflush(stderr); + if(full_buffers==0){ + if(eof){ + // flush buffer! + int pos=0; + int len; + fprintf(stderr,"\nf");fflush(stderr); + while((len=buf_read_pos-pos)>0){ + len=write(1,buffer[buf_write]+pos,len); + fprintf(stderr,"(%d)",len);fflush(stderr); + if(len<=0) break; + pos+=len; + } + exit(1); + } + fprintf(stderr," empty");fflush(stderr); + //empty=1; // we must fill buffers! + } else { + // yeah, we can read from the buffer! + int len=BUFFSIZE-buf_write_pos; + fprintf(stderr,"W");fflush(stderr); + len=write(1,buffer[buf_write]+buf_write_pos,len); + fprintf(stderr,"(%d)",len);fflush(stderr); + if(len>0){ + buf_write_pos+=len; + if(buf_write_pos>=BUFFSIZE){ + // block is empty, find next! + buf_write=(buf_write+1)%NUM_BUFS; + --full_buffers; + buf_write_pos=0; + fprintf(stderr,"-");fflush(stderr); + can_read=1; + } + } + } + } + } else { + fprintf(stderr,".");fflush(stderr); + } +} + +return 0; +} +