Click here to Skip to main content
15,895,709 members
Articles / Desktop Programming / WTL

Cyclomatic Complexity Viewer

Rate me:
Please Sign up or sign in to vote.
4.29/5 (13 votes)
30 Jun 20055 min read 108.8K   2.8K   34  
A Cyclomatic complexity viewer application.
// Commands.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "CycloComplexViewer.h"
#include "Commands.h"
#include "parser.h"
#include "graphpreparator.h"
#include "graphbuilder.h"
#include "filegenerator.h"
#include "errordlg.h"
#include "mvaluedlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCommands

CCommands::CCommands()
{
	m_pApplication = NULL;
	m_pApplicationEventsObj = NULL;
	m_pDebuggerEventsObj = NULL;
}

CCommands::~CCommands()
{
	ASSERT (m_pApplication != NULL);
	m_pApplication->Release();
}

void CCommands::SetApplicationObject(IApplication* pApplication)
{
	// Diese Funktion geht davon aus, dass AddRef bereits auf pApplication angewendet wurde,
	//  was CDSAddIn durch den Aufruf von QueryInterface direkt vor dem Aufruf dieser
	//  Funktion bereits erledigt hat.
	m_pApplication = pApplication;

	// Ereignis-Handler f�r Anwendung erzeugen
	XApplicationEventsObj::CreateInstance(&m_pApplicationEventsObj);
	m_pApplicationEventsObj->AddRef();
	m_pApplicationEventsObj->Connect(m_pApplication);
	m_pApplicationEventsObj->m_pCommands = this;

	// Ereignis-Handler f�r Debugger erzeugen
	CComPtr<IDispatch> pDebugger;
	if (SUCCEEDED(m_pApplication->get_Debugger(&pDebugger)) 
		&& pDebugger != NULL)
	{
		XDebuggerEventsObj::CreateInstance(&m_pDebuggerEventsObj);
		m_pDebuggerEventsObj->AddRef();
		m_pDebuggerEventsObj->Connect(pDebugger);
		m_pDebuggerEventsObj->m_pCommands = this;
	}
}

void CCommands::UnadviseFromEvents()
{
	ASSERT (m_pApplicationEventsObj != NULL);
	m_pApplicationEventsObj->Disconnect(m_pApplication);
	m_pApplicationEventsObj->Release();
	m_pApplicationEventsObj = NULL;

	if (m_pDebuggerEventsObj != NULL)
	{
		// Da wir die Verbindung zu den Debugger-Ereignissen herstellen konnten, 
		//  sollte es auch m�glich sein, erneut auf das Debugger-Objekt zuzugreifen, 
		//  um die Verbindung zu seinen Ereignissen zu trennen (daher das VERIFY_OK weiter unten -- siehe stdafx.h).
		CComPtr<IDispatch> pDebugger;
		VERIFY_OK(m_pApplication->get_Debugger(&pDebugger));
		ASSERT (pDebugger != NULL);
		m_pDebuggerEventsObj->Disconnect(pDebugger);
		m_pDebuggerEventsObj->Release();
		m_pDebuggerEventsObj = NULL;
	}
}


/////////////////////////////////////////////////////////////////////////////
// Ereignis-Handler

// ZU ERLEDIGEN: F�llen Sie die Implementierung f�r die Ereignisse aus, die Sie behandeln wollen
//  Verwenden Sie m_pCommands->GetApplicationObject(), um auf das Objekt
//  "Developer Studio Application" zuzugreifen

//Application-Ereignisse

HRESULT CCommands::XApplicationEvents::BeforeBuildStart()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BuildFinish(long nNumErrors, long nNumWarnings)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BeforeApplicationShutDown()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::DocumentOpen(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BeforeDocumentClose(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::DocumentSave(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::NewDocument(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WindowActivate(IDispatch* theWindow)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WindowDeactivate(IDispatch* theWindow)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WorkspaceOpen()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WorkspaceClose()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::NewWorkspace()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

// Debugger-Ereignis

HRESULT CCommands::XDebuggerEvents::BreakpointHit(IDispatch* pBreakpoint)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}


/////////////////////////////////////////////////////////////////////////////
// Ccommands-Methoden

STDMETHODIMP CCommands::CycloComplexViewerCommandMethod() 
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// ZU ERLEDIGEN: Ersetzen Sie dies durch den Code zur tats�chlichen Ausf�hrung des Befehls
	//  Verwenden Sie m_pApplication, um auf das Objekt "Developer Studio Application" zuzugreifen
	//  und VERIFY_OK, um Fehlermeldungen in DEBUG-Builds des Add-Ins anzusehen
	//  (siehe stdafx.h)

	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
	CComPtr<IDispatch> pDispDoc;
	m_pApplication->get_ActiveDocument(&pDispDoc);
	CComQIPtr<ITextDocument, &IID_ITextDocument> pDocument;
	pDocument = pDispDoc;
	pDispDoc = NULL;

	if (pDocument)
	{
		CComPtr<IDispatch> pDispSel;
		pDocument->get_Selection(&pDispSel);
		CComQIPtr<ITextSelection, &IID_ITextSelection> pSelection;
		pSelection = pDispSel;
		pDispSel = NULL;

		CComBSTR bstr;
		HRESULT hr = pSelection->get_Text(&bstr);
		if (SUCCEEDED(hr))
		{
			CString text(bstr);
			int length = text.GetLength();
			if (length == 0)
			{
				MessageBox(NULL , "Select a function", "CycloComplexViewer", MB_OK);
				return hr;
			}
			CParser parser;
			string codebuffer(text);
			parser.Parse(codebuffer, length);
			CGraphPreparator graphbuilder;
			graphbuilder.WriteNodes("c:\\nodes.txt");
			CGraphBuilder tree;
			tree.BuildGraph();
			int i = tree.CalculateMetric();
#ifdef _DEBUG
			graphbuilder.WriteVector("c:\\tree.txt");
			string name = "\\.\\output.dot";
			CFileGenerator filebuilder(name);
			filebuilder.WriteFile();
			string parameter = "-Tpng ";
			parameter += name;
			parameter += " -o graph.png";
			if (!ShellExecute(NULL, "open", "dot.exe", parameter.c_str(), "C:\\", SW_HIDE))
			{
				GetLastError();
			}
#endif
			CMValueDlg metric_dlg;
			metric_dlg.LoadValue(i);
			metric_dlg.DoModal();
			//does what it says
			tree.Cleanup();
		}
	}
	else
	{
		CErrorDlg errorbox;
		errorbox.DoModal();
	}

	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
	return S_OK;
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
I am a qualified Veterinary Surgeon who prefers treating computers with viruses than animals with viruses. I have recently completed a MEng German Informatics degree at the University of Reading with a 2:1. I also have the ISEB Foundation Certificate in Software Testing.

Currently I am umemployed and desparately looking for a job in the IT industry.

Comments and Discussions