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

Include File Hierarchy Viewer

Rate me:
Please Sign up or sign in to vote.
4.84/5 (108 votes)
29 Jul 2003CPOL8 min read 385.9K   4.5K   151  
A tool to view the include file hierarchy of your source code.
//====================================================================
// Although great care has gone into developing this software,
// it is provided without any guarantee of reliability, accuracy
// of information, or correctness of operation.  I am not responsible
// for any damages that may occur as a result of using this software.
// Use this software entirely at your own risk.
// Copyright 2003, Chris Richardson
//
// Description: Represents the results of a parsed file.
//
//====================================================================

#include "stdafx.h"
#include "Global.h"
#include "ParsedFile.h"

#include "pugxml.h"

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


//////////////////////////////////////////////////////////////////////
// CParsedFile Implementation.
//////////////////////////////////////////////////////////////////////

CParsedFile::CParsedFile( const TCHAR * p_pszFile ) :
   c_sFileName( p_pszFile ),
   c_bNotFoundFlag( false ),
   c_bAlreadyIncluded( false ),
   c_ulLineCount( 0 )
{
}
//
// ------------------------------------------------------------------
//
CParsedFile::~CParsedFile()
{
}
//
// ------------------------------------------------------------------
//
void CParsedFile::AddIncludedFile( CParsedFile * p_poFile )
{
   c_oIncludedFiles.push_back( p_poFile );
}
//
// ------------------------------------------------------------------
//
void CParsedFile::AddIncludedByFile( CParsedFile * p_poFile )
{
   c_oIncludedByFiles.push_back( p_poFile );
}
//
// ------------------------------------------------------------------
//
void CParsedFile::SetAlreadyIncluded( bool p_bIncluded )
{
   if( c_bAlreadyIncluded == p_bIncluded )
      return;

   c_bAlreadyIncluded = p_bIncluded;
   if( !p_bIncluded )
   {
      // Clear the flag for all our included files as well.
      for( int i = 0; i<c_oIncludedFiles.size(); ++i )
      {
         c_oIncludedFiles[i]->SetAlreadyIncluded( false );
      }
   }
}
//
// ------------------------------------------------------------------
//
void CParsedFile::SaveToFile( FILE * p_pstFile, int p_iLevel )
{
   BOOL a_bRecurse = TRUE;
   if( GetAlreadyIncluded() )
   {
      // This file was already included.  We don't want to recurse because
      // we might infinitely recurse (i.e, "rpc.h" includes "windows.h", which includes "rpc.h").
      a_bRecurse = FALSE;
   }
   SetAlreadyIncluded( true );
   
   TCHAR a_szSpacePadding[1024] = {0};
   int a_iSpaces = 0;

   for( int i = 0; i<p_iLevel + 1; ++i )
   {
      _tcscat( a_szSpacePadding, _T("   ") );
      a_iSpaces += 3;

      if( i == p_iLevel - 1 )
         _ftprintf( p_pstFile, a_szSpacePadding );
      
      if( a_iSpaces >= 1020 )
         break;
   }

   int a_iIncludedFiles = c_oIncludedFiles.size();
   _ftprintf( p_pstFile,
              _T("<File\n%sName=\"%s\"\n%sFound=\"%d\"\n%sMultiple=\"%d\"\n%sLines=\"%d\"\n%sIncludedFiles=\"%d\">\n"),
              a_szSpacePadding,
              c_sFileName.c_str(),
              a_szSpacePadding,
              !c_bNotFoundFlag,
              a_szSpacePadding,
              a_bRecurse ? 0 : 1,
              a_szSpacePadding,
              c_ulLineCount,
              a_szSpacePadding,
              a_iIncludedFiles );

   if( a_bRecurse )
   {
      // Now recursively insert all our included files.
      
      for( int i = 0; i<a_iIncludedFiles; ++i )
      {
         if( c_oIncludedFiles[i] == this )
         {
            // We included ourself.  What to do?
            continue;
         }
         
         c_oIncludedFiles[i]->SaveToFile( p_pstFile, p_iLevel + 1 );
      }
   }
   
   // Chop off some spaces so we will go back down one level.
   for( int s = 0; s<3; ++s )
   {
      if( !a_iSpaces )
         break;

      a_szSpacePadding[a_iSpaces - 1] = _T('\0');
      a_iSpaces--;
   }
   _ftprintf( p_pstFile, _T("%s</File>\n"), a_szSpacePadding );
}
//
// ------------------------------------------------------------------
//
void CParsedFile::ReadFromXml( CPugXmlBranch *     p_poBranch,
                               CParsedFileList &   p_roResults )
{
   c_bNotFoundFlag = !p_poBranch->GetAttribute( _T("Found"), true );
   
   for( int i = 0; i<p_poBranch->GetChildrenCount(); ++i )
   {
      CPugXmlBranch a_oFile = p_poBranch->GetChildAt( i );
      if( !a_oFile.IsNamed( _T("File") ) )
         continue;
      
      const TCHAR * a_pszFileName = a_oFile.GetAttribute( _T("Name") );
      CParsedFile * a_poFile = NULL;

      CParsedFileArray::iterator a_oIter = c_oIncludedFiles.begin();
      while( a_oIter != c_oIncludedFiles.end() )
      {
         if( !_tcscmp( (*a_oIter)->GetFileName().c_str(), a_pszFileName ) )
         {
            // The file has already been parsed.
            a_poFile = *a_oIter;
            break;
         }
         
         a_oIter++;
      }
      
      if( !a_poFile )
      {
         CParsedFileList::iterator a_oIter = p_roResults.begin();
         while( a_oIter != p_roResults.end() )
         {
            if( !_tcscmp( (*a_oIter)->GetFileName().c_str(), a_pszFileName ) )
            {
               // The file has already been parsed.
               a_poFile = *a_oIter;
               c_oIncludedFiles.push_back( a_poFile );
               break;
            }
         
            a_oIter++;
         }
      }

      if( !a_poFile )
      {
         // Create a new file object.
         a_poFile = new CParsedFile( a_pszFileName );
         p_roResults.push_back( a_poFile );
         c_oIncludedFiles.push_back( a_poFile );
      }
      
      a_poFile->ReadFromXml( &a_oFile, p_roResults );
   }
}
//
// ------------------------------------------------------------------
//

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)
United States United States
I like to program, I like to sail.

Comments and Discussions