1/* fileio.c -- does standard I/O 2 3 (c) 1998-2006 (W3C) MIT, ERCIM, Keio University 4 See tidy.h for the copyright notice. 5 6 CVS Info : 7 8 $Author: iccir $ 9 $Date: 2007/01/30 23:46:51 $ 10 $Revision: 1.3 $ 11 12 Default implementations of Tidy input sources 13 and output sinks based on standard C FILE*. 14 15*/ 16 17#include <stdio.h> 18 19#include "forward.h" 20#include "fileio.h" 21#include "tidy.h" 22 23#ifndef SUPPORT_POSIX_MAPPED_FILES 24typedef struct _fp_input_source 25{ 26 FILE* fp; 27 TidyBuffer unget; 28} FileSource; 29 30static int TIDY_CALL filesrc_getByte( void* sourceData ) 31{ 32 FileSource* fin = (FileSource*) sourceData; 33 int bv; 34 if ( fin->unget.size > 0 ) 35 bv = tidyBufPopByte( &fin->unget ); 36 else 37 bv = fgetc( fin->fp ); 38 return bv; 39} 40 41static Bool TIDY_CALL filesrc_eof( void* sourceData ) 42{ 43 FileSource* fin = (FileSource*) sourceData; 44 Bool isEOF = ( fin->unget.size == 0 ); 45 if ( isEOF ) 46 isEOF = feof( fin->fp ) != 0; 47 return isEOF; 48} 49 50static void TIDY_CALL filesrc_ungetByte( void* sourceData, byte bv ) 51{ 52 FileSource* fin = (FileSource*) sourceData; 53 tidyBufPutByte( &fin->unget, bv ); 54} 55 56int TY_(initFileSource)( TidyInputSource* inp, FILE* fp ) 57{ 58 FileSource* fin = NULL; 59 60 inp->getByte = filesrc_getByte; 61 inp->eof = filesrc_eof; 62 inp->ungetByte = filesrc_ungetByte; 63 64 fin = (FileSource*) MemAlloc( sizeof(FileSource) ); 65 ClearMemory( fin, sizeof(FileSource) ); 66 fin->fp = fp; 67 inp->sourceData = fin; 68 return 0; 69} 70 71void TY_(freeFileSource)( TidyInputSource* inp, Bool closeIt ) 72{ 73 FileSource* fin = (FileSource*) inp->sourceData; 74 if ( closeIt && fin && fin->fp ) 75 fclose( fin->fp ); 76 tidyBufFree( &fin->unget ); 77 MemFree( fin ); 78} 79#endif 80 81void TIDY_CALL TY_(filesink_putByte)( void* sinkData, byte bv ) 82{ 83 FILE* fout = (FILE*) sinkData; 84 fputc( bv, fout ); 85} 86 87void TY_(initFileSink)( TidyOutputSink* outp, FILE* fp ) 88{ 89 outp->putByte = TY_(filesink_putByte); 90 outp->sinkData = fp; 91} 92 93/* 94 * local variables: 95 * mode: c 96 * indent-tabs-mode: nil 97 * c-basic-offset: 4 98 * eval: (c-set-offset 'substatement-open 0) 99 * end: 100 */ 101