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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds