Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Strip out comments from code
Message
General information
Forum:
Visual FoxPro
Category:
FoxPro 2.x
Environment versions
Visual FoxPro:
FoxPro Dos
Miscellaneous
Thread ID:
01332371
Message ID:
01332406
Views:
10
This message has been marked as the solution to the initial question of the thread.
>Hi all
>
>Just had a crazy day so I have a crazy requirement. I want to strip out all comments from my code and also tabs (indentations) and probably even blank lines between codes.
>
>I can always write a prg but if one with something similar is already there I would like to make use of it.
>
>Thanks.

For removing comments, I've done something like this long time ago, I just googled for it for I could not remember where I have the code but remembered someone asked this question some time ago in the newsgroups, so this was my answer, it uses some VFP functions, like local or strtofile, but they are easy to replace:

(If you save as c:\temp\removecmments.prg it will remove the comments of itself, the comments are silly but, as I said in the post, I put them there to test the program <g>)
#define _TAB Chr(9)
#define _CR Chr(13)
#define _LF Chr(10)
#define _DA '&' + '&'

Strtofile(RemoveComments('c:\temp\removecomments.prg'), 'c:\temp\test.prg')

function RemoveComments
lparameters tcProgram

* This program will remove all lines that starts with a *, for they are comments ;
but it will also remove all the comments continuation, like this oneline as well ;
as all the remaining text after the && comments

* tcProgram is a mandatory parameter
if Vartype(tcProgram) # 'C' or Empty(tcProgram) or not File(tcProgram)
    return .f.
endif

local lcProgramText, i, lnMemoWidth, llCont, llRemove, lnPos, lcNewProgramText, llWasComment, llCommentCont

lnMemoWidth            = Set("Memowidth")                                
        && To restore previous setting
lcProgramText        = Filetostr(tcProgram)                            
        && Store the contents of tcProgram into lcProgramText
lcNewProgramText    = ''                                                
            && Modified text
llCommentCont        = .f.                                            
                && Flag to follow comments continuation
llWasComment        = .f.                                                
            && Flag to indicate the line was a comment
llCont                = .f.                                            
                && Flag to indicate that this line is continuation of the previous one

set memowidth to 8192                                                    
        && MemLines and MLine work with the current memo width set

for i = 1 to Memlines(lcProgramText)
    lcRawLine            = Mline(lcProgramText, i)                    
        && Original text
    lcLine                = Alltrim(Strtran(lcRawLine, _TAB))            
    && Trimmed and _TAB stripped text
    lnPos                    = At(_DA, lcRawLine)                        
            && Position of double-ampersand comments

    do case
        case llCommentCont or (Left(lcLine, 1) = '*'    and !llCont) && * comment start or continues
            lcNewLine            = ''                                    
            && Not really necessary, it can be handled with llRemove
            llWasComment        = .T.                                    
            && Next line is a comment too!
            llRemove                = .T.                                
                && To avoid adding unnecessary empty lines
        case lnPos > 1                                                
                && There is a double-ampersand comment
            lcNewLine            = Left(lcRawLine, lnPos - 1)            
    && Remove it!
            llWasComment        = .T.                                    
            && This is to handle ;
            double ampersand comments that spans ;
            more than one line ;
            although they are not pretty!
        otherwise
            llWasComment        = .F.                                    
            && Regular line of code
            lcNewLine            = lcRawLine                            
            && Just copy it
    endcase
    lcNewProgramText        = lcNewProgramText + Iif(llRemove, '',Iif(Empty(lcNewProgramText), '', _CR) + lcNewLine)
    llRemove                    = .F.
    llCont                    = Right(lcLine, 1) = ';'                
        && Next line is a continuation of this one
    llCommentCont            = llCont and llWasComment                
        && Next line is also a comment

    * This should handle lines starting with a * but as a result of an large formula, like:

    lnUnrelevantValue        = (i + 1) ;
                                    * 5                                
                && in here * 5 is definetely not a comment!
next i

set memowidth to (lnMemoWidth)                                        
        && Restore the original Memowidth

return lcNewProgramText                                                
            && The comment-stripped version of the program 
This is where I found my code, I think the OP made some changes to it that might be useful for you Newsgroup post
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform