When the company requires you to write a function comment with a unified format, this little thing may help.
Ma2jun@sina.com
Design goals:
1. Automatically extract the function name; 2. Automatic extraction function network (assuming in the header file); 3. Automatically list the parameter list; 4. Automatically extract the return value; 5. Automatically fill in the author and date (author name)
E.g:
/ / -------------------------------------------------------------------------------------------- // Name: cdsaddin :: onConnection // Function: // Parameters: [ipplication * PAPP] --- // [variant_bool bfirsttime] --- // [long dwcookie] --- // [variant_bool * onConnection] - - // returns: STDMETHODIMP --- // author: JM 2002-3-4 // ---------------------------- -------------------- STDMETHODIMP CDSADDIN :: Onnection (Iapplication * PAP, Variant_Bool Bfirsttime, long dwcookie, variant_bool * onconnection)
Design ideas:
1. Use the IDSADDIN interface; 2. Design the various fields in the CCMTBLOCK (Comment Block) maintenance annotation to provide extension flexibility.
achieve:
/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- ---
// cmtblock.h: interface for the ccmtblock class.////
#if! defined (AFX_CMTBLOCK_H__51B4002A_A935_4764_80B5_03103D5E716E__INCluded_) # define afX_cmtblock_h__51b4002a_a935_4764_80b5_03103d5e716e__included_
#if _msc_ver> 1000 # prgma overce # endif //_msc_ver> 1000 # include
Class ccmtblock {public: ccmtblock (); ~ ccmtblock ();
PRIVATE: CSTRING M_SZBEGIN; // Note The first line (HARD CODED) CSTRING M_SZNAME; / / Function Name (Auto) cstract m_szpurpose; // Function (Auto) cstring m_szparam; // Function Parameter list (Auto) CString m_szreturn; // Function Return (CAN CHANGE) CSTRING M_SZAUTHOR; // Function Author (Auto) cstring m_szend; // Note Tail line (HARD CODED)
CString M_SZFUNCDEFINE; / / Function Definition (Automatically Get) // CString M_SZBLOCK; // Function Note Block
Std :: Vector
PRIVATE: VOID EXTRACTNAME (); // Extract Name Void extractpurpurt (); // extraction function void extractParam (); // extraction parameter list void extractreturn (); // extraction return Type Void composeauthorAnddate (); // Editing the author and date time public: void SetFunctionDefine (CString szFuncDefine); // set the function definition (automatically) CString ComposeComment (); // comment edit function void SetPurpose (CString & szFuncPurpose) {m_szPurpose = szFuncPurpose;} // set the function header files Comment
}
#ENDIF /! Defined (AFX_CMTBLOCK_H__51B4002A_A935_4764_80B5_03103D5E716E__included_)
/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- ---
// cmtblock.cpp: Implementation of the ccmtblock class.////
#include "stdafx.h" #include "autocomment.h" #include "cmtblock.h"
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
//// construction / destruction //
Ccmtblock :: ccmtblock () {m_szbegin = _t ("//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------- / r / n "); m_szend = _t (" // -------------------------------------------------------------------------------------------------------------------------------------------- ---------------------- / r / n ");
m_arrSort.clear (); m_arrSort.push_back (& m_szBegin); m_arrSort.push_back (& m_szName); m_arrSort.push_back (& m_szPurpose); m_arrSort.push_back (& m_szParam); m_arrSort.push_back (& m_szReturn); m_arrSort.push_back (& m_szAuthor); m_arrSort. Push_back (& m_szend);
Ccmtblock :: ~ ccmtblock () {m_arrsort.clear ();
void CCmtBlock :: ExtractName () {int iPosSpace = m_szFuncDefine.FindOneOf (_T ( "")); int iPosBracket = m_szFuncDefine.FindOneOf (_T ( "(")); CString szName = m_szFuncDefine.Mid (iPosSpace, iPosBracket-iPosSpace) Szname.trimleft (); szname.trimright (); m_szname = _t ("// Name:") SZNAME _T ("/ r / n");} void ccmtblock :: extractpurpose () {cstring sztemp = m_szpurpose ; M_szpurpose = _t ("// function:") sztemp _t ("/ r / n");}
void CCmtBlock :: ExtractParam () {// get the parameter table int iPosLeftBracket = m_szFuncDefine.FindOneOf (_T ( "(")); int iPosRightBracket = m_szFuncDefine.ReverseFind (_T ( ')')); CString szParamTable = m_szFuncDefine.Mid ( iPOSLTBRACKET 1, IPOSRIGHTBRACKET-iPOSLTBRACKET-1);
// Decomposition parameter // Judgment whether there is parameter SZPARAMTABLE.TRIMLEFT (); szparamtable.trimright (); if (SzparamTable ==_T (")) {// No parameter m_szparam = _t (" // parameter: None / r / N "); return;} m_szparam = _t (" // parameter: "); int ipos1 = 0; int ipos2 = 0; cstract szoneparam = _t ("); cstring szone; int = 0; szparamTable = _T (","); while ((ipos2 = szparamtable.find (_t (','), ipos1))! = -1) {ivuit ;
/ / Find the parameter szoneParam = SZPARAMTABLE.MID (IPOS1, IPOS2-IPOS1); szoneParam.trimleft (); szoneParam.trimright (); if (ilinecount == 1) szone = _t ("[") szoneParam _T (" ] --- "); else szone = _t (" // [") szoneparam _t ("] --- "); szoneline = _t (" / r / n ");
// Add to the function list on m_szparam = szoneline;
IPOS1 = IPOS2 1;}
// :: MessageBox (null, szparamtable, null, mb_ok);
Void ccmtblock :: Extractreturn () {cstring szret = m_szfuncdefine.mid (0, m_szfuncdefine.findoneof (_t ("))); m_szreturn = _t (" // Return: ") Szret _T (" --- / R / N ");
EXTERN CSTRING G_SZAUTHOR; / / Author Name
Void ccmtblock :: composeauthor; = g_szauthor; //_ t ("author");
COLEDATETIME & DATE = COLEDATETIME :: getcurrenttime (); cstring szdate = date.format (var_datevalueonly);
m_szAuthor = _T ( "// Author:") szAuthor _T ( "") szDate _T ( "/ r / n");}
Void ccmtblock :: setFunctionDefine (cstring szfuncdefine) {m_szfuncdefine = SZFUNCDEFINE
CString ccmtblock :: composecomment () {extractName (); // extraction name extractPurpose (); // extraction function extractParam (); // extraction parameter list extractreturn (); // Extract return type composeauthorAnddate (); // Editing author And date time
CSTRING SZBLOCK = _T (""); for (int i = 0; i Return szblock;} // -------------------------------------------- -------------------------------------------------- ------- AutoCommentCommandMethod function is the function that is automatically created by Addin Wizard /// ccommands methods STDMETHODIMP CCOMMANDS :: AutoCommentCommandMethod () {AFX_MANAGE_STATE ()); // TODO: Replace this with the actual code to execute this command // Use m_pApplication to access the Developer Studio Application object, // and VERIFY_OK to see error strings in DEBUG builds of your add-in // (see stdafx.h) // VERIFY_OK (m_pApplication-> EnableModeless (VARIANT_FALSE)); // :: MessageBox (NULL, ". AutoComment Command invoked", "AutoComment", MB_OK | MB_ICONINFORMATION); // VERIFY_OK (m_pApplication-> EnableModeless (VARIANT_TRUE)); // get the current document CComPtr // Get selected object ccomptr // Get function definition (assuming that the cursor is inserted in the function definition) // - The logo looking for: HeadLine, the end ({) >> future implementation // select function defined first line CCOMBSTR BSTRFUNCDEFINE; PSEECTION- > Selectline (); pselection-> get_text (& bstrfuncdefine); // Plug the cursor in the start position Variant var1, var2; variantinit (& var2); variantinit; pselection-> startofline (var1, var2); IF (BStrFuncdefine.Length () == 0) Return E_FAIL; // is not a function definition uses_conversion; cstract szfuncdefine = W2A (bstrfuncdefine); m_cmtblock.setfunctionDefine (Szfuncdefine); / / Find the notes defined in the header file CString & SzfuncComm = FindFunctionPurposeinHeader (SzfuncDefine); m_cmtblock.setpurpose (SZFUNCCMM); // Composition function comment string cstring szblock = m_cmtblock.composecomment (); bstr bstrblock; bstrblock = a2w (szblock); pselection-> put_text (bstrblock); Return S_OK; CString CCommands :: FindFunctionPurposeInHeader (CString & szDefine) {CString szRet; // get the current document CComPtr // open the corresponding .h files CComBSTR bstrFullName; pDoc-> get_FullName (& bstrFullName); CString szFullName (bstrFullName); szFullName.MakeLower (); CString szExt = szFullName.Right (3); if (! SzExt = "cpp") // is not .cpp return ""; szfullname.trimright ("cpp"); szfullname = "h"; // conversion into .h CSTDIOFILE FILE; if (! File.open (szfullname, cfile :: ModeRead)) Return ""; cstring szline; cstring szpurpose; int pos; while (file.readstring (szline)) {// Check if each line is a function name IF (((POS = Szline.find (SZFUNCNAME))! = -1) {// Check if the front and rear characters of SZFUNCNAME are spaces and '(' // Construct a string in front of SZFUNCNAME before and after adding a character cstring sztemp = Szline .Mid (POS-1, SZFUNCNAME.GETLENGTH () 2); if (Sztemp.Left (1)! = "" || (Sztemp.right (1)! = "" && sztemp.right (1)! = "(")) Szret = ""; ELSE {// Find function declaration // View the current line of the current line There is any comment int posplash = 0; if ((possplash = szline.reversefind ('/'))! = -1 ) Szret = szline.right (Szline.getLength () - Possplash - 1); ELSE {// View the next line There is no comment if (file.readstring (szline)) {szline.trimright (); szline.trimright (); IF (szline.Left (1) == "/") {// has a comment SZLINE.trimleft ("//"); szret = Szline;} else szret = ";} } Fig. File.close (); Szret.trimright (); szret.trimleft (); return szret;} / / -------------------------------------------------------------------------------------------- ----------------------------------------------- // Remove the following code in the cdsaddin :: onConnection function, pay attention: The CParamdlg class is a dialog class that input the author name. if (bFirstTime == VARIANT_TRUE) {VERIFY_OK (pApplication-> AddCommandBarButton (dsGlyph, bszCmdName, m_dwCookie)); // first time allows the user to enter the name CParamDlg dlgParam; if (dlgParam.DoModal () == IDOK) {g_szAuthor = dlgParam.m_szAuthor; // into registry :: WriteProfileString ( "DSAddIn_AutoComment", "Author", g_szAuthor);}} else {char szAuthor [255]; :: GetProfileString ( "DSAddIn_AutoComment", "Author", "Author" , Szauthor, SizeOf (Szauthor); g_szauthor = cstring (szauthor);} // ------------------------------- -------------------------------------------------- ---------------- I hope everyone can make valuable comments :-) JM ----- ma2jun@sina.com