int LogFile::Open() { int nError = 1; // preset this in case LOGOG_FLAVOR_WINDOWS is not defined bool bFileAlreadyExists = false; FILE *fpTest; #ifdef LOGOG_FLAVOR_WINDOWS nError = fopen_s( &fpTest, m_pFileName, "r"); // ignore the error code #else // LOGOG_FLAVOR_WINDOWS fpTest = fopen( m_pFileName, "r"); #endif // LOGOG_FLAVOR_WINDOWS if ( fpTest != NULL ) { fclose( fpTest ); bFileAlreadyExists = true; } /** Windows tries to be clever and help us with converting line feeds ** to carriage returns when writing a text file. This causes problems ** when writing a Unicode file as Windows helpfully inserts a single-byte ** 0x0D between the return and line feed on write. So we open and operate ** the output in binary mode only. **/ #ifdef LOGOG_FLAVOR_WINDOWS #ifdef LOGOG_UNICODE nError = fopen_s( &m_pFile, m_pFileName, "ab, ccs=UNICODE" ); #else // LOGOG_UNICODE nError = fopen_s( &m_pFile, m_pFileName, "ab" ); #endif // LOGOG_UNICODE if ( nError != 0 ) return nError; #else // LOGOG_FLAVOR_WINDOWS m_pFile = fopen( m_pFileName, "ab+" ); #endif // LOGOG_FLAVOR_WINDOWS if ( m_pFile == NULL ) m_bOpenFailed = true; // and no further outputs will work else { #ifdef LOGOG_UNICODE if ( !bFileAlreadyExists ) { WriteUnicodeBOM(); } #endif } return ( m_pFile ? 0 : -1 ); }
int LogFile::Open() { #ifdef LOGOG_UNICODE bool bFileAlreadyExists = false; #endif // LOGOG_UNICODE FILE *fpTest; #ifdef LOGOG_FLAVOR_WINDOWS int nError; // only exists in Windows build nError = fopen_s( &fpTest, m_pFileName, "r"); // ignore the return code for now #else // LOGOG_FLAVOR_WINDOWS fpTest = fopen( m_pFileName, "r"); #endif // LOGOG_FLAVOR_WINDOWS if ( fpTest != NULL ) { fclose( fpTest ); #ifdef LOGOG_UNICODE bFileAlreadyExists = true; #endif // LOGOG_UNICODE } /** Windows tries to be clever and help us with converting line feeds ** to carriage returns when writing a text file. This causes problems ** when writing a Unicode file as Windows helpfully inserts a single-byte ** 0x0D between the return and line feed on write. So we open and operate ** the output in binary mode only. **/ #ifdef LOGOG_FLAVOR_WINDOWS #ifdef LOGOG_UNICODE const char *openMode = "ab, ccs=UNICODE"; #else // LOGOG_UNICODE const char *openMode = "ab"; #endif // LOGOG_UNICODE m_pFile = _fsopen( m_pFileName, openMode, _SH_DENYWR ); nError = (m_pFile != NULL) ? (0) : (errno); if ( nError != 0 ) return nError; #else // LOGOG_FLAVOR_WINDOWS m_pFile = fopen( m_pFileName, "ab+" ); #endif // LOGOG_FLAVOR_WINDOWS if ( m_pFile == NULL ) m_bOpenFailed = true; // and no further outputs will work else { #ifdef LOGOG_UNICODE if ( !bFileAlreadyExists ) { WriteUnicodeBOM(); } #endif } // Disable output buffering if requested. // Buffering is performed by default. if (!m_bEnableOutputBuffering && m_pFile) { setvbuf(m_pFile, NULL, _IONBF, 0); } return ( m_pFile ? 0 : -1 ); }