WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Remove Comments from code

 Keywords:  

Question:

We're going to be undergoing a security audit that includes reviewing our coding policies and we were told by a QSA consultant that it is bad practice to have commented code in a production environment. He said it just gives information to someone who breaks in and gets hold of the code - which makes sense. He said the correct way of doing it is to make a production copy in which all comments are removed. Now, I don't know about the rest of you, but I add lots of comments to my code and it would take forever to have to remove them all before moving to a production environment. Has anyone heard of this before? Has anyone attempted to do this with Winbatch?

Answer:

;==========================================================================================================================================
; Name:
;    StripAndMergeComments.wbt
;
; Purpose:
;    Remove comments from WinBatch source code file.
;    Create related comment list file.
;    Create new anonymous comment-less wbt code file for production environment.
;    Merge previously stripped comments with production code file giving back complete source code file.
;
; Note:
;    This feasibility study contains no error- or consistency-checking.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Author:
;    Les Ferch.20090410.
; Modified and additional work:
;    Detlev Dalitz.20090411.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; This theme is based on:
;
; Topic: Remove comments from code
; Conf:  WinBatch
; From:  dblaze1
; Date:  Friday, April 10, 2009 01:14 PM
;
; We're going to be undergoing a security audit that includes reviewing our coding policies and
; ... we were told by a QSA consultant that it is bad practice to have commented code in a production environment.
; ... He said it just gives information to someone who breaks in and gets hold of the code - which makes sense.
; ... He said the correct way of doing it is to make a production copy in which all comments are removed.
; ... Now, I don't know about the rest of you, but I add lots of comments to my code and it would take forever
;     to have to remove them all before moving to a production environment.
; ... Has anyone heard of this before?
; ... Has anyone attempted to do this with Winbatch?
; Thanks,
; Doug
;==========================================================================================================================================


GoSub Define_Functions


;Test.
blnResult = DirChange (DirScript ())
strThisScript = IntControl (1004, 0, 0, 0, 0)

strWbtFile     = strThisScript
strProdFile    = ItemReplace (`prod.wbt`   , -1, strThisScript, `.`)
strCommentFile = ItemReplace (`comment.txt`, -1, strThisScript, `.`)
strMergedFile  = ItemReplace (`merged.wbt` , -1, strThisScript, `.`)


:Step1 ; Remove comments.
udfRemoveWBTComments (strWbtFile, strProdFile, strCommentFile)

:Step2 ; Restore comments.
udfRestoreWBTComments (strMergedFile, strCommentFile)

:Step3 ; Display all involved files.
strWBStudio = DirHome () : `WinBatch Studio.exe`
Run (strWBStudio, `"` : strProdFile    : `"`)
Run (strWBStudio, `"` : strCommentFile : `"`)
Run (strWBStudio, `"` : strMergedFile  : `"`)

Exit


;==========================================================================================================================================
:Define_Functions
;==========================================================================================================================================
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStripComment (strLine)
y = 0
z = 0
strQuoteChars = `'` : `"` : "`"
While @TRUE
   x = StrScan (strLine, strQuoteChars, y + 1, @FWDSCAN)
   If x == 0 Then Break
   strQuoteChar = StrSub (strLine, x, 1)
   z = y
   y = StrIndex (strLine, strQuoteChar, x + 1, @FWDSCAN)
   If y == 0
      y = z
      Break
   EndIf
EndWhile
x = StrIndex (strLine, `;`, y + 1, @FWDSCAN)
strComment = ``
If x > 0
   strComment = StrSub (strLine, x, -1)
   strLine = StrSub (strLine, 1, x - 1)
EndIf
Return Arrayize (strLine : @LF : strComment, @LF)
;..........................................................................................................................................
; This function is called by the main function to remove comments on same line as code.
;
; Les Ferch.20090410.
; Detlev Dalitz.????????.20090411.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfRemoveWBTComments (strWbtFile, strProdFile, strCommentFile)
arrData = ArrayFileGet (strWbtFile)
intDataDim = ArrInfo (arrData, 1)
intDataMax = intDataDim - 1
arrNew = ArrDimension (intDataDim)
arrComment = ArrDimension (intDataDim + 1)
intNumberFmt = StrLen (intDataDim)
intCommentNr = 0
For intElem = 0 To intDataMax
   strLine = arrData [intElem]
   intLineNr = intElem + 1
   ; Test: This comment is indented.
   If StrSub (StrTrim (strLine), 1, 1) == `;`
      intCommentNr = intCommentNr + 1
      strCommentFmt = StrFixLeft (intCommentNr, `0`, intNumberFmt)
      strLineNrFmt = StrFixLeft (intLineNr, `0`, intNumberFmt)
      strCSVString = `"` : StrReplace (StrTrim (strLine), `"`, `""`) : `"` ; Make ready for CSV.
      arrComment [intElem] = strLineNrFmt : `:` : strCommentFmt : `:`: strCSVString
      arrNew [intElem] = StrFill (` `, StrIndex (strLine, `;`, 1, @FWDSCAN) - 1) : `; #` : strCommentFmt
   Else
      arrTmp = udfStripComment (strLine)
      arrNew [intElem] = arrTmp [0]
      If arrTmp [1] != ``
         intCommentNr = intCommentNr + 1
         strCommentFmt = StrFixLeft (intCommentNr, `0`, intNumberFmt)
         strLineNrFmt = StrFixLeft (intLineNr, `0`, intNumberFmt)
         strCSVString = `"` : StrReplace (arrTmp [1], `"`, `""`) : `"` ; Make ready for CSV.
         arrComment [intElem] = strLineNrFmt : `:` : strCommentFmt : `:` : strCSVString
         arrNew [intElem] = arrTmp [0] : `; #` : strCommentFmt
      EndIf
   EndIf
Next
strCSVString = `"` : StrReplace (strProdFile, `"`, `""`) : `"` ; Make ready for CSV.
arrComment [intDataMax + 1] = intDataDim : `:` : strCommentFmt : `:` : strCSVString
intBytesWritten = ArrayFilePut (strCommentFile, arrComment)
intBytesWritten = ArrayFilePut (strProdFile, arrNew)

; Delete empty lines from comment file.
strData = FileGet (strCommentFile)
strSearch = @CRLF : @CRLF
While !!StrIndex (strData, strSearch, 1, @FWDSCAN)
   strData = StrReplace (strData, strSearch, @CRLF)
EndWhile
intBytesWritten = FilePut (strCommentFile, strData) ; Test: This comment is inline.
;..........................................................................................................................................
; Call this function to remove comments from WBT source.
;
; Les Ferch.20090410.
; Detlev Dalitz.20090411.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfRestoreWBTComments (strMergedFile, strCommentFile)
arrComment = ArrayFileGetCSV (strCommentFile, 0, `:`)
intCommentDim = ArrInfo (arrComment, 1)
intCommentMax = intCommentDim - 1

strWbtFile = arrComment [intCommentMax, 2]
intCommentMax = intCommentMax - 1

arrData = ArrayFileGet (strWbtFile)
intDataDim = ArrInfo (arrData, 1)
intDataMax = intDataDim - 1

For intElem = 0 To intCommentMax
   intLineNr      = arrComment [intElem, 0]
   intCommentNr   = arrComment [intElem, 1]
   intCommentText = arrComment [intElem, 2]

   strTemp = arrData [intLineNr - 1]
   strTemp = StrReplace (strTemp, `; #` : intCommentNr, intCommentText)
   arrData [intLineNr - 1] = strTemp
Next

intBytesWritten = ArrayFilePut (strMergedFile, arrData)
;..........................................................................................................................................
; Call this function to restore comments from the comment ressource CSV file.
;
; Detlev Dalitz.20090411.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
;==========================================================================================================================================
Return ; from GoSub Define_Functions
;==========================================================================================================================================


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Reduced example of an anonymous comment-less wbt production file.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;   ; #001
;   ; #002
;   ; #003
;   ; #004
;   ; #005
;   ; #006
;   ; #007
;   ; #008
;   ; #009
;
;   ; #010
;   #DefineFunction StripComment (strLine)
;   ...
;   #EndFunction
;
;   ; #011
;   #DefineFunction RemoveWBTComments (strWbtFile, strNewFile, strCommentFile)
;   ...
;      intLineNr = intElem + 1
;      ; #012
;      If StrSub (StrTrim (strLine), 1, 1) == `;`
;   ...
;   intBytesWritten = ArrayFilePut (strNewFile, arrNew)
;
;   ; #013
;   strData = FileGet (strCommentFile)
;   ...
;   #EndFunction
;
;   ; #014
;   blnResult = DirChange (DirScript ())
;   strThisScript = IntControl (1004, 0, 0, 0, 0)  ; #015
;   ...
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; CSV-Format of the related comment file.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Comment entries:
; Col-1 = Line number within code file.
; Col-2 = Comment number.
; Col-3 = Comment text.
;
; Footer line:
; Col-1 = Number of lines in production file.
; Col-2 = Number of comment replacements.
; Col-3 = Filepath to the corresponding production file.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;   001:001:";~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
;   002:002:"; Name: StripComments.wbt"
;   003:003:";          Create new anonymous comment-less wbt code file."
;   005:005:";          Create related comment list file."
;   006:006:";~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
;   007:007:"; Author: Les Ferch."
;   008:008:"; Modified by: Detlev Dalitz.20090411."
;   009:009:";~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
;   011:010:"; This function is called by the main function to remove comments on same Line as code."
;   036:011:"; Call this function to remove comments from WBT source."
;   048:012:"; Test: This comment is indented."
;   070:013:"; Delete empty lines from comment file."
;   079:014:"; Test."
;   081:015:"; Test: This comment is inline."
;   119:015:"D:\WINBATCH\TEST\2009\20090410.Test.Remove comments from code.prod.wbt"
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Article ID:   W18250
Filename:   Remove Comments from code.txt
File Created: 2012:11:26:09:56:00
Last Updated: 2012:11:26:09:56:00