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

LintProject - Improving the Usability of PC-Lint with Visual C++ Solutions and Projects

,
Rate me:
Please Sign up or sign in to vote.
4.83/5 (36 votes)
29 Jan 2009CPOL13 min read 281.9K   1.7K   52  
Utility to run PC-Lint on Visual C++ solutions and projects, generating XML and HTML reports of the results.
/************************************************************************
 *
 *  Description : Implementation class for CProjectFileReader.
 *
 *     (c) Copyright 2000-2008 by Anna-Jayne Metcalfe (anna@riverblade.co.uk)
 *     and Beth Mackenzie (beth@riverblade.co.uk) / Riverblade Limited
 *
 *  Licence Terms:
 *
 *     This code may be freely reused, subject to the licence terms below.
 *     Please do let us know of any bugs you find or improvements you make,
 *     so that we can pass them on to the rest of the development community.
 *
 *     This code is free software; you can redistribute it and/or
 *     modify it under the terms of the Code Project Open License (CPOL)
 *     version 1.0 (http://www.codeproject.com/info/cpol10.aspx).
 *
 *     This code is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     Code Project Open Licence for further details.
 *
 ************************************************************************
 *    $Archive: /Projects/Applications/LintProject/Development/Shared/ProjectFileReaderImpl.h $
 *   $Revision: 4 $
 *       $Date: 22/02/08 20:58 $
 *     $Author: Anna $
 * 
 * $Nokeywords: $
 ************************************************************************/

/// \file	
/// \brief Implementation class for CProjectFileReader.

#pragma once

#ifdef _UTILS_LOCAL_INCLUDE
	#include "StringUtils.h"
	#include "FileUtils.h"
	#include "PathUtils.h"
	#include "SplitPath.h"
#else
	#include <Utils\Include\StringUtils.h>
	#include <Utils\Include\FileUtils.h>
	#include <Utils\Include\PathUtils.h>
	#include <Utils\Include\SplitPath.h>
#endif

#include "ProjectConfiguration.h"



namespace Riverblade
{
	namespace Libraries
	{
		namespace AddInSolutionModel
		{
			/// \brief Abstract base class for project file reader implementation classes.
			///
			class CProjectFileReaderImpl
			{
				public:
					/// \brief Main constructor
					///
					CProjectFileReaderImpl(void)
						: 	m_arrayFiles(),
							m_mapAvailableConfigurations(),
							m_mapConfigToSettings(),
							m_mapInternalEnvVars(),
							m_sProjectPathName(_T("") ),
							m_sProjectName(_T("") ),
							m_sSolutionPathName(_T("") )
					{
					}


					/// \brief Class destructor
					///
					virtual ~CProjectFileReaderImpl(void)
					{
					}



				// Data members
				protected:
					///	\brief For internal use only. REMOVE??
					typedef CRBMap<CString, CString> CConfigNameMap;

					/// \brief Maps configuration string (e.g. "Debug|Win32") to the corresponding CProjectConfiguration object.
					typedef	CAtlMap<CString, CProjectConfiguration> CConfigSettingsMap;
					typedef	CAtlMap<CString, CString> CEnvVarsMap;

					CAtlArray<CString>						m_arrayFiles;					///< Array of filenames within the project.
					CConfigNameMap							m_mapAvailableConfigurations;	///< LEGACY. TO BE REMOVED.
					CConfigSettingsMap						m_mapConfigToSettings;			///< Maps configuration strings to the corresponding CProjectConfiguration object.
					CEnvVarsMap								m_mapInternalEnvVars;			///< Maps Visual Studio and user defined environment vars to the corresponding names
					CString									m_sProjectPathName;				///< The pathname of the project file.
					CString									m_sProjectName;					///< The name of the project.
					CString									m_sSolutionPathName;			///< The pathname of the solution file (optional)



				// Operations
				public:
					bool SetSolutionPathName(const CString& sPathName)
					{
						m_sSolutionPathName = sPathName;

						return true;
					}


					/// \brief Read and parse the project file with the given pathname.
					/// 
					/// \param	sPathName			The pathname of the project file
					/// \return						\em true if the project file was parsed successfully;
					///								\em false otherwise.
					///
					bool Read(const CString& sPathName)
					{
						m_sProjectPathName = sPathName;

						// First read the entire file into a buffer for processing
						CString sFileText;
						const HRESULT hr = Utils::ReadTextFile(sPathName, sFileText);
						DBG_UNREFERENCED_LOCAL_VARIABLE(hr);

						ATLASSERT(SUCCEEDED(hr) );

						if (!sFileText.IsEmpty() )
						{
							// Initialise our map of internal environment variable values
							InitialiseEnvironmentVarMap();

							// The file exists and has been read successfully
							// From here on in its up to the parser...
							ATLVERIFY(Parse(sFileText) );

							return true;
						}
						return false;
					}


					/// \brief Parse the text of the project file.
					///
					/// This method is pure virtual and must be overridden by derived classes.
					///
					/// \param		sFileText		The text of the project file.
					/// \return		bool			\em true if the file contents were successfully parsed; false otherwise.
					///
					virtual bool Parse(const CString& sFileText) = 0;


					/// \brief Return the pathname corresponding to the given filename within the project.
					/// 
					/// \param	sFileName			The filename.
					/// \return						The corresponding to the filename.
					///
					CString GetPathName(const CString& sFileName) const
					{
						CString sPathName = sFileName;

						if (!sFileName.IsEmpty() && Utils::IsPathRelative(sFileName) )
						{
							Utils::CSplitPath path(m_sProjectPathName);

							sPathName = Utils::CombinePath(path.GetDrive() + path.GetDirectory(), sFileName);
						}
						return sPathName;
					}


					/// \brief Return the pathname of the project folder.
					///
					/// \return								The pathname of the project folder.
					///
					CString GetProjectFolder(void) const
					{
						Utils::CSplitPath split(m_sProjectPathName);

						return split.GetDrive() + split.GetDirectory();
					}


					/// \brief Return the "configuration string" for a given configuration within the project.
					///
					/// \param	sConfig						The name of the configuration for which the configuration string should be returned.
					/// \return								The corresponding configuration string, or an empty string if the configuration name
					///										is not recognised.
					///
					virtual CString GetConfigurationString(const CString& sConfig) const = 0;


					/// Return the "Intermediate Files" folder for a given configuration within the project.
					///
					/// \param	sConfig						The name of the configuration for which the folder path should be returned.
					/// \return								The corresponding "Intermediate Files" folder path. This is likely to be a relative path.
					///
					CString GetIntermediateFilesFolder(const CString& sConfig) const
					{
						CString sIntermediateFilesFolder;
						if (m_mapAvailableConfigurations.Lookup(sConfig, sIntermediateFilesFolder) )
						{
							return sIntermediateFilesFolder;
						}
						return _T("");
					}


					/// \brief Return the properties of a given configuration within the project.
					///
					/// \param	sConfig						The name of the configuration for which the properties should be returned.
					/// \return								A CProjectConfiguration structure containing the
					///                                     properties of the configuration.
					///
					CProjectConfiguration GetConfigurationProperties(const CString& sConfig) const
					{
						CProjectConfiguration info;
						(void)m_mapConfigToSettings.Lookup(sConfig, info);

						return info;
					}


					/// \brief Return the names of the files within the project
					///
					/// \param	rarrayFiles					An array which will be filled with the filenames of all files in the project.
					/// \param	sExt						An optional extension, which will be used to filter the filenames placed in rarrayFiles.
					/// \return								The number of filenames returned in the array.
					///
					int GetFiles(CAtlArray<CString>& rarrayFiles, const CString& sExt) const
					{
						rarrayFiles.RemoveAll();

						if (sExt.IsEmpty())
						{
							(void)rarrayFiles.Append(m_arrayFiles);
						}
						else
						{
							for (size_t n = 0; n < m_arrayFiles.GetCount(); n++)
							{
								const CString sFile = m_arrayFiles[n];

								Utils::CSplitPath path(sFile);

								if (0 == path.GetExtension().CompareNoCase(sExt) )
								{
									(void)rarrayFiles.Add(sFile);
								}
							}					
						}
						return static_cast<int>(rarrayFiles.GetCount() );
					}


					/// \brief Return the names of the configurations within the project.
					///
					/// \param	rarrayConfigurations		An array which will be filled with the names of the configurations within the project.
					/// \return								The number of configurations returned in the array.
					///
					int GetConfigurations(CAtlArray<CString>& rarrayConfigurations) const
					{
						rarrayConfigurations.RemoveAll();

						POSITION pos = m_mapAvailableConfigurations.GetHeadPosition();
						while (NULL != pos)
						{
							const CString sConfig = m_mapAvailableConfigurations.GetNextKey(pos);

							ATLASSERT(!sConfig.IsEmpty() );
							if (!sConfig.IsEmpty() )
							{
								(void)rarrayConfigurations.Add(sConfig);
							}
						}
						return static_cast<int>(rarrayConfigurations.GetCount() );
					}


					/// \brief Determine whether the given configuration name is valid.
					///
					///	\param	sConfiguration	A configuration string identifying the project configuration.
					/// \return					\em true if the configuration is valid; \em false otherwise.
					///
					bool IsValidConfiguration(const CString& sConfiguration) const
					{
						CString sUnused;
						if (m_mapAvailableConfigurations.Lookup(sConfiguration, sUnused) )
						{
							return true;
						}
						return false;
					}


					/// \brief Expand any Visual Studio environment variables in the given string.
					///
					/// The string may be a pathname, sequence of pathnames or just a delimited string containing
					/// environment variables. Note that only Windows environment vars are currently supported;
					/// Visual Studio specific environment variables such as "$(TargetPath)" are not supported
					/// and will be ignored.
					///
					/// Please note that once full environment var support is added this method will
					/// be moved to CProjectConfiguration.
					///
					/// \param	rsText			A string containing environment variables in Visual Studio
					///							format (e.g. "$(varname)" )
					/// \return					\em true if all environment vars found were expanded; \em false
					///							if any remain unexpanded.
					///
					/// TODO: Handle Visual Studio environment variables:
					///
					///		- $(FilePath)
					///		- $(FileDir)
					///		- $(FileName)
					///		- $(FileExt)
					///		- $(ConfigurationName)	Required
					///		- $(CurLine)
					///		- $(CurCol)
					///		- $(CurText)
					///		- $(CurDir)
					///		- $(InputDir)
					///		- $(InputName)
					///		- $(InputPath)
					///		- $(InputFileName)
					///		- $(InputExt)
					///		- $(IntDir)
					///		- $(OutDir)				Required
					///		- $(ProjectDir)			Required
					///		- $(ProjectFileName)
					///		- $(ProjectName)
					///		- $(ProjectPath)		Required
					///		- $(RemoteMachine)
					///		- $(SolutionDir)		Required
					///		- $(SolutionName)
					///		- $(SolutionExt)
					///		- $(SolutionFileName)
					///		- $(SolutionPath)
					///		- $(TargetDir)			Required
					///		- $(TargetExt)
					///		- $(TargetFileName)
					///		- $(TargetName)			Required
					///		- $(TargetPath)
					///		- $(TargetExt)
					///		- $(TargetArgs)
					///		- $(WkspDir)			Required - same as $(SolutionDir)
					///		- $(WkspName)			Same as $(SolutionName)
					///
					virtual bool ExpandEnvironmentVars(CString& rsText) const
					{
						// Locate all environment vars in the given string. Once we've identified them,
						// they will be expanded 
						CAtlArray<CString> arrayEnvVars;

						int nStartPos = 0;

						while (nStartPos >= 0)
						{
							nStartPos = rsText.Find( _T("$("), nStartPos);
							if (nStartPos >= 0)
							{
								const int nEndPos = rsText.Find( _T(")"), nStartPos + 1);

								if (nEndPos > nStartPos)
								{
									const CString sEnvVar = rsText.Mid(nStartPos, (nEndPos - nStartPos) + 1);

									arrayEnvVars.Add(sEnvVar);
								}
								nStartPos = nEndPos;
							}
						}

						// Now expand the environment vars we found
						for (size_t n = 0; n < arrayEnvVars.GetCount(); n++)
						{
							const CString& sFullEnvVar = arrayEnvVars[n];

							const CString sEnvVar = Utils::Between(sFullEnvVar, _T("$("), _T(")") );
							if (!sEnvVar.IsEmpty() )
							{
								CString sValue;
								if (sValue.GetEnvironmentVariable(sEnvVar) )
								{
									// If the var is a system var, expand it directly
									rsText.Replace(sFullEnvVar, sValue);
								}
								else if (m_mapInternalEnvVars.Lookup(sEnvVar, sValue) )
								{
									// ...otherwise, if the var is an IDE environment var, expand it using the internal var map
									rsText.Replace(sFullEnvVar, sValue);
								}
							}
						}
						return true;
					}


