Find switch operators in files

by alex 2. January 2012 09:04

import os

import sys

 

def processFile(fileName):

    f = open(fileName)

 

    switchFound = False

    foundOneBracket = False

    openBrackets = 0

    switchLineCount = 0

    lineCounter = 1

    switchStartedAt = 0

    printedFileName = False

 

    for line in f.readlines():

 

            if line.find("switch") > 0:

                switchLineCount = 0

                switchFound = True

                foundOneBracket = False

                switchStartedAt = lineCounter

 

            # now we need to count brackets

            if switchFound :

                if line.find("{") > 0:

                    openBrackets += 1

                    foundOneBracket = True

 

                if line.find("}") > 0 :

                    openBrackets -= 1

 

            switchLineCount += 1

 

            if switchFound and foundOneBracket and openBrackets == 0:

                if not printedFileName :

                    print fileName + ":"

                    printedFileName = True

 

                switchFound = False

                print  "switch: %d size %d" % (switchStartedAt, switchLineCount)

 

            lineCounter += 1

 

    pass

 

def processFolder(folderName):

    for root, dirs, files in os.walk(folderName):

        for name in files:

            filename = os.path.join(root, name)

            if filename.find(".svn") == -1 and filename[-2:] in ( ".m") :

                processFile(filename)

    pass

 

processFolder("/Users/alex/test/IPHTWO/")

Currently rated 1.7 by 29 people

  • Currently 1.689655/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General | General | General | General

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen