Click here to Skip to main content
Click here to Skip to main content

Switch between Header and CPP file

By , 26 Sep 2001
 

Introduction

A simple macro that allows you to quickly switch between associated header and implementation files. It's only been tested with Visual Studio 6.

Sub HeaderSourceSwitch()
'DESCRIPTION: Switch between .h and .cpp
'Author: Miezoo (miezoo@f2s.com)

	On Error Resume Next
	Dim sExt, sFile
	
	' current file's extension (all lowercase)
	sExt = fileExtension(ActiveDocument.FullName)
	
	' get file's name (without extension)
	sFile = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - Len(sExt) - 1)

	' valid extension?
	If sExt = "h" or sExt = "hpp" or sExt = "hxx" Then
		sFile = sFile & ".cpp"
	ElseIf sExt = "c" or sExt = "cpp" or sExt = "cxx" Then
		sFile = sFile & ".h"
	Else
		MsgBox "Works for .h and .cpp files only!", vbExclamation, "Error"
		Exit Sub
	End If

	' is the file already opened?
	For Each doc In Application.Documents
		If LCase(doc.Name) = LCase(sFile) Then
			doc.Active = True
			Exit Sub
		End If
	Next

	' see which project contains the damn file
	sFile = findFileWithinWorkspace(sFile)
	Documents.Open sFile

	If Err.Number > 0 Then
		MsgBox "Pair file not found :(", vbCritical, "Error"
		Err.Clear
		Exit Sub
	End If

End Sub


Function findFileWithinWorkspace(file)
'DESCRIPTION: Iterates through all projects (*.dsp) within the current workspace
'             and finds which one owns the given file
	Dim project, bFound, sLine
	sLine = Null

	For Each project In Application.Projects
		' open .dsp file as .txt
		Documents.Open project.FullName, "Text", True
		'if this project is "the one", it must contain a line like: SOURCE=.\path\file
		bFound = ActiveDocument.Selection.FindText(file, dsMatchFromStart + dsMatchRegExp)
		If bFound = True Then
			ActiveDocument.Selection.SelectLine
			sLine = ActiveDocument.Selection
			ActiveDocument.Close
			sLine = Left(sLine, Len(sLine) - 2)		' eliminate LF & CR
			sLine = Right(sLine, Len(sLine) - 8)	' eliminate SOURCE=.\
			
			If Left(sLine, 1) <> "." Then
				sLine = filePath(project.FullName) & sLine
			End If
			
			Exit For
		End If
		ActiveDocument.Close
	Next

	findFileWithinWorkspace = sLine
End Function


Function fileExtension(file)
'DESCRIPTION: Returns file extension.
	Dim iPos, iDot
	
	iDot = Null
	Do While True
		iPos = Instr(1, file, ".", 1)
		If iPos = 0 or iPos = Null Then
			Exit Do
		Else
			file = Right(file, Len(file) - iPos)
			iDot = iPos
		End If
	Loop

	If iDot = Null Then
		fileExtension = ""
	Else
		fileExtension = LCase(file)
	End If

End Function


Function filePath(file)
'DESCRIPTION: Returns file path (without the trailing backslash "\").
	Dim iPos, iBS, strPath
	
	strPath = file
	iBS = Null
	Do While True
		iPos = Instr(1, file, "\", 1)
		If iPos = 0 or iPos = Null Then
			Exit Do
		Else
			file = Right(file, Len(file) - iPos)
			iBS = iPos
		End If
	Loop

	If iBS = Null Then
		filePath = ""
	Else
		filePath = Left(strPath, Len(strPath) - Len(file) - 1)
	End If
End Function

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

About the Author

Miezoo
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCodeWinPosmembermichaelmakuch19 Jul '07 - 16:39 
If you like the switcher then CodeWinPos might be for you too
http://codewinpos.sourceforge.net
GeneralExcellent, slight fix thoughmemberJohn-Lucas Brown26 Jun '06 - 1:43 
Works really well needs a slight change within the "findFileWithinWorkspace" function.
I have pairs of files in my workspace
 
OptionsDlg.cpp and OptionsDlg.h
and
AdvOptionsDlg.cpp and AdvOptionsDlg.h
 
from OptionsDlg.cpp running the macro will load AdvOptionsDlg.h as it's the first one found, the FindText function needs the additional flag "+ dsMatchWord".
 
Function findFileWithinWorkspace(file)
...
bFound = ActiveDocument.Selection.FindText(file, dsMatchFromStart + dsMatchRegExp + dsMatchWord)
....

 
Cheers, John.
GeneralDoes the job...Thanks!memberJithendriya16 Sep '05 - 6:19 
Nice little application!
GeneralIt's useful! thank you!memberxiaohe52112 Sep '05 - 20:21 
It's useful! thank you! you are very clever!
 
I love Programming
QuestionIsn't that already implemented in VC++?memberAnonymous8 Oct '01 - 11:55 

Ummmmm, why not just use the "WizardBar Actions" button that's on the WizardBar toolbar in VC++? (All the way on the right side of the WizardBar toolbar.) Isn't that exactly what that button does? i.e. switch from the header to the implementation and back - including jumping to the correct method?
 

GeneralAnother macro that does the job as well...memberRob Schivas4 Oct '01 - 20:31 
Hi.
 
Try out one of the macros on this link: http://codeguru.earthweb.com/devstudio_macros/open_current_header.shtml#ToggleHandCPP
 
Myself, I'm using the one by Sven Rymenants. This one has a clever search scheme, i.e
it doesn't assume that everybody uses headerfile extension *.h. Some of us actually uses *.hpp as well....
This one seems to work in workspaces containing several projects.
 
Regards
Cool | :cool:
 
Rob
GeneralCodeWiz add-in does a better jobmemberSplintor2 Oct '01 - 23:20 
It can also switch to the specific method declaration/definition
See http://www.ee.oulu.fi/~latti/CodeWiz/ReadMe.html
GeneralFilename matching problemsmemberJanne2 Oct '01 - 21:58 
I'm no expert on these matters, but it seems to work better if
 
bFound = ActiveDocument.Selection.FindText(file, dsMatchFromStart + dsMatchRegExp)
 
is replaced with
 
bFound = ActiveDocument.Selection.FindText(file, dsMatchFromStart + dsMatchWord)
 
(When I invoked the macro with a file "NumEdit.h", I expected to see
"NumEdit.cpp"; instead i got "FixNumEdit.cpp", which is another file
in my workspace, but certainly not what I wanted!)

 
Janne
GeneralHmmmmmemberRob Schivas2 Oct '01 - 21:15 
Hi!
Looks ok, but what if you e.g have a workspace containing two project; Pro01.dsp and Pro02.dsp? So what??
Well I'll explain:
- Pro01 contains the source and header files test01.cpp and test01.hpp, respectively.
- Pro02 happens to contain the same filenames, but this is NOT the same files.
- The file test01.cpp, belonging to Pro02, is the active document. I invoke method HeaderSourceSwitch
in your macro, and guess what happens??... test01.hpp that is owned by Pro01.dsp got focus!!!
 
- Function "findFileWithinWorkspace" is the problem. Its search scheme terminates on first hit, i.e the
macro might not work properly in workspaces with several projects.
 
I guess You will figure out a workaround.
Keep up the good work!
 
Wink | ;)
 
Rob
GeneralGood I love this macro.memberKevin Cao27 Sep '01 - 18:51 
Bind it to LeftAlt+H
Thank You! Thank you Thank you!!!! Big Grin | :-D Big Grin | :-D Big Grin | :-D Big Grin | :-D
 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 27 Sep 2001
Article Copyright 2001 by Miezoo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid