/** * @file NFFilePtrA.h * @brief FILE auto close class * @remarks * @author °­µ¿¸í(edith2580@gmail.com) * @date 2009-04-02 */ #pragma once #include namespace Nave { /** * @class NFFilePtrW * @brief FILE auto close class * @remarks NFFilePtrW file(L"c:\1.txt", L"rt"); \r\n * fgets(str, 128, file); \r\n * return; * @par * @author Edith * @date 2009-04-05 */ class NFFilePtrW { public: /// NFFilePtrW »ý¼ºÀÚ NFFilePtrW() : m_pFile(NULL) { } /** * @brief ÆÄÀÏ ÀÚµ¿¿­±â »ý¼ºÀÚ * @param file ÆÄÀϸí * @param mode ÆÄÀÏ ¸ðµå */ NFFilePtrW(LPCWSTR file, LPCWSTR mode) { m_pFile = _wfopen(file, mode); } /// NFFilePtrW ¼Ò¸êÀÚ ~NFFilePtrW() { if(m_pFile) fclose(m_pFile); m_pFile = NULL; } /** * @brief NFFilePtrW1 = FilePtrW2 ÀÌÅÍ·¹ÀÌÅÍ * @warning NFFilePtrW2´Â ´õÀÌ»ó »ç¿ëÇÒ¼ö ¾ø½À´Ï´Ù. */ NFFilePtrW& operator=(NFFilePtrW& _Right) throw() { Reset(_Right.Release()); return (*this); } /** * @brief NFFilePtrW = FILE* ÀÌÅÍ·¹ÀÌÅÍ * @warning NFFilePtrW¿¡¼­ ÀÚµ¿ fclose°¡ µË´Ï´Ù. \r\n * FILE*°´Ã¼¸¦ fcloseÇÏ¸é ¹®Á¦°¡ ¹ß»ýÇÒ ¼öÀÖ½À´Ï´Ù. */ NFFilePtrW& operator=(FILE* _Right) throw() { Reset(_Right); return (*this); } /** * @brief FILE* Çüº¯È¯ ¿ÀÆÛ·¹ÀÌÅÍ ÀÔ´Ï´Ù. * @return ÇöÀçÀÇ ÆÄÀÏ Æ÷ÀÎÅÍ */ operator FILE*() const throw() { return m_pFile; } /** * @brief ÆÄÀÏ Æ÷ÀÎÅ͸¦ ÃʱâÈ­ ÇÕ´Ï´Ù. FILEÀÌ close µÇÁö ¾Ê½À´Ï´Ù. * @return ÇöÀçÀÇ ÆÄÀÏ Æ÷ÀÎÅÍ */ FILE* Release() { FILE* fp = m_pFile; m_pFile = NULL; return fp; } /** * @brief »õ·Î¿î °ªÀ¸·Î Àç¼³Á¤ÇÕ´Ï´Ù. ±âÁ¸ÀÇ FILEÀº close µË´Ï´Ù. * @param fp »õ·Î¿î ÆÄÀÏ Æ÷ÀÎÅÍ */ void Reset(LPCWSTR file, LPCWSTR mode) { if(m_pFile) fclose(m_pFile); m_pFile = _wfopen(file, mode); } /** * @brief »õ·Î¿î °ªÀ¸·Î Àç¼³Á¤ÇÕ´Ï´Ù. * ±âÁ¸ÀÇ FILEÀº close µË´Ï´Ù. * @param fp »õ·Î¿î ÆÄÀÏ Æ÷ÀÎÅÍ */ void Reset(FILE* fp) { if(m_pFile) fclose(m_pFile); m_pFile = fp; } /** * @brief ÆÄÀÏÀÇ ³¡ÀÎÁö °Ë»çÇÕ´Ï´Ù. */ int IsEOF() { return feof(m_pFile); } /** * @brief ÆÄÀÏÀÇ ±æÀ̸¦ ¸®ÅÏÇÕ´Ï´Ù. * @return ÆÄÀÏÀÇ ±æÀÌ */ long Length() { fpos_t cuspos; if(fgetpos( m_pFile, &cuspos ) != 0) return -1; // error fseek( m_pFile, 0L, SEEK_END ); long length = ftell( m_pFile ); fsetpos(m_pFile, &cuspos); return length; } private: /// ÆÄÀÏ Æ÷ÀÎÅÍ FILE* m_pFile; }; /** * @class NFFilePtrA * @brief FILE auto close class * @remarks NFFilePtrA file("c:\1.txt", "rt"); \r\n * fgets(str, 128, file); \r\n * return; * @par * @author Edith * @date 2009-04-05 */ class NFFilePtrA { public: /// NFFilePtrA »ý¼ºÀÚ NFFilePtrA() : m_pFile(NULL) { } /** * @brief ÆÄÀÏ ÀÚµ¿¿­±â »ý¼ºÀÚ * @param file ÆÄÀϸí * @param mode ÆÄÀÏ ¸ðµå */ NFFilePtrA(LPCSTR file, LPCSTR mode) { m_pFile = fopen(file, mode); } /// NFFilePtrA ¼Ò¸êÀÚ ~NFFilePtrA() { if(m_pFile) fclose(m_pFile); m_pFile = NULL; } /** * @brief NFFilePtr1 = FilePtrW2 ÀÌÅÍ·¹ÀÌÅÍ * @warning NFFilePtr2´Â ´õÀÌ»ó »ç¿ëÇÒ¼ö ¾ø½À´Ï´Ù. */ NFFilePtrA& operator=(NFFilePtrA& _Right) throw() { Reset(_Right.Release()); return (*this); } /** * @brief NFFilePtrA = FILE* ÀÌÅÍ·¹ÀÌÅÍ * @warning NFFilePtr¿¡¼­ ÀÚµ¿ fclose°¡ µË´Ï´Ù. \r\n * FILE*°´Ã¼¸¦ fcloseÇÏ¸é ¹®Á¦°¡ ¹ß»ýÇÒ ¼öÀÖ½À´Ï´Ù. */ NFFilePtrA& operator=(FILE* _Right) throw() { Reset(_Right); return (*this); } /** * @brief FILE* Çüº¯È¯ ¿ÀÆÛ·¹ÀÌÅÍ ÀÔ´Ï´Ù. * @return ÇöÀçÀÇ ÆÄÀÏ Æ÷ÀÎÅÍ */ operator FILE*() const throw() { return m_pFile; } /** * @brief ÆÄÀÏ Æ÷ÀÎÅ͸¦ ÃʱâÈ­ ÇÕ´Ï´Ù. FILEÀÌ close µÇÁö ¾Ê½À´Ï´Ù. * @return ÇöÀçÀÇ ÆÄÀÏ Æ÷ÀÎÅÍ */ FILE* Release() { FILE* fp = m_pFile; m_pFile = NULL; return fp; } /** * @brief »õ·Î¿î °ªÀ¸·Î Àç¼³Á¤ÇÕ´Ï´Ù. ±âÁ¸ÀÇ FILEÀº close µË´Ï´Ù. * @param fp »õ·Î¿î ÆÄÀÏ Æ÷ÀÎÅÍ */ void Reset(LPCSTR file, LPCSTR mode) { if(m_pFile) fclose(m_pFile); m_pFile = fopen(file, mode); } /** * @brief »õ·Î¿î °ªÀ¸·Î Àç¼³Á¤ÇÕ´Ï´Ù. * ±âÁ¸ÀÇ FILEÀº close µË´Ï´Ù. * @param fp »õ·Î¿î ÆÄÀÏ Æ÷ÀÎÅÍ */ void Reset(FILE* fp) { if(m_pFile) fclose(m_pFile); m_pFile = fp; } /** * @brief ÆÄÀÏÀÇ ³¡ÀÎÁö °Ë»çÇÕ´Ï´Ù. */ int IsEOF() { return feof(m_pFile); } /** * @brief ÆÄÀÏÀÇ ±æÀ̸¦ ¸®ÅÏÇÕ´Ï´Ù. * @return ÆÄÀÏÀÇ ±æÀÌ */ long Length() { fpos_t cuspos; if(fgetpos( m_pFile, &cuspos ) != 0) return -1; // error fseek( m_pFile, 0L, SEEK_END ); long length = ftell( m_pFile ); fsetpos(m_pFile, &cuspos); return length; } private: /// ÆÄÀÏ Æ÷ÀÎÅÍ FILE* m_pFile; }; }