Coverage Report

Created: 2025-08-26 06:03

/src/tidy-html5/src/fileio.c
Line
Count
Source (jump to first uncovered line)
1
/* fileio.c -- does standard I/O
2
3
  (c) 1998-2007 (W3C) MIT, ERCIM, Keio University
4
  See tidy.h for the copyright notice.
5
6
  Default implementations of Tidy input sources
7
  and output sinks based on standard C FILE*.
8
9
*/
10
11
#include <stdio.h>
12
13
#include "forward.h"
14
#include "fileio.h"
15
#include "tidy.h"
16
#include "sprtf.h"
17
18
typedef struct _fp_input_source
19
{
20
    FILE*        fp;
21
    TidyBuffer   unget;
22
} FileSource;
23
24
static int TIDY_CALL filesrc_getByte( void* sourceData )
25
0
{
26
0
  FileSource* fin = (FileSource*) sourceData;
27
0
  int bv;
28
0
  if ( fin->unget.size > 0 )
29
0
    bv = tidyBufPopByte( &fin->unget );
30
0
  else
31
0
    bv = fgetc( fin->fp );
32
0
  return bv;
33
0
}
34
35
static Bool TIDY_CALL filesrc_eof( void* sourceData )
36
0
{
37
0
  FileSource* fin = (FileSource*) sourceData;
38
0
  Bool isEOF = ( fin->unget.size == 0 );
39
0
  if ( isEOF )
40
0
    isEOF = feof( fin->fp ) != 0;
41
0
  return isEOF;
42
0
}
43
44
static void TIDY_CALL filesrc_ungetByte( void* sourceData, byte bv )
45
0
{
46
0
  FileSource* fin = (FileSource*) sourceData;
47
0
  tidyBufPutByte( &fin->unget, bv );
48
0
}
49
50
#if SUPPORT_POSIX_MAPPED_FILES
51
#  define initFileSource initStdIOFileSource
52
#  define freeFileSource freeStdIOFileSource
53
#endif
54
int TY_(initFileSource)( TidyAllocator *allocator, TidyInputSource* inp, FILE* fp )
55
0
{
56
0
  FileSource* fin = NULL;
57
58
0
  fin = (FileSource*) TidyAlloc( allocator, sizeof(FileSource) );
59
0
  if ( !fin )
60
0
      return -1;
61
0
  TidyClearMemory( fin, sizeof(FileSource) );
62
0
  fin->unget.allocator = allocator;
63
0
  fin->fp = fp;
64
65
0
  inp->getByte    = filesrc_getByte;
66
0
  inp->eof        = filesrc_eof;
67
0
  inp->ungetByte  = filesrc_ungetByte;
68
0
  inp->sourceData = fin;
69
70
0
  return 0;
71
0
}
72
73
void TY_(freeFileSource)( TidyInputSource* inp, Bool closeIt )
74
0
{
75
0
    FileSource* fin = (FileSource*) inp->sourceData;
76
0
    if ( closeIt && fin && fin->fp )
77
0
      fclose( fin->fp );
78
0
    tidyBufFree( &fin->unget );
79
0
    if (fin)
80
0
        TidyFree( fin->unget.allocator, fin );
81
0
}
82
83
void TIDY_CALL TY_(filesink_putByte)( void* sinkData, byte bv )
84
0
{
85
0
  FILE* fout = (FILE*) sinkData;
86
0
  fputc( bv, fout );
87
#if defined(ENABLE_DEBUG_LOG)
88
  if (fileno(fout) != 2)
89
  {
90
      if (bv != 0x0d)
91
      {
92
          /*\
93
           * avoid duplicate newline - SPRTF will translate an 0x0d to CRLF,
94
           *  and do the same with the following 0x0a
95
          \*/
96
          SPRTF("%c",bv);
97
      }
98
  }
99
#endif
100
0
}
101
102
void TY_(initFileSink)( TidyOutputSink* outp, FILE* fp )
103
0
{
104
0
  outp->putByte  = TY_(filesink_putByte);
105
0
  outp->sinkData = fp;
106
0
}
107
108
/*
109
 * local variables:
110
 * mode: c
111
 * indent-tabs-mode: nil
112
 * c-basic-offset: 4
113
 * eval: (c-set-offset 'substatement-open 0)
114
 * end:
115
 */