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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds