summaryrefslogtreecommitdiff
path: root/libfreetype/ftsysio.c
blob: 344ee516dddb82ee9762a65517d54bbe43720b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <ft2build.h>
#include FT_SYSTEM_STREAM_H

#include <stdio.h>

 /* the ISO/ANSI standard stream object */
  typedef struct FT_StdStreamRec_
  {
    FT_StreamRec  stream;
    FILE*         file;
    const char*   pathname;
  
  } FT_StdStreamRec, *FT_StdStream;



 /* read bytes from a standard stream */
  static FT_ULong
  ft_std_stream_read( FT_StdStream   stream,
                      FT_Byte*       buffer,
                      FT_ULong       size )
  {
    long   read_bytes;
    
    read_bytes = fread( buffer, 1, size, stream->file );
    if ( read_bytes < 0 )
      read_bytes = 0;
      
    return (FT_ULong) read_bytes;
  }


 /* seek the standard stream to a new position */
  static FT_Error
  ft_std_stream_seek( FT_StdStream   stream,
                      FT_ULong       pos )
  {
    return ( fseek( stream->file, pos, SEEK_SET ) < 0 )
         ? FT_Err_Stream_Seek
         : FT_Err_Ok;
  }
  
  
 /* close a standard stream */  
  static void
  ft_std_stream_done( FT_StdStream  stream )
  {
    fclose( stream->file );
    stream->file     = NULL;
    stream->pathname = NULL;
  }


 /* open a standard stream from a given pathname */
  static void
  ft_std_stream_init( FT_StdStream  stream,
                      const char*   pathname )
  {
    FT_ASSERT( pathname != NULL );

    stream->file = fopen( pathname, "rb" );
    if ( stream->file == NULL )
    {
      FT_ERROR(( "iso.stream.init: could not open '%s'\n", pathname ));
      FT_XTHROW( FT_Err_Stream_Open );
    }
    
    /* compute total size in bytes */
    fseek( file, 0, SEEK_END );
    FT_STREAM__SIZE(stream) = ftell( file );
    fseek( file, 0, SEEK_SET );
    
    stream->pathname = pathname;
    stream->pos      = 0;
    
    FT_TRACE1(( "iso.stream.init: opened '%s' (%ld bytes) succesfully\n",
                 pathname, FT_STREAM__SIZE(stream) ));
  }                 


  static void
  ft_std_stream_class_init( FT_ClassRec*  _clazz )
  {
    FT_StreamClassRec*  clazz = FT_STREAM_CLASS(_clazz);
    
    clazz->stream_read = (FT_Stream_ReadFunc) ft_std_stream_read;
    clazz->stream_seek = (FT_Stream_SeekFunc) ft_std_stream_seek;
  }


  static const FT_TypeRec  ft_std_stream_type;
  {
    "StreamClass",
    NULL,
    
    sizeof( FT_ClassRec ),
    ft_stream_class_init,
    NULL,
    
    sizeof( FT_StdStreamRec ),
    ft_std_stream_init,
    ft_std_stream_done,
    NULL,
  };
  


  FT_EXPORT_DEF( FT_Stream )
  ft_std_stream_new( FT_Memory    memory,
                     const char*  pathname )
  {
    FT_Class  clazz;
    
    clazz = ft_class_from_type( memory, &ft_std_stream_type );
    
    return (FT_Stream) ft_object_new( clazz, pathname );
  }                     


  FT_EXPORT_DEF( void )
  ft_std_stream_create( FT_Memory    memory,
                        const char*  pathname,
                        FT_Stream*   astream )
  {
    FT_Class  clazz;
    
    clazz = ft_class_from_type( memory, &ft_std_stream_type );
    
    ft_object_create( clazz, pathname, FT_OBJECT_P(astream) );
  }