					/// \brief Define the IDE environment variables we support.
					///
					/// Note that this is currently a stub implementation based on the need to support .vsprops files - 
					/// further expansion is therefore likely.
					///
					void InitialiseEnvironmentVarMap(void)
					{
						if (!m_sSolutionPathName.IsEmpty() )
						{
							// VC7 specific
							m_mapInternalEnvVars[_T("SolutionDir")]		= Utils::CSplitPath(m_sSolutionPathName).GetFolderPath();
							m_mapInternalEnvVars[_T("SolutionName")]	= Utils::CSplitPath(m_sSolutionPathName).GetFileName();

							// VC6 specific
							m_mapInternalEnvVars[_T("WkspDir")]			= Utils::CSplitPath(m_sSolutionPathName).GetFolderPath();
							m_mapInternalEnvVars[_T("WkspName")]		= Utils::CSplitPath(m_sSolutionPathName).GetFileName();
						}
						// General
						m_mapInternalEnvVars[_T("ProjectDir")]			= Utils::CSplitPath(m_sProjectPathName).GetFolderPath();
						m_mapInternalEnvVars[_T("ProjectName")]			= m_sProjectName;
					}
			};

		};	// namespace AddInSolutionModel
	};	// namespace Libraries
};	//namespace Riverblade

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
Founder Riverblade Limited
United Kingdom United Kingdom
I haven't always written software for a living. When I graduated from Surrey University in 1989, it was with an Electronic Engineering degree, but unfortunately that never really gave me the opportunity to do anything particularly interesting (with the possible exception of designing Darth Vader's Codpiece * for the UK Army in 1990).
    * Also known as the Standard Army Bootswitch. But that's another story...
Since the opportunity arose to lead a software team developing C++ software for Avionic Test Systems in 1996, I've not looked back. More recently I've been involved in the development of subsea acoustic navigation systems, digital TV broadcast systems, port security/tracking systems, and most recently software development tools with my own company, Riverblade Ltd.

One of my personal specialities is IDE plug-in development. ResOrg was my first attempt at a plug-in, but my day to day work is with Visual Lint, an interactive code analysis tool environment with works within the Visual Studio and Eclipse IDEs or on build servers.

I love lots of things, but particularly music, photography and anything connected with history or engineering. I despise ignorant, intolerant and obstructive people - and it shows...I can be a bolshy cow if you wind me up the wrong way...Laugh | :laugh:

I'm currently based 15 minutes walk from the beach in Bournemouth on the south coast of England. Since I moved here I've grown to love the place - even if it is full of grockles in Summer!

Written By
Software Developer Riverblade Ltd
United Kingdom United Kingdom
I'm a software developer and/or tester with Riverblade Ltd (www.riverblade.co.uk) developing our core product range including our Visual Lint integration product and Lint Project Professional.

I incorporate a number of technologies into a daily basis including Windows API, C++ (VS2008), Managed C++, CLI, Databases, Java, JNI, Eclipse Framework, CDT and of course Visual Studio Extensibility (VSIP VSX).

In my spare time I enjoy cooking (prepping ingredients from scratch!), running, cycling, swimming, reading, interested in experimental electronic music (such as ClockDVA), movies, volunteering my IT skills where I can.

Comments and Discussions