Click here to Skip to main content
15,896,503 members
Articles / Desktop Programming / MFC

JesInclAnalyzer

Rate me:
Please Sign up or sign in to vote.
4.81/5 (9 votes)
11 Mar 2009CPOL5 min read 20.3K   214   5  
Include Analyzer (Unwanted header file inclusion removal)
/*
 * Copy-Right(c) 2009 Jellow T. K., Refer CPOL Clauses.
 *
 * JesFileProcessor.cpp - JesFileProcessor class implementation file.
 *
 * @version:    1.0            Date:  10- 3-2009
 */


#include "stdafx.h"
#include "JesInclAnalyzer.h"
#include "JesFileProcessor.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

static const CString NEW_LINE = _T( "\n" );

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

JesFileProcessor::JesFileProcessor()
{

}

JesFileProcessor::JesFileProcessor( const CString& csFileName_i ) 
                  : m_bObjCreated( true ),
                    m_dwCurPos( 0 ),
                    m_dwPrevPos( 0 ),
                    m_eUpdateStatus( NONE )
{
    // Make a copy of Source file
    const CString& TMP_FILE_SUFFIX = _T( "_Copy.cpp" );
    const int EXT_SIZE = 4; // .cpp
    m_csFileName = csFileName_i.Left( csFileName_i.GetLength() - EXT_SIZE ) + TMP_FILE_SUFFIX;
    if( !CopyFile( csFileName_i, m_csFileName, FALSE ))
    {
        TCHAR szBuf[ MAX_PATH ]; 
        LPVOID lpMsgBuf;
        DWORD dwLastErr = GetLastError(); 
        FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                       FORMAT_MESSAGE_FROM_SYSTEM,
                       NULL, dwLastErr, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
                       ( LPTSTR ) &lpMsgBuf,
                       0, NULL );
        wsprintf( szBuf, _T( " CopyFile() failed with error %d :- %s" ),
                  dwLastErr, lpMsgBuf );
        CString csErrMsg = csFileName_i + szBuf;
        AfxMessageBox( csErrMsg );
        LocalFree( lpMsgBuf );
        
        m_bObjCreated = false;
        return;
    }
}

JesFileProcessor::~JesFileProcessor()
{
    if( CFile::hFileNull != m_SrcFile.m_hFile )
    {
        m_SrcFile.Close();
    }    
}

CString& JesFileProcessor::GetTempFileName()
{
    return m_csFileName;
}

bool JesFileProcessor::GetUpdatedFile( bool bRevertLastChange_i )
{
    if( !m_bObjCreated )
    {
        return false;
    }
    
    if( bRevertLastChange_i )
    {
        if( !UpdateLine( true, m_dwPrevPos, m_csPrevLine ))
        {
            DeleteFile( m_csFileName );
            return false;
        }
        if( UPDATED != m_eUpdateStatus )
        {
            m_eUpdateStatus = REVERTED;
        }        
    }
    else if( MARKED == m_eUpdateStatus )
    {
        m_eUpdateStatus = UPDATED;
    }
    else
    {
        // Do nothing
    }
    
    // Open file for processing
    if( !m_SrcFile.Open( m_csFileName, CFile::modeReadWrite, &m_FileException ))
    {
        ShowFileException();
        DeleteFile( m_csFileName );
        return false; 
    }

    m_SrcFile.Seek( m_dwCurPos, CFile::begin );

    static const CString COMMENT_LINE = _T( "//" );
    static const CString INCLUDE_MACRO = _T( "#include" );
    CString csLine;
    while( true )
    {
        m_dwPrevPos = m_SrcFile.GetPosition();
        if( !m_SrcFile.ReadString( csLine ))
        {
            m_SrcFile.Close();
            if( UPDATED != m_eUpdateStatus )
            {
                DeleteFile( m_csFileName );
            }
            return false;
        }
        
        if( 0 == csLine.Find( INCLUDE_MACRO, 0 ))
        {
            m_csPrevLine = csLine;
            break;
        }        
    }

    CString csCommentLine = COMMENT_LINE + csLine;
    if( !UpdateLine( false, m_dwPrevPos, csCommentLine ))
    {
        m_SrcFile.Close();
        DeleteFile( m_csFileName );
        return false;
    }
    
    if( UPDATED != m_eUpdateStatus )
    {
        m_eUpdateStatus = MARKED;
    }
    
    m_SrcFile.Close();

    return true;
}

bool JesFileProcessor::UpdateLine( bool bOpenFile_i, DWORD dwLinePos_i, const CString& csUpdateLine_i )
{
    // Open file for processing, if not opened already
    if( bOpenFile_i )
    {
        if( !m_SrcFile.Open( m_csFileName, CFile::modeReadWrite, &m_FileException ))
        {
            ShowFileException();
            return false; 
        }
    }

    m_SrcFile.Seek( dwLinePos_i, CFile::begin );
    // Skip line to be updated
    CString csLine;
    m_SrcFile.ReadString( csLine );
    CStringArray FileLines;
    while( m_SrcFile.ReadString( csLine ))
    {
        FileLines.Add( csLine );
    }
    m_SrcFile.Seek( dwLinePos_i, CFile::begin );
    m_SrcFile.WriteString( csUpdateLine_i + NEW_LINE );
    m_dwCurPos = m_SrcFile.GetPosition();
    int nSize = FileLines.GetSize();
    for( int nIdx = 0; nIdx < nSize; ++nIdx )
    {
        m_SrcFile.WriteString( FileLines[ nIdx ] + NEW_LINE );
    }
    if( bOpenFile_i )
    {
        m_SrcFile.Close();
    }
            
    return true;
}

void JesFileProcessor::ShowFileException()
{
    CString csErrReason;
    switch( m_FileException.m_cause )
    {
    case CFileException::generic:
        csErrReason = _T( "an unspecified error occurred." );
        break;

    case CFileException::fileNotFound:
        csErrReason = _T( "the file could not be located." );
        break;

    case CFileException::badPath:
        csErrReason = _T( "all or part of the path is invalid." );
        break;

    case CFileException::tooManyOpenFiles:
        csErrReason = _T( "the permitted number of open files was exceeded." );
        break;

    case CFileException::accessDenied:
        csErrReason = _T( "the file could not be accessed." );
        break;

    case CFileException::invalidFile:
        csErrReason = _T( "there was an attempt to use an invalid file handle." );
        break;

    case CFileException::removeCurrentDir:
        csErrReason = _T( "the current working directory cannot be removed." );
        break;

    case CFileException::directoryFull:
        csErrReason = _T( "there are no more directory entries." );
        break;

    case CFileException::badSeek:
        csErrReason = _T( "there was an error trying to set the file pointer." );
        break;

    case CFileException::hardIO:
        csErrReason = _T( "there was a hardware error." );
        break;

    case CFileException::sharingViolation:
        csErrReason = _T( "SHARE.EXE was not loaded, or a shared region was locked." );
        break;

    case CFileException::lockViolation:
        csErrReason = _T( "there was an attempt to lock a region that was already locked." );
        break;

    case CFileException::diskFull:
        csErrReason = _T( "the disk is full." );
        break;

    case CFileException::endOfFile:
        csErrReason = _T( "the end of file was reached." );
        break;

    default:
        csErrReason = _T( "unknown error." );
    }

    static const CString ERR_MSG_CONNECTOR = _T( " open failed since " );
    CString csErrMsg = m_FileException.m_strFileName + ERR_MSG_CONNECTOR + csErrReason;
    AfxMessageBox( csErrMsg );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions