Click here to Skip to main content
15,893,486 members
Articles / Desktop Programming / MFC

WWhizInterface: Enhancements to the Visual C++ Automation Interface

Rate me:
Please Sign up or sign in to vote.
4.50/5 (5 votes)
28 Jul 2001 149.9K   2.6K   47  
A C++ interface with a number of Visual C++ automation enhancements, allowing for more robust add-in programming.
///////////////////////////////////////////////////////////////////////////////
// $Workfile: FileList.cpp $
// $Archive: /WorkspaceWhiz/Src/WWhizInterface/FileList.cpp $
// $Date:: 1/03/01 12:13a  $ $Revision:: 15   $ $Author: Jjensen $
///////////////////////////////////////////////////////////////////////////////
// This source file is part of the Workspace Whiz! source distribution and
// is Copyright 1997-2001 by Joshua C. Jensen.  (http://workspacewhiz.com/)
//
// The code presented in this file may be freely used and modified for all
// non-commercial and commercial purposes so long as due credit is given and
// this header is left intact.
///////////////////////////////////////////////////////////////////////////////
#include "FileList.h"
#include "WorkspaceInfo.h"

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

///////////////////////////////////////////////////////////////////////////////
// FileMap
///////////////////////////////////////////////////////////////////////////////

FileMap::~FileMap()
{
	RemoveAll();
}


File* FileMap::Get(CString fullName) const
{
	fullName.MakeLower();
	File* file;
	if (m_fileMap.Lookup(fullName, file))
		return file;
	return NULL;
}


bool FileMap::Get(File* file) const
{
	if (m_fileMap.Lookup(file->GetFullName(), file))
		return true;
	return false;
}

	
bool FileMap::Add(File* file)
{
	m_fileMap[file->GetFullName()] = file;
	file->AddRef();

	return true;
}


bool FileMap::Remove(File* file)
{
	m_fileMap.RemoveKey(file->GetFullName());
	file->Release();

	return true;
}


void FileMap::RemoveAll()
{
	CString fullName;
	File* file;

	POSITION pos = m_fileMap.GetStartPosition();
	while (pos)
	{
		m_fileMap.GetNextAssoc(pos, fullName, file);
		file->Release();
	}

	m_fileMap.RemoveAll();
}


void FileMap::InitHashTable(DWORD hashSize)
{
	m_fileMap.InitHashTable(hashSize);
}


FileMap g_globalFileMap;

/**
	Only valid for the global file map.
**/
void FileMap::CleanUp()
{
	if (this != &g_globalFileMap)
		return;

	POSITION pos = m_fileMap.GetStartPosition();
	while (pos)
	{
		CString fullName;
		File* file;
		m_fileMap.GetNextAssoc(pos, fullName, file);
		if (file->GetRefCount() == 1)
			Remove(file);
	}
}


///////////////////////////////////////////////////////////////////////////////
// File
///////////////////////////////////////////////////////////////////////////////

WWHIZ_DEFINE_REFCOUNT(File)

/**
**/
bool File::Create(File& file, const CString& filenameConst)
{
	CString filename = filenameConst;

	// Find the path.
	int pathEndPosition = filename.ReverseFind('\\');
	if (pathEndPosition == -1)
		return false;
	file.SetPath(filename.Left(pathEndPosition + 1));
	filename = filename.Mid(pathEndPosition + 1);

	// Find the extension.
	int extPosition = filename.ReverseFind('.');
	if (extPosition != -1)
	{
		file.m_ext = filename.Mid(extPosition + 1);
		file.m_ext.MakeLower();
	}
	else
		extPosition = filename.GetLength();

	// Find the file title.
	file.m_title = filename.Left(extPosition);

	// Build the shortened name (no symbols).
	char shortName[200];
	int sLen = 0;
	for (int i = 0; i < file.m_title.GetLength(); i++)
		if (isalnum(file.m_title[i]))
			shortName[sLen++] = (char)tolower(file.m_title[i]);
	shortName[sLen] = 0;
	file.m_shortName = shortName;

	file.m_fullName = file.m_path + file.m_title;
	if (!file.m_ext.IsEmpty())
		file.m_fullName += "." + file.m_ext;

	file.m_fullName.MakeLower();

	file.m_timeStamp = CTime(0);

	return true;
}


/**
**/
File* File::Create(const CString& filename)
{
	// Check the map.
	File* file = g_globalFileMap.Get(filename);
	if (file)
		return file;

	// Create a new file.
	file = new File;
	File::Create(*file, filename);

	// Add to map.
	g_globalFileMap.Add(file);

	return file;
}


/**
**/
File::File() :
	m_touched(0),
	m_changed(false),
	m_tagBufferSize(0),
	m_tags(NULL),
	m_workspaceFile(false)
{
}


File::~File()
{
	ClearTags();
}


void File::ClearTags()
{
	// Clean up the tag lists and tag buffer.
	m_tagBuffer = NULL;
	m_tagBufferSize = 0;
	delete [] m_tags;
	m_tags = NULL;
	m_orderedTagList.RemoveAll();
	m_tagList.RemoveAll();
}

	
///////////////////////////////////////////////////////////////////////////////
// FileList
///////////////////////////////////////////////////////////////////////////////

FileList::FileList()
{
	m_files.SetSize(0, 100);
}


FileList::~FileList()
{
	RemoveAll();
}


/**
	Remove all files from the container.
**/
void FileList::RemoveAll()
{
	m_files.RemoveAll();
	m_fileMap.RemoveAll();
}


WWhizFile& FileList::Create(const CString& filename)
{
	File* file = new File;
	File::Create(*file, filename);
	file->AddRef();
	return *file;
}


static int __cdecl CompareArray(const void* elem1, const void* elem2)
{
	File* file1 = *(File**)elem1;
	File* file2 = *(File**)elem2;

	int ret = file1->GetShortName().Compare(file2->GetShortName());
	if (ret == 0)
	{
		// If the names match, compare against the file extensions.
		ret = file1->GetExt().Compare(file2->GetExt());
		if (ret == 0)
		{
			// If the extensions match, compare against the path.
			ret = file1->GetPath().CompareNoCase(file2->GetPath());
		}
	}
	return ret;
}
	

void FileList::Sort()
{
	// Sort the file array.
	qsort(m_files.GetData(), m_files.GetSize(), sizeof(File*), CompareArray);
}

// Find exact file index.
int FileList::FindExact(WWhizFile& file) const
{
	// Scan the file list.
	for (int i = 0; i < GetCount(); i++)
	{
		WWhizFile* fileCmp = Get(i);

		// Compare the extension.
		if (!file.GetExt().IsEmpty()  &&  file.GetExt() != fileCmp->GetExt())
			continue;

		// Compare the file titles.
		if (file.GetTitle().CompareNoCase(fileCmp->GetTitle()) != 0)
			continue;

		// Compare the path.
		if (file.GetPath().CompareNoCase(fileCmp->GetPath()) == 0)
		{
			return i;
		}
	}

	return -1;
}


// Find next file index.
int FileList::FindNext(int startPos, WWhizFile& file) const
{
	// Scan the file list.
	for (int i = startPos + 1; i < GetCount(); i++)
	{
		WWhizFile* fileCmp = Get(i);

		// Compare the file titles.
		if (file.GetTitle().CompareNoCase(fileCmp->GetTitle()) == 0)
			return i;
	}

	for (i = 0; i < startPos; i++)
	{
		WWhizFile* fileCmp = Get(i);

		// Compare the file titles.
		if (file.GetTitle().CompareNoCase(fileCmp->GetTitle()) == 0)
			return i;
	}

	return -1;
}


/**
	Find previous file index.
**/
int FileList::FindPrevious(int startPos, WWhizFile& file) const
{
	// Scan the file list.
	for (int i = startPos - 1; i >= 0; i--)
	{
		WWhizFile* fileCmp = Get(i);

		// Compare the file titles.
		if (file.GetTitle().CompareNoCase(fileCmp->GetTitle()) == 0)
			return i;
	}

	for (i = GetCount() - 1; i > startPos; i--)
	{
		WWhizFile* fileCmp = Get(i);

		// Compare the file titles.
		if (file.GetTitle().CompareNoCase(fileCmp->GetTitle()) == 0)
			return i;
	}

	return -1;
}


/**
**/
File* FileList::Add(const CString& fullPath)
{
	File* file = File::Create(fullPath);
	m_fileMap.Add(file);
	return file;
}


/**
	Add file to the end of the file list.
**/
bool FileList::Add(File* file)
{
	if (!m_fileMap.Get(file))
	{
		m_files.Add(file);
		m_fileMap.Add(file);
		return true;
	}

	return false;
}

	
/**
**/
void FileList::Remove(int index)
{
	m_fileMap.Remove(m_files[index]);
	m_files.RemoveAt(index);
}

	

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
Web Developer
United States United States
Joshua Jensen is a gamer at heart and as such, creates games for a living. He has the distinct pleasure of creating titles exclusively for the Xbox.

In his spare time, he maintains a Visual C++ add-in called Workspace Whiz! Find it at http://workspacewhiz.com/.

Comments and Discussions