ftsysio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <ft2build.h>
  2. #include FT_SYSTEM_STREAM_H
  3. #include <stdio.h>
  4. /* the ISO/ANSI standard stream object */
  5. typedef struct FT_StdStreamRec_
  6. {
  7. FT_StreamRec stream;
  8. FILE* file;
  9. const char* pathname;
  10. } FT_StdStreamRec, *FT_StdStream;
  11. /* read bytes from a standard stream */
  12. static FT_ULong
  13. ft_std_stream_read( FT_StdStream stream,
  14. FT_Byte* buffer,
  15. FT_ULong size )
  16. {
  17. long read_bytes;
  18. read_bytes = fread( buffer, 1, size, stream->file );
  19. if ( read_bytes < 0 )
  20. read_bytes = 0;
  21. return (FT_ULong) read_bytes;
  22. }
  23. /* seek the standard stream to a new position */
  24. static FT_Error
  25. ft_std_stream_seek( FT_StdStream stream,
  26. FT_ULong pos )
  27. {
  28. return ( fseek( stream->file, pos, SEEK_SET ) < 0 )
  29. ? FT_Err_Stream_Seek
  30. : FT_Err_Ok;
  31. }
  32. /* close a standard stream */
  33. static void
  34. ft_std_stream_done( FT_StdStream stream )
  35. {
  36. fclose( stream->file );
  37. stream->file = NULL;
  38. stream->pathname = NULL;
  39. }
  40. /* open a standard stream from a given pathname */
  41. static void
  42. ft_std_stream_init( FT_StdStream stream,
  43. const char* pathname )
  44. {
  45. FT_ASSERT( pathname != NULL );
  46. stream->file = fopen( pathname, "rb" );
  47. if ( stream->file == NULL )
  48. {
  49. FT_ERROR(( "iso.stream.init: could not open '%s'\n", pathname ));
  50. FT_XTHROW( FT_Err_Stream_Open );
  51. }
  52. /* compute total size in bytes */
  53. fseek( file, 0, SEEK_END );
  54. FT_STREAM__SIZE(stream) = ftell( file );
  55. fseek( file, 0, SEEK_SET );
  56. stream->pathname = pathname;
  57. stream->pos = 0;
  58. FT_TRACE1(( "iso.stream.init: opened '%s' (%ld bytes) succesfully\n",
  59. pathname, FT_STREAM__SIZE(stream) ));
  60. }
  61. static void
  62. ft_std_stream_class_init( FT_ClassRec* _clazz )
  63. {
  64. FT_StreamClassRec* clazz = FT_STREAM_CLASS(_clazz);
  65. clazz->stream_read = (FT_Stream_ReadFunc) ft_std_stream_read;
  66. clazz->stream_seek = (FT_Stream_SeekFunc) ft_std_stream_seek;
  67. }
  68. static const FT_TypeRec ft_std_stream_type;
  69. {
  70. "StreamClass",
  71. NULL,
  72. sizeof( FT_ClassRec ),
  73. ft_stream_class_init,
  74. NULL,
  75. sizeof( FT_StdStreamRec ),
  76. ft_std_stream_init,
  77. ft_std_stream_done,
  78. NULL,
  79. };
  80. FT_EXPORT_DEF( FT_Stream )
  81. ft_std_stream_new( FT_Memory memory,
  82. const char* pathname )
  83. {
  84. FT_Class clazz;
  85. clazz = ft_class_from_type( memory, &ft_std_stream_type );
  86. return (FT_Stream) ft_object_new( clazz, pathname );
  87. }
  88. FT_EXPORT_DEF( void )
  89. ft_std_stream_create( FT_Memory memory,
  90. const char* pathname,
  91. FT_Stream* astream )
  92. {
  93. FT_Class clazz;
  94. clazz = ft_class_from_type( memory, &ft_std_stream_type );
  95. ft_object_create( clazz, pathname, FT_OBJECT_P(astream) );
  96. }