Artifact Content

Not logged in

Artifact 15fdc1958c9c2f8808fa81e5666d1e9c7a01935e


     1  //--- K.I.LIB ---
     2  // kl_file.h : file operations
     3  
     4  #ifndef AFX_KIFILE_H__7D126C1E_3E5C_476E_9A4E_81CA8055621D__INCLUDED_
     5  #define AFX_KIFILE_H__7D126C1E_3E5C_476E_9A4E_81CA8055621D__INCLUDED_
     6  
     7  /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
     8  // バイナリファイル操作
     9  
    10  class kiFile
    11  {
    12  public: //-- static ----------------------------------------
    13  
    14  	// ファイルサイズ取得( 名前, エラー時に返したい値 )
    15  	static unsigned long getSize( const char* fname, unsigned long err=0xffffffff );
    16  	static __int64 getSize64( const char* fname );
    17  
    18  public: //-- 外向きインターフェイス --------------------------
    19  
    20  	// 開いて閉じて
    21  	bool open( const char* filename, bool read=true, bool create=true );
    22  	void close();
    23  
    24  	// 読んで書いて
    25  	unsigned long read( unsigned char* buf, unsigned long len );
    26  	void write( const void* buf, unsigned long len );
    27  	int getc();
    28  	void putc( unsigned char c );
    29  
    30  	// シーク
    31  	void seekTo( unsigned long pos )
    32  		{
    33  			if( !m_bReadMode ) flush();
    34  			::SetFilePointer( m_hFile, pos, NULL, FILE_BEGIN );
    35  			if(  m_bReadMode ) flush();
    36  		}
    37  	void seek( long pos )
    38  		{
    39  			if( !m_bReadMode ) flush();
    40  			::SetFilePointer( m_hFile,
    41  				pos-(signed)m_nBufSize+(signed)m_nBufPos, NULL,
    42  				FILE_CURRENT );
    43  			if(  m_bReadMode ) flush();
    44  		}
    45  	unsigned long tell()
    46  		{
    47  			return ::SetFilePointer( m_hFile, 0, NULL, FILE_CURRENT )
    48  					- m_nBufSize + m_nBufPos;
    49  		}
    50  
    51  	// 情報取得
    52  	bool isOpened()
    53  		{
    54  			return m_hFile != INVALID_HANDLE_VALUE;
    55  		}
    56  	unsigned long getSize( unsigned long* higher=NULL )
    57  		{
    58  			return ::GetFileSize( m_hFile, higher );
    59  		}
    60  	bool isEOF()
    61  		{
    62  			return (m_nBufPos==0 && m_nBufSize==0);
    63  		}
    64  
    65  public: //-- 内部処理 -----------------------------------
    66  
    67  	kiFile() : kifile_bufsize( 65536 )
    68  		{
    69  			m_hFile= INVALID_HANDLE_VALUE;
    70  			m_pBuf = new unsigned char[kifile_bufsize];
    71  		}
    72  
    73  	virtual ~kiFile()
    74  		{
    75  			close();
    76  			delete [] m_pBuf;
    77  		}
    78  
    79  	HANDLE getHandle() const
    80  		{
    81  			return m_hFile;
    82  		}
    83  
    84  private:
    85  	const int kifile_bufsize;
    86  	void flush();
    87  
    88  	HANDLE m_hFile;
    89  	bool   m_bReadMode;
    90  	unsigned char* m_pBuf;
    91  	unsigned long  m_nBufSize, m_nBufPos;
    92  };
    93  
    94  #endif