使用Excel VBA宏在.h文件中排除已注释的声明

huangapple go评论62阅读模式
英文:

Excluding commented declarations in an .h file using Excel VBA macro

问题

Excel VBA,我在使用Excel宏函数时遇到了问题

` StrComp(FileLine, "*/")`

该代码的目的是在.h文件中查找重复项。

我想要排除所有被注释的声明,所以我添加了这段代码:

VarIsAComment = StrComp(FileLine, "//#define", vbTextCompare)

它工作得很好,但当我尝试查找带有"/*"的注释时问题就开始了...
我尝试了这段代码:

While Not EOF(1)
    Line Input #1, FileLine
    CurLineNum = CurLineNum + 1
    If (StrComp(FileLine, "#define", vbBinaryCompare)) Then
    
        VarIsAComment = StrComp(FileLine, "//#define", vbTextCompare) '运行正常
        If (StrComp(FileLine, "/*")) Then CommentStarted = 1 '不起作用
        If (StrComp(FileLine, "*/")) Then CommentStarted = 0 

问题是,如果FileLine像这样:

#define mBlk    bytem[12]/*comment*/ 

它运行得很好,但当FileLine只是/*时,CommentStarted保持为false,然后仅在下一行时变为true,然后在同一个循环中再次变为false。

有任何想法吗?

谢谢

英文:

Excel VBA, I'm having a problem with an excel macro with function

` StrComp(FileLine, "*/")`

The purpose of the code is to find duplicates in an .h file.

And I want to exclude all commented declarations, so I've added this piece of code:

VarIsAComment = StrComp(FileLine, "//#define", vbTextCompare)

and it's working fine, but the problem starts when I try to find comment with "/*"..
I've tryied this code:

While Not EOF(1)
            Line Input #1, FileLine
            CurLineNum = CurLineNum + 1
            If (StrComp(FileLine, "#define", vbBinaryCompare)) Then
            
                VarIsAComment = StrComp(FileLine, "//#define", vbTextCompare) //Woks fine
                If (StrComp(FileLine, "/*")) Then CommentStarted = 1 //not working
                If (StrComp(FileLine, "*/")) Then CommentStarted = 0 

So the problem is that if FileLine is something like this:

#define mBlk	bytem[12]/*comment*/ 

it works fine, but when FileLine is only /* CommentStarted stays to false and then goes to true only on the next line and then returns to false in the same cycle.

Any idea?

Thanks

答案1

得分: 1

如评论中所写,您似乎误解了StrComp的工作原理。您的语句StrComp(FileLine, "#define", vbBinaryCompare将始终返回1或-1,除非该行恰好是"#define"。1和-1这两个值都被解释为True(在VBA中,与大多数语言一样,0被转换为False,而其他任何值都被转换为True)。

同样,VarIsAComment = StrComp(FileLine, "//#define", vbTextCompare)将始终为True,除非该行恰好是"//#define"

您想要的是检查:

o 行是否以#define开头:

If FileLine Like "#define*" Then

o 行是否包含#define

If InStr(FileLine, "#define") > 0 Then

o 摆脱//注释:

Dim p As Long
p = InStr(FileLine, "//")
If p > 0 Then
   If p > 1 Then FileLine = Left(FileLine, p-1) Else FileLine = ""
End If

o 摆脱/*注释并检查是否开始(或结束)注释块:

Dim p1 As Long, p2 As Long
p1 = InStr(FileLine, "/*")
p2 = InStr(FileLine, "*/")
If p1 > 0 Then
    If p1 > 1 Then FileLine = Left(FileLine, p1-1) Else FileLine = ""
    CommentStarted = True
End If
If p2 > p1 Then CommentStarted = False
英文:

As written in the comments, you seem to misunderstand how StrComp works. Your statement StrComp(FileLine, "#define", vbBinaryCompare will always return either 1 or -1, except if the line is exactly "#define". Both values 1 and -1 are interpreted as True (in VBA, as in most languages, 0 is converted to False and every other value is converted to True).

Similarly, VarIsAComment = StrComp(FileLine, "//#define", vbTextCompare) will always be True except if the line is exactly "//#define".

What you want is to check

o If line starts with #define:

If FileLine like "#define*" Then

o If line contains #define:

If InStr(FileLine, "#define") > 0 Then

o Get rid of the // comment:

Dim p  As Long
p = InStr(FileLine, "//")
If p > 0 Then 
   If p > 1 Then FileLine = Left(FileLine, p-1) Else FileLine = ""
End If

o Get rid of /* comments and check if we start (or end) a comment block

Dim p1 As Long, p2  As Long
p1 = InStr(FileLine, "/*")
p2 = InStr(FileLine, "*/")
If p1 > 0 Then 
    If p1 > 1 Then FileLine = Left(FileLine, p1-1) Else FileLine = ""
    CommentStarted = True
End If
If p2 > p1 Then CommentStarted = False

huangapple
  • 本文由 发表于 2023年5月25日 15:32:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定