⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os2file.c

📁 字体缩放显示
💻 C
📖 第 1 页 / 共 3 页
字号:
  /********                                                   ********/  /********  R E E N T R A N T   I M P L E M E N T A T I O N  ********/  /********                                                   ********/  /*******************************************************************/  /*******************************************************************/  /*******************************************************************//* a simple macro to access the file component's data */#define files  ( *((TFile_Component*)engine.file_component) )#define CUR_Stream   STREAM2REC( stream )    /* re-entrant macros */#define CUR_Frame    (*frame)#define STREAM_VARS  stream,#define STREAM_VAR   stream/******************************************************************* * *  Function    :  TTFile_Init * *  Description :  Initializes the File component. * ******************************************************************/ FT_INTERNAL_FUNC( TT_Error ) TTFile_Init( PEngine_Instance  engine ) {   return TT_Err_Ok; }/******************************************************************* * *  Function    :  TTFile_Done * *  Description :  Finalizes the File component. * ******************************************************************/ FT_INTERNAL_FUNC( TT_Error ) TTFile_Done( PEngine_Instance  engine ) {   return TT_Err_Ok; }/******************************************************************* * *  Function    :  TT_Use_Stream * *  Description :  Duplicates a stream for a new usage. * *  Input  :  input_stream   source stream to duplicate *            copy           address of target duplicate stream * *  Output :  error code. *            The target stream is set to NULL in case of failure. * ******************************************************************/  FT_EXPORT_FUNC( TT_Error )  TT_Use_Stream( TT_Stream   input_stream,                 TT_Stream*  copy )  {    PStream_Rec  rec = STREAM2REC( input_stream );    return TT_Open_Stream( rec->name, copy );  }/******************************************************************* * *  Function    : TT_Done_Stream * *  Description : Releases a given stream. * *  Input  :  stream  target stream * *  Output : * ******************************************************************/ FT_EXPORT_FUNC( TT_Error ) TT_Done_Stream( TT_Stream*  stream ) {   return TT_Close_Stream( stream ); }/******************************************************************* * *  Function    :  TT_Access_Frame * *  Description :  Notifies the component that we're going to read *                 'size' bytes from the current file position. *                 This function should load/cache/map these bytes *                 so that they will be addressed by the GET_xxx() *                 functions easily. * *  Input  :  size   number of bytes to access. * *  Output :  SUCCESS on success.  FAILURE on error. * *  Notes:    The function fails if the byte range is not within the *            the file, or if there is not enough memory to cache *            the bytes properly (which usually means that `size' is *            too big in both cases). * ******************************************************************/  FT_EXPORT_FUNC( TT_Error )  TT_Access_Frame( STREAM_ARGS FRAME_ARGS int  size )  {    TT_Error  error;    if ( CUR_Frame.address != NULL )      return TT_Err_Nested_Frame_Access;    if ( ALLOC( CUR_Frame.address, size ) )      return error;    CUR_Frame.size    = size;    error = TT_Read_File( STREAM_VARS (void*)CUR_Frame.address, size );    if ( error )    {      FREE( CUR_Frame.address );      CUR_Frame.size    = 0;    }    CUR_Frame.cursor = CUR_Frame.address;    return error;  }/******************************************************************* * *  Function    :  TT_Check_And_Access_Frame * *  Description :  Notifies the component that we're going to read *                 `size' bytes from the current file position. *                 This function should load/cache/map these bytes *                 so that they will be addressed by the GET_xxx() *                 functions easily. * *  Input  :  size   number of bytes to access. * *  Output :  SUCCESS on success.  FAILURE on error. * *  Notes:    The function truncates `size' if the byte range is not *            within the file. * *            It will fail if there is not enough memory to cache *            the bytes properly (which usually means that `size' is *            too big). * *            It will fail if you make two consecutive calls *            to TT_Access_Frame(), without a TT_Forget_Frame() between *            them. * *            The only difference with TT_Access_Frame() is that we *            check that the frame is within the current file.  We *            otherwise truncate it. * ******************************************************************/  FT_EXPORT_FUNC( TT_Error )  TT_Check_And_Access_Frame( STREAM_ARGS FRAME_ARGS int  size )  {    TT_Error  error;    Long      readBytes;    if ( CUR_Frame.address != NULL )      return TT_Err_Nested_Frame_Access;    if ( ALLOC( CUR_Frame.address, size ) )      return error;    CUR_Frame.size    = size;    readBytes = CUR_Stream->size - TT_File_Pos( STREAM_VAR );    if ( size > readBytes )      size = readBytes;    error = TT_Read_File( STREAM_VARS (void*)CUR_Frame.address, size );    if ( error )    {      FREE( CUR_Frame.address );      CUR_Frame.size    = 0;    }    CUR_Frame.cursor = CUR_Frame.address;    return error;  }/******************************************************************* * *  Function    :  TT_Forget_Frame * *  Description :  Releases a cached frame after reading. * *  Input  :  None * *  Output :  SUCCESS on success.  FAILURE on error. * ******************************************************************/  FT_EXPORT_FUNC( TT_Error )  TT_Forget_Frame( FRAME_ARG )  {    if ( CUR_Frame.address == NULL )      return TT_Err_Nested_Frame_Access;    FREE( CUR_Frame.address );    ZERO_Frame( CUR_Frame );    return TT_Err_Ok;  }#endif /* TT_CONFIG_OPTION_THREAD_SAFE */  /*******************************************************************/  /*******************************************************************/  /*******************************************************************/  /***********                                             ***********/  /***********  C O M M O N   I M P L E M E N T A T I O N  ***********/  /***********                                             ***********/  /*******************************************************************/  /*******************************************************************/  /*******************************************************************//******************************************************************* * *  Function    :  Stream_Activate * *  Description :  activates a stream, this will either: *                   - open a new file handle if the stream is closed *                   - move the stream to the head of the linked list * *  Input  :  stream   the stream to activate * *  Output :  error condition. * *  Note   :  this function is also called with fresh new streams *            created by TT_Open_Stream().  They have their 'size' *            field set to -1. * ******************************************************************/  static  TT_Error  Stream_Activate( PStream_Rec  stream )  {    ULONG ulAction;  /* !Mike! */    if ( !stream->opened )    {#if 0    if ( !(stream->file = fopen( stream->name, "rb" )) )    /* !Mike! */#endif/* XXX : Strange.  GCC/EMX wants an (Byte*) for the file name? */#ifdef __EMX__      if ( DosOpen( (Byte*)stream->name, &(stream->file),                    &ulAction, 0,                    0, OPEN_ACTION_OPEN_IF_EXISTS,                    OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY, NULL ) )        return TT_Err_Could_Not_ReOpen_File;#else      if ( DosOpen( stream->name, &(stream->file),                    &ulAction, 0,                    0, OPEN_ACTION_OPEN_IF_EXISTS,                    OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY, NULL ) )        return TT_Err_Could_Not_ReOpen_File;#endif /* __EMX__ */      stream->opened = TRUE;      /* A newly created stream has a size field of -1 */      if ( stream->size < 0 )      {#if 0        fseek( stream->file, 0, SEEK_END );             /* !Mike! */        stream->size = ftell( stream->file );#endif        DosSetFilePtr( stream->file, 0, FILE_END, (ULONG *)&(stream->size) );#if 0        fseek( stream->file, 0, SEEK_SET );#endif        DosSetFilePtr( stream->file, 0, FILE_BEGIN, &ulAction );      }      /* Reset cursor in file */      if ( stream->position )      {#if 0   /* !Mike! */        if ( fseek( stream->file, stream->position, SEEK_SET ) != 0 )#endif         if ( DosSetFilePtr( stream->file, stream->position,                            FILE_BEGIN, &ulAction ) )        {          /* error during seek */#if 0          fclose( stream->file );           /* !Mike! */#endif          DosClose( stream->file );          stream->opened = FALSE;          return TT_Err_Could_Not_ReSeek_File;        }      }    }    return TT_Err_Ok;  }/******************************************************************* * *  Function    :  Stream_DeActivate * *  Description :  deactivates a stream, this will: *                   - close its file handle if it was opened *                   - remove it from the opened list if necessary * *  Input  :  stream   the stream to deactivate * *  Output :  Error condition * *  Note   :  the function is called whenever a stream is deleted *            (_not_ when a stream handle's is closed due to an *            activation).  However, the stream record isn't *            destroyed by it... * ******************************************************************/  static  TT_Error  Stream_Deactivate( PStream_Rec  stream )  {    if ( stream->opened )    {      /* Save its current position within the file */#if 0      stream->position = ftell( stream->file );     /* !Mike! */#endif      DosSetFilePtr( stream->file, 0, FILE_CURRENT,                     (ULONG *)&(stream->position) );#if 0      fclose( stream->file );                       /* !Mike! */#endif      DosClose( stream->file );      stream->file   = 0;      stream->opened = FALSE;    }    return TT_Err_Ok;  }/******************************************************************* * *  Function    :  TT_Stream_Size * *  Description :  Returns the length of a given stream, even if it *                 is flushed. * *  Input  :  stream     the stream * *  Output :  length of stream in bytes. * ******************************************************************/  FT_EXPORT_FUNC( Long )  TT_Stream_Size( TT_Stream  stream )  {    PStream_Rec  rec = STREAM2REC( stream );    if ( rec )      return rec->size;    else      return 0;  /* invalid stream - return 0 */  }/******************************************************************* * *  Function    :  TT_Open_Stream * *  Description :  Opens the font file and saves the total file size. * *  Input  :  error          address of stream's error variable *                           (re-entrant build only) *            filepathname   pathname of the file to open *            stream         address of target TT_Stream structure * *  Output :  SUCCESS on sucess, FAILURE on error. *            The target stream is set to -1 in case of failure. * ******************************************************************/  FT_INTERNAL_FUNC( TT_Error )  TT_Open_Stream( const String*  filepathname,                  TT_Stream*     stream )  {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -