Comment Function Macro for C/C++

I have looked at some of the other function comment macros and unfortunately, in my opinion, each one was lacking in some important area. Here are some of the features that I needed that I found missing in the other macros:
  • return pointer type with spaces befor "*";
  • templates in function parameters
  • undefined return
  • split function declaration in a few lines
One thing I will add is that my macros does not handle stdcall/cdecl and another modifiers yet.

Visual Studio Macro

Sub mcFunctionComments()
'DESCRIPTION: Function comments template
'Author : Y.Marchenko. Date : 23/09/2000

 author="Y.Marchenko"
 Delimeter="::"	 ' class delimeters
 a_char=asc("=") ' character used in adornment
 n_char=60       ' amount of adornment characters used 
 Tab3=vbTab&vbTab&vbTab	'indent 3 tabs
 Header = Trim(ActiveDocument.Selection)
 if len(Header)=0 then
  MsgBox("Select function declaration") 
  Exit Sub 
 end if

 ActiveDocument.Selection.StartOfLine
 ActiveDocument.Selection.NewLine
 ActiveDocument.Selection.LineUp
 if ActiveDocument.Language <>"C/C++" then
  ActiveDocument.Selection=ActiveDocument.Language
  MsgBox("Wrong file type")
 else
  'Delete all comments from function declaration
  Loc=InStr(Header,"/*")
  while Loc
   s1=Left(Header,Loc-1)
   s2=Mid(Header,InStr(Header,"*/")+2)
   Header=s1&s2
   Loc=InStr(Header,"/*")
  wend

  Loc = InStr(Header, "(")
  f_title=Trim(Left(Header,Loc-1 ))
  f_pars=Trim(Mid(Header,Loc+1,InStr(Header, ")")-Loc-1))

  ActiveDocument.Selection = "/*" + String(n_char,a_char) 
                           & "*\" & vbNewLine

  'Get the function return type.
  arrStr=Split(f_title,Delimeter)
  UB=UBound(arrStr)

  For i=0 To UB
   arrStr(i)=Trim(arrStr(i))
  Next

  Loc=InStrRev(arrStr(0)," " )
  Loc1=InStrRev(arrStr(0),"*" )
  if Loc1>Loc then Loc=Loc1	' pointer returned
  f_name=Mid(arrStr(0),Loc+1)
  f_return=Trim(Left(arrStr(0),Loc))

  if Len(f_return) = 0 then	' undefined return type
   f_return="int (by default)"

   if UB > 0 then
    arrStr(UB)=Replace(arrStr(UB)," ","")
    arrStr(UB)=Replace(arrStr(UB),vbTab,"")

    if arrStr(UB-1)=arrStr(UB)then
     f_return="constructor"
    elseif arrStr(UB) = "~" & arrStr(UB-1) then
     f_return="destructor"
    end if

   end if

  end if

  For i=1 To UBound(arrStr)
   f_name=f_name & Delimeter & Trim(arrStr(i))
  Next

  ActiveDocument.Selection = vbTab & "Author :" 
                           & author&Tab3 
                           & "Date : " 
                           & Date&Tab3 
                           & "version 1.0" 
                           & vbNewLine

  ActiveDocument.Selection = "  " 
                           & String(n_char,a_char) 
                           & vbNewLine

  ActiveDocument.Selection = vbTab 
                           & "Function :" 
                           & Tab3 
                           & f_name 
                           & vbNewLine

  ActiveDocument.Selection = vbTab 
                           & "Description :" 
                           & vbTab 
                           & vbNewLine

  ActiveDocument.Selection = vbTab 
                           & "Return :" 
                           & Tab3 
                           & f_return 
                           & vbTab 
                           & "-" 
                           & vbTab 
                           & vbNewLine

  'Get the function parameters.
  ActiveDocument.Selection = vbTab & "Parameters :" & vbNewLine
  arrStr=Split(f_pars,",")
  bTmpl=false	' template is the current parameter
  
  For i=0 To UBound(arrStr)
   arrStr(i)=Trim(arrStr(i))
   Loc = InStr(arrStr(i), "<")
   if Loc then bTmpl = true
   if bTmpl then
    if Loc = 0 then
     arrStr(i)=arrStr(i-1) & "," & arrStr(i)
     Loc = InStr(arrStr(i), ">")
     if Loc then ' template end
      bTmpl=false

      ActiveDocument.Selection = Tab3&Replace(arrStr(i),vbNewLine,"") 
                               & vbTab 
                               & "-" 
                               & vbTab&vbNewLine
     end if
    end if
   else
    ActiveDocument.Selection = Tab3&Replace(arrStr(i),vbNewLine,"") 
                             & vbTab 
                             & "-" 
                             & vbTab&vbNewLine
   end if
  Next
  ActiveDocument.Selection = vbTab & "Note :" & vbNewLine
  ActiveDocument.Selection = "\*" + String(n_char,a_char) & "*/"
 end if
End Sub


Comments

  • Online Stopwatch Online Stopwatch Online Stopwatch

    Posted by Woficowncix on 03/23/2013 07:01am

    A Freehttp://www.online-stopwatch.com/ flash online stopwatch, quick easy to use stopwatch! http://www.online-stopwatch.com/also an online countdown! A Free flash online stopwatch, quick easyhttp://www.online-stopwatch.com/ to use stopwatch! also an online countdown! A Free flash online stopwatch, quick easy to use stopwatch! also an online countdown! http://www.online-stopwatch.com/ stopwatch, online stopwatch, online countdown, free online stopwatch, free flash stopwatch, large stopwatch, stopwatch timer, online countdown timer, free countdown timer stopwatch, http://www.online-stopwatch.com/online stopwatch, online countdown, free online stopwatch, http://www.online-stopwatch.com/free flash stopwatch, large stopwatch, stopwatch timer, online countdown timer, free countdown timer stopwatch, online stopwatch, online countdown, free online http://www.online-stopwatch.com/ stopwatch, free flash stopwatch, large stopwatch, stopwatch timer, online countdown timer, free countdown timer

    Reply
  • Works fine

    Posted by Legacy on 05/04/2002 12:00am

    Originally posted by: Baelwrath

    Copied, rejoined the lines and it works fine.

    Remember to edit the author variable and I reduced the tabs and
    increased the number of ='s in the boxes, but thats just me!

    Good work.

    Regards

    Reply
  • Try Failed

    Posted by Legacy on 10/24/2001 12:00am

    Originally posted by: zimin cai

    I have use it but, the only thing it do is that adding a blank line. and before this LOC = Instr(..) bring a error, I comment it. So, I want to know how to use.

    Reply
  • How to use it ...

    Posted by Legacy on 04/29/2001 12:00am

    Originally posted by: Yevgeniy Marchenko

    Initialy the macro was written correctly.
    But when it was publiced here the long strings were devided in short. After that the macro does not work.
    If you want to use this macro you need assemble all splited string.
    For example:
    ActiveDocument.Selection = Tab3&
    Replace(arrStr(i),vbNewLine,"")
    & vbTab
    & "-"
    & vbTab&vbNewLine
    MUST be:
    ActiveDocument.Selection = Tab3&Replace(arrStr(i),vbNewLine,"") & vbTab & "-" & vbTab&vbNewLine

    and so on...

    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 …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds