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

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds