#ifndef _GNU_SOURCE #warning >> sorry, but this will only compile with gnu-libc! << #error >> compile with gcc -D_GNU_SOURCE << #else /*------------------------------------------------------------------*\ author: mathias gumz date: 040129 15:17:57 about: demonstrates the usage of the fopencookie-funktions/extension of gnu-libc to write own filehandlers !!! THIS CODE WILL NOT WORK ON NON_GNU_LIBC_SYSTEMS !!! \*------------------------------------------------------------------*/ #include #include /* essential header */ /*------------------------------------------------------------------*\ passthru functions just to see if it works in general \*------------------------------------------------------------------*/ static size_t passThruRead(void* myhandle, char* buffer, size_t size) { return fread(buffer, 1, size, (FILE*)myhandle); } static size_t passThruWrite(void* myhandle, char* buffer, size_t size) { return fwrite(buffer, 1, size, (FILE*)myhandle); } static int passThruSeek(void* myhandle, fpos_t* position, int whence) { return fseek((FILE*)myhandle, *(unsigned int*)position, whence); } static int passThruClose(void* myhandle) { return fclose((FILE*)myhandle); } cookie_io_functions_t passThruFunctions= { (cookie_read_function_t*)passThruRead, (cookie_write_function_t*)passThruWrite, (cookie_seek_function_t*)passThruSeek, (cookie_close_function_t*)passThruClose }; /*------------------------------------------------------------------*\ my own reader/writer .. shifts each written byte 1 left read byte 1 right \*------------------------------------------------------------------*/ static size_t myShiftRead(void* myhandle, char* buffer, size_t size) { register size_t i; int ret; ret= fread(buffer, 1, size, (FILE*)myhandle); if ( !ferror((FILE*)myhandle) ) { for ( i= 0; i < ret; i++ ) { buffer[i]= buffer[i] >> 1; } } return ret; } /* TODO fix this function .. atm just a first and quick try */ static size_t myShiftWrite(void* myhandle, const char* buffer, size_t size) { register size_t i; char c; for ( i= 0; i < size; i++ ) { c= buffer[i] << 1; if ( (fwrite(&c, 1, 1, (FILE*)myhandle)) != 1 ) return -1; } return i; } static int myShiftSeek(void* myhandle, fpos_t* position, int whence) { return fseek((FILE*)myhandle, *(unsigned long*)position, whence); } static int myShiftClose(void* myhandle) { return fclose((FILE*)myhandle); } cookie_io_functions_t myIOFunctions= { (cookie_read_function_t*)myShiftRead, (cookie_write_function_t*)myShiftWrite, (cookie_seek_function_t*)myShiftSeek, (cookie_close_function_t*)myShiftClose }; /*------------------------------------------------------------------*\ main :) \*------------------------------------------------------------------*/ int main() { FILE* n_file1; /* normal handle */ FILE* n_file2; /* normal handle */ FILE* bs_file; /* handle for bitshifted read/write */ FILE* pt_file; /* handle for passthru read/write */ char c; /* ordenary filehandles */ n_file1= fopen("fcookie.c", "r"); n_file2= fopen("fcookie.c", "r"); bs_file= fopencookie(n_file1, "r", myIOFunctions); pt_file= fopencookie(n_file2, "r", passThruFunctions); while ( !feof(pt_file) && !feof(bs_file) ) { c= '\0'; fread(&c, 1, 1, pt_file); printf("%c | %d <=> ", ( iscntrl(c) ? '@' : c), c); c= '\0'; fread(&c, 1, 1, bs_file); printf("%d | %c\n", c, ( iscntrl(c) ? '@' : c)); } fclose(pt_file); fclose(bs_file); return 0; } #endif /* vim:ts=2:sw=2:tw=80:ft=c */