home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzCE2_src.ZIP / FrotzCE / FrotzCEIO.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-06  |  4.3 KB  |  226 lines

  1. #include "stdafx.h"
  2. #include "FrotzCEIO.h"
  3.  
  4. class CFrotzFile : public CFile
  5. {
  6. public:
  7.     CFrotzFile( );
  8.     CFrotzFile( int hFile );
  9.     CFrotzFile( LPCTSTR lpszFileName, UINT nOpenFlags );
  10.  
  11.     BOOL
  12.         m_bError;
  13. };
  14.  
  15. CFrotzFile::CFrotzFile( )
  16. : m_bError( FALSE )
  17. {
  18. }
  19.  
  20. CFrotzFile::CFrotzFile( int hFile )
  21. : CFile( hFile ), m_bError( FALSE )
  22. {
  23. }
  24.  
  25. CFrotzFile::CFrotzFile( LPCTSTR lpszFileName, UINT nOpenFlags )
  26. : CFile( lpszFileName, nOpenFlags ), m_bError( FALSE )
  27. {
  28. }
  29.  
  30.  
  31. extern "C" FILE *fopen( const char *filename, const char *mode )
  32. {
  33. CFrotzFile *pcFile;
  34. int nFileMode = 0;
  35. CString strFileName = filename;
  36.  
  37.     // Check file mode
  38.     if (strchr( mode, 'r' ) != NULL) 
  39.     {
  40.         if (strchr( mode, '+' ) != NULL) nFileMode = CFrotzFile::modeReadWrite;
  41.         else nFileMode = CFrotzFile::modeRead;
  42.     }
  43.     else if (strchr( mode, 'w' ) != NULL) 
  44.     {
  45.         if (strchr( mode, '+' ) != NULL) nFileMode = CFrotzFile::modeReadWrite;
  46.         else nFileMode = CFrotzFile::modeWrite;
  47.         
  48.         nFileMode |= CFrotzFile::modeCreate;
  49.     }
  50.     else if (strchr( mode, 'a' ) != NULL) 
  51.     {
  52.         if (strchr( mode, '+' ) != NULL) nFileMode = CFrotzFile::modeReadWrite;
  53.         else nFileMode = CFrotzFile::modeWrite;
  54.         
  55.         nFileMode |= CFrotzFile::modeCreate | CFrotzFile::modeNoTruncate;
  56.     }
  57.     if (strchr( mode, 'b' ) != NULL) nFileMode |= CFrotzFile::typeBinary;
  58.     else nFileMode |= CFrotzFile::typeText;
  59.  
  60.     // Create new file object
  61.     if((pcFile = new CFrotzFile) != NULL)
  62.     {
  63.         // Open file
  64.         if (pcFile->Open( strFileName, nFileMode ) == 0)
  65.         {
  66.             delete pcFile;
  67.             pcFile = NULL;
  68.         }
  69.     }
  70.  
  71.     return pcFile;
  72. }
  73.  
  74. extern "C" size_t fread( void *buffer, size_t size, size_t count, FILE *stream )
  75. {
  76. CFrotzFile *pcFile = (CFrotzFile *) stream;
  77.  
  78.     // Read specified no. of bytes from file
  79.     return (size_t) (pcFile->Read( buffer, size*count ) / size);
  80. }
  81.  
  82. extern "C" int fseek( FILE *stream, long offset, int origin )
  83. {
  84. CFrotzFile *pcFile = (CFrotzFile *) stream;
  85. UINT nFrom;
  86. int nReturn = 0;
  87.  
  88.     // Check seek origin
  89.     switch (origin)
  90.     {
  91.     case SEEK_SET:
  92.         nFrom = CFrotzFile::begin;
  93.         break;
  94.     case SEEK_CUR:
  95.         nFrom = CFrotzFile::current;
  96.         break;
  97.     case SEEK_END:
  98.         nFrom = CFrotzFile::end;
  99.         break;
  100.     }
  101.  
  102.     TRY
  103.     {
  104.         // Seek to specified offset
  105.         pcFile->Seek( offset, nFrom );
  106.     }
  107.     CATCH( CFileException, e )
  108.     {
  109.         pcFile->m_bError = TRUE;
  110.         nReturn = -1;
  111.     }
  112.     END_CATCH
  113.  
  114.     return nReturn;
  115. }
  116.  
  117. extern "C" long ftell( FILE *stream )
  118. {
  119. CFrotzFile *pcFile = (CFrotzFile *) stream;
  120. DWORD dwCurrPos = pcFile->GetPosition();
  121.  
  122.     // Return actual file position
  123.     return (long) pcFile->Seek( dwCurrPos, CFrotzFile::begin );
  124. }
  125.  
  126. extern "C" int fclose( FILE *stream )
  127. {
  128. CFrotzFile *pcFile = (CFrotzFile *) stream;
  129.  
  130.     // Close file
  131.     pcFile->Close();
  132.  
  133.     // Delete file object
  134.     delete pcFile;
  135.     pcFile = NULL;
  136.  
  137.     return 0;
  138. }
  139.  
  140. extern "C" int fgetc( FILE *stream )
  141. {
  142. CFrotzFile *pcFile = (CFrotzFile *) stream;
  143. unsigned char ch;
  144.  
  145.     // Read a byte from the file    
  146.     if (pcFile->Read( &ch, 1 )) return (int) ch;
  147.     else return EOF;
  148. }
  149.  
  150. extern "C" int ferror( FILE *stream )
  151. {
  152. CFrotzFile *pcFile = (CFrotzFile *) stream;
  153.  
  154.     return pcFile->m_bError;
  155. }
  156.  
  157. extern "C" size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream )
  158. {
  159. CFrotzFile *pcFile = (CFrotzFile *) stream;
  160. size_t nReturn = size * count;
  161.  
  162.     TRY
  163.     {
  164.         // Write buffer to file
  165.         pcFile->Write( buffer, size * count );
  166.     }
  167.     CATCH( CFileException, e )
  168.     {
  169.         pcFile->m_bError = TRUE;
  170.         nReturn = 0;
  171.     }
  172.     END_CATCH
  173.  
  174.     return nReturn;
  175. }
  176.  
  177. extern "C" int fputc( int c, FILE *stream )
  178. {
  179. CFrotzFile *pcFile = (CFrotzFile *) stream;
  180. unsigned char ch = (unsigned char) c;
  181. int nReturn = c;
  182.  
  183.     TRY
  184.     {
  185.         // Write char to file
  186.         pcFile->Write( &ch, 1 );
  187.     }
  188.     CATCH( CFileException, e )
  189.     {
  190.         pcFile->m_bError = TRUE;
  191.         nReturn = EOF;
  192.     }
  193.     END_CATCH
  194.  
  195.     return nReturn;
  196. }
  197.  
  198. extern "C" void debug_msg( const char *pszString, int nNum )
  199. {
  200. CString strDebug, strMsg = pszString;
  201.  
  202.     strDebug.Format( TEXT("%s - %d"), (LPCTSTR) strMsg , nNum);
  203.     AfxMessageBox( strDebug);
  204. }
  205.  
  206. extern "C" int fputs( const char *string, FILE *stream )
  207. {
  208. CFrotzFile *pcFile = (CFrotzFile *) stream;
  209. size_t nReturn = strlen( string );
  210.  
  211.     TRY
  212.     {
  213.         // Write string to file
  214.         pcFile->Write( string, nReturn );
  215.     }
  216.     CATCH( CFileException, e )
  217.     {
  218.         pcFile->m_bError = TRUE;
  219.         nReturn = EOF;
  220.     }
  221.     END_CATCH
  222.  
  223.     return nReturn;
  224. }
  225.  
  226.