Thursday, October 09, 2008

Visual Basic 6 Support for Microsoft's LOC Counter

Microsoft has released a great free lines-of-code (LOC) counter.  It is available for download here: http://code.msdn.microsoft.com/LOCCounter

The tool, however, doesn't come with VB6 support.  As much as I hate VB6, we still have a ton of it where I work.  Luckily, the tool is extensible and by editing the LineCounters.xml file and creating some regular expressions I was able to get it to work pretty well with VB6.  The LineCounters.xml file is installed in the same directory as the program.  Open it in your favorite text editor and plop in the following lineCounter section (after one of the /lineCounter closing tags of course):

<lineCounter name="VB6">

  <fileExtension>cls</fileExtension>

  <fileExtension>bas</fileExtension>

  <fileExtension>frm</fileExtension>

  <fileExtension>dsr</fileExtension>

  <codeArea name="Blank lines" isCode="false">

    <expression>^\s*$</expression>

  </codeArea>

  <codeArea name="' comments" isCode="false">

    <expression>^\s*'.*$</expression>

  </codeArea>

  <codeArea name="VB6 Class and Form Garbage" isCode="false" multiLine="true">

      <startExpression>^VERSION\s.*$</startExpression>

      <endExpression>^Option Explicit.*$</endExpression>

  </codeArea>

  <codeArea name="VB6 File Attributes" isCode="false">

      <expression>^Attribute\s.*$</expression>

  </codeArea>

  <codeArea name="VB6 Option Explicits" isCode="false">

      <expression>^Option Explicit.*$</expression>

  </codeArea>

  <pspMetricArea name="Class" toCount="true" caseSensitive="false" >

    <pspMetricAreaStartFlag>'(\W)*PSP_METRICS_CLASS_BEGIN</pspMetricAreaStartFlag>

    <pspMetricAreaEndFlag>'(\W)*PSP_METRICS_CLASS_END</pspMetricAreaEndFlag>

    <PspMetricNameFlag>(?&lt;=PSP_METRICS_CLASS_BEGIN(\W)*:(\W)*)(\w)*</PspMetricNameFlag>

    <ObjectType>(?&lt;=&lt;Object_Type&gt;(\W)*:(\W)*)(\w)*</ObjectType>

  </pspMetricArea>

  <pspMetricArea name="Method" toCount="true" caseSensitive="false" >

    <pspMetricAreaStartFlag>'(\W)*PSP_METRICS_METHOD_BEGIN</pspMetricAreaStartFlag>

    <pspMetricAreaEndFlag>'(\W)*PSP_METRICS_METHOD_END</pspMetricAreaEndFlag>

    <PspMetricNameFlag>(?&lt;=PSP_METRICS_METHOD_BEGIN(\W)*:(\W)*)(\w)*</PspMetricNameFlag>

    <ObjectType>(?&lt;=&lt;Object_Type&gt;(\W)*:(\W)*)(\w)*</ObjectType>

  </pspMetricArea>

</lineCounter>

Note that for this to work properly to remove the garbage at the top of a form file the forms must have "Option Explicit" in them.  This worked for me as that has always been a standard.  Hopefully it is useful for someone else.

1 comment:

David said...

Exactly what I needed. Funny, this post is 8 years old and perfectly matched what I needed for some VB6 apps we are converting. Thanks!