Make File Pair

Macro that creates a .h / .cpp file pair.
It will prompt for a file name and create the .h / .cpp files accordingly.
The .h file will be given a small header and the usual #ifndef _FILE_H ... sentry.
The .cpp file will be given an #include "MyHFile.h".

The technique behind this code generation is patended and trademarked in 123 countrys so dont you dare modifying it....heh...not really, do as you wish :-)

The whole thing is split up into 3 separate macros (each can be run individually):
  • MakeHFileifdef
  • IncludeMyH
  • MakeFilePair

Sub MakeHFileifdef()
'DESCRIPTION: Generates #ifdef THISHFILE_H ... #endif definitions

      'if name = "ARNE.H" then s = "_ARNE_H"
	  s = Ucase(Application.ActiveDocument.Name)
	  p = Len(s)
	  for i = 1 to Len(s)	
	    if Mid(s,i,1)="." then p = i-1
	  next
	  s = "_" + Left(s,p) + "_H"

	  Application.ActiveDocument.Selection.StartOfDocument
 	  Application.ActiveDocument.Selection = "#ifndef " + s + vbLf + "#define " + s +vbLf

	  Application.ActiveDocument.Selection.EndOfDocument
 	  Application.ActiveDocument.Selection = vbLf + "#endif // " + s + vbLf
End Sub

Sub IncludeMyH()
'DESCRIPTION: Inserts an #include "thisFile.h"


      'if name = "ARNE.CPP" then s = "ARNE"
	  s = Application.ActiveDocument.Name
	  p = Len(s)
	  for i = 1 to Len(s)	
	    if Mid(s,i,1)="." then p = i-1
	  next
	  s = Left(s,p)
	  Application.ActiveDocument.Selection = "#include "+Chr(34)+s+".h"+Chr(34)+vbLf
End Sub

Sub GetFriendFile()
'DESCRIPTION: Opens the corresponding .h / .cpp file

	currentFileName = Application.ActiveDocument.FullName
	newFileName = ""
	'MsgBox currentFileName  
	if (Ucase(Right(currentFileName,2))=".H") then
	  newFileName = Left(currentFileName,Len(currentFileName)-2)+".CPP"
	elseif	(Ucase(Right(currentFileName,4))=".CPP") then
	  newFileName = Left(currentFileName,Len(currentFileName)-4)+".H"
	end if

	'MsgBox newFileName  
	if newFileName<>"" then	Application.Documents.Open newFileName

		


End Sub



Sub MakeFilePair()
'DESCRIPTION: Generates a .h / .cpp file pair

  dim newDoc 
  s = InputBox("This macro generates a .h / .cpp file pair."+vbLf+vbLf+"Enter filename (not including extension)","Generate filepair")
  if s<>"" then
    newName = s+".h"
	sTxt = "/*-----------------------------------------------" + vbCrLf
	sTxt = sTxt+"     File name    : " + newName + vbCrLf
	sTxt = sTxt+"     Author       : "+ vbCrLf
	sTxt = sTxt+"  "+ vbCrLf
	sTxt = sTxt+"     Description  : "+ vbCrLf
	sTxt = sTxt+"  -----------------------------------------------*/" + vbCrLf
	set newDoc = Application.Documents.Add("Text")
	newDoc.Save (newName)
	newDoc.Selection = vbCrLf+ "#include "+Chr(34)+"stdafx.h" +Chr(34) + vbCfLf+ vbCrLf
	MakeHFileIfDef()
	newDoc.Selection.StartOfDocument

	newDoc.Selection = sTxt

	newName = s +".cpp"
	set newDoc = Application.Documents.Add("Text")
	newDoc.Save (newName)
	IncludeMyH()
  end if
  
End Sub




Comments

  • My way of doing the same

    Posted by Legacy on 08/01/2003 12:00am

    Originally posted by: Graham Reeds

    ' Create .CPP / .H File Pair
    
    '
    ' Graham 'Mournblade' Reeds, 31/07/2003
    '
    ' Based on work by:-
    ' Per Nilsson (original), Paul Wendt, Oztan Harmanci
    ' Kai Uwe Kroos, Ana Lyser
    '
    ' Macro that creates a .cpp / .h pair
    ' It will prompt for a file name and create accordingly

    ' Significant changes:
    ' - Since my code is stored in a \SRC off my main project path I have had to
    ' fix it so it saves the files with the other source files.
    ' - _CTEST_H is now _CTEST_H_
    '
    ' Todo:
    ' - Add a comment header to each file. Comment should come from an external template file
    '


    ' DESCRIPTION: Generates a .h / .cpp file pair
    Sub CreateFilePair()
    dim newDoc
    s = InputBox("This macro generates a .cpp / .h file pair." + vbLf + vbLf + "Enter filename (not including extension)", "Generate file pair")
    if s <> "" then
    ' get the path of the currently active file
    Application.CurrentDirectory = Application.ActiveDocument.Path

    ' create new header file
    hFile = s + ".h"
    headerFile = Application.CurrentDirectory + "\" + hFile
    ' create a new .h document
    set newDoc = Application.Documents.Add("Text")
    newDoc.Save(headerFile)
    ' Add it to the current project
    ActiveProject.AddFile(headerFile)

    ' Add include guards
    ' if name = "CTEST.H" then s = "_CTEST_H_"
    g = Ucase(hFile)
    p = Len(g)
    for i = 1 to Len(g)
    if Mid(g, i ,1) = "." then
    p = i-1
    end if
    next
    g = "_" + Left(g, p) + "_H_"
    Application.ActiveDocument.Selection.StartOfDocument
    Application.ActiveDocument.Selection = "#ifndef " + g + vbLf + "#define " + g +vbLf
    Application.ActiveDocument.Selection.EndOfDocument
    Application.ActiveDocument.Selection = vbLf + "#endif // " + g + vbLf


    ' create cpp file
    cppFile = s + ".cpp"
    sourceFile = Application.CurrentDirectory + "\" + cppFile
    ' create a new .cpp document
    set newDoc = Application.Documents.Add("Text")
    newDoc.Save(sourceFile)
    ' Add it to the current project
    ActiveProject.AddFile(sourceFile)

    newDoc.Selection = vbCrLf + "#include """ + hFile + """" + vbCrLf + vbCrLf

    end if

    End Sub

    Reply
  • -Auto add to project -Creation in project folder

    Posted by Legacy on 07/02/2003 12:00am

    Originally posted by: Oztan Harmanci

    -automatic addition to project and creation of the files in the correct folder ( folder of the project )
    
    

    I used Paul Wendt's GetProjectPath function since I dont know jack about this language ( basic? )

    Function GetProjectPath(ByVal proj)
    dim szProjectPath
    dim nIndex
    dim szReturn

    szProjectPath = proj.FullName
    nIndex = InStrRev(szProjectPath, "\")

    if (nIndex <> 0) then
    szReturn = Left(szProjectPath, nIndex)
    end if

    GetProjectPath = szReturn
    End Function


    Sub MakeHFileifdef()
    'DESCRIPTION: Generates #ifdef THISHFILE_H ... #endif definitions

    'if name = "ARNE.H" then s = "_ARNE_H"
    s = Ucase(Application.ActiveDocument.Name)
    p = Len(s)
    for i = 1 to Len(s)
    if Mid(s,i,1)="." then p = i-1
    next
    s = "_" + Left(s,p) + "_H"

    Application.ActiveDocument.Selection.StartOfDocument
    Application.ActiveDocument.Selection = "#ifndef " + s + vbLf + "#define " + s +vbLf

    Application.ActiveDocument.Selection.EndOfDocument
    Application.ActiveDocument.Selection = vbLf + "#endif // " + s + vbLf
    End Sub

    Sub IncludeMyH()
    'DESCRIPTION: Inserts an #include "thisFile.h"


    'if name = "ARNE.CPP" then s = "ARNE"
    s = Application.ActiveDocument.Name
    p = Len(s)
    for i = 1 to Len(s)
    if Mid(s,i,1)="." then p = i-1
    next
    s = Left(s,p)
    Application.ActiveDocument.Selection = "#include "+Chr(34)+s+".h"+Chr(34)+vbLf
    End Sub

    Sub GetFriendFile()
    'DESCRIPTION: Opens the corresponding .h / .cpp file

    currentFileName = Application.ActiveDocument.FullName
    newFileName = ""
    'MsgBox currentFileName
    if (Ucase(Right(currentFileName,2))=".H") then
    newFileName = Left(currentFileName,Len(currentFileName)-2)+".CPP"
    elseif (Ucase(Right(currentFileName,4))=".CPP") then
    newFileName = Left(currentFileName,Len(currentFileName)-4)+".H"
    end if

    'MsgBox newFileName
    if newFileName<>"" then Application.Documents.Open newFileName


    End Sub

    Sub MakeFilePair()
    'DESCRIPTION: Generates a .h / .cpp file pair

    dim newDoc
    s = InputBox("This macro generates a .h / .cpp file pair."+vbLf+vbLf+"Enter filename (not including extension)","Generate filepair")
    if s<>"" then
    newName = GetProjectPath ( ActiveProject ) + s+".h"
    sTxt = "/*-----------------------------------------------" + vbCrLf
    sTxt = sTxt+" File name : " + newName + vbCrLf
    sTxt = sTxt+" Author : "+ vbCrLf
    sTxt = sTxt+" "+ vbCrLf
    sTxt = sTxt+" Description : "+ vbCrLf
    sTxt = sTxt+" -----------------------------------------------*/" + vbCrLf
    set newDoc = Application.Documents.Add("Text")
    newDoc.Save (newName)
    ActiveProject.AddFile newName
    newDoc.Selection = vbCrLf+ "#include "+Chr(34)+"stdafx.h" +Chr(34) + vbCfLf+ vbCrLf
    MakeHFileIfDef()
    newDoc.Selection.StartOfDocument

    newDoc.Selection = sTxt


    newName = GetProjectPath ( ActiveProject ) + s +".cpp"
    set newDoc = Application.Documents.Add("Text")
    newDoc.Save (newName)
    IncludeMyH()
    ActiveProject.AddFile newName

    end if

    End Sub

    Reply
  • How do i get dependent file for Project in VC++

    Posted by Legacy on 01/21/2000 12:00am

    Originally posted by: ramesh


    i have requirement of getting dependent files to perticular project through program in VC++(like apllication package WIZARD uses)

    thank you

    Reply
  • Great! Now, how do you auto add files to current project??

    Posted by Legacy on 06/09/1999 12:00am

    Originally posted by: Chuck Baker

    Thanks! I have been looking all over for a macro that
    creates the .h and .cpp files.

    Now, how do you get the macro to automatically insert these
    two new files INTO the current Project? Just like if you
    did an ADD FILES TO PROJECT...

    I really need this functionality to complete my macros!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds