Version:
~ [ 1.0 ] ~
1 /* fileio.c -- does standard I/O
2
3 (c) 1998-2005 (W3C) MIT, ERCIM, Keio University
4 See tidy.h for the copyright notice.
5
6 CVS Info :
7
8 $Author: arnaud02 $
9 $Date: 2005/08/02 10:07:28 $
10 $Revision: 1.8 $
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 "fileio.h"
20 #include "tidy.h"
21
22
23 typedef struct _fp_input_source
24 {
25 FILE* fp;
26 TidyBuffer unget;
27 } FileSource;
28
29 static int TIDY_CALL filesrc_getByte( ulong sourceData )
30 {
31 FileSource* fin = (FileSource*) sourceData;
32 int bv;
33 if ( fin->unget.size > 0 )
34 bv = tidyBufPopByte( &fin->unget );
35 else
36 bv = fgetc( fin->fp );
37 return bv;
38 }
39
40 static Bool TIDY_CALL filesrc_eof( ulong sourceData )
41 {
42 FileSource* fin = (FileSource*) sourceData;
43 Bool isEOF = ( fin->unget.size == 0 );
44 if ( isEOF )
45 isEOF = feof( fin->fp ) != 0;
46 return isEOF;
47 }
48
49 static void TIDY_CALL filesrc_ungetByte( ulong sourceData, byte bv )
50 {
51 FileSource* fin = (FileSource*) sourceData;
52 tidyBufPutByte( &fin->unget, bv );
53 }
54
55 void TIDY_CALL initFileSource( TidyInputSource* inp, FILE* fp )
56 {
57 FileSource* fin = NULL;
58
59 inp->getByte = filesrc_getByte;
60 inp->eof = filesrc_eof;
61 inp->ungetByte = filesrc_ungetByte;
62
63 fin = (FileSource*) MemAlloc( sizeof(FileSource) );
64 ClearMemory( fin, sizeof(FileSource) );
65 fin->fp = fp;
66 inp->sourceData = (ulong) fin;
67 }
68
69 void TIDY_CALL freeFileSource( TidyInputSource* inp, Bool closeIt )
70 {
71 FileSource* fin = (FileSource*) inp->sourceData;
72 if ( closeIt && fin && fin->fp )
73 fclose( fin->fp );
74 tidyBufFree( &fin->unget );
75 MemFree( fin );
76 }
77
78 void TIDY_CALL filesink_putByte( ulong sinkData, byte bv )
79 {
80 FILE* fout = (FILE*) sinkData;
81 fputc( bv, fout );
82 }
83
84 void TIDY_CALL initFileSink( TidyOutputSink* outp, FILE* fp )
85 {
86 outp->putByte = filesink_putByte;
87 outp->sinkData = (ulong) fp;
88 }
89
90
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.