Wireshark-dev: [Wireshark-dev] [PATCH] commas after the last entry of an array structure intial

From: Sebastien Tandel <sebastien@xxxxxxxxx>
Date: Wed, 20 Dec 2006 02:22:46 +0100
Hi all,  in fact, Jaap ... :)


   just for the fun ... I wrote quickly a script which get rid of comma
at the end of an array structure initialization. (see in attach)
My first estimation was wrong there are a little bit more than 3600
cases ;) But this script doesn't take into account nested array
structure initialization (which should happen quite rarely).
I ran the script on each dissectors (.c, .h) and I've attached the patch
(really big). (Yes! It compiles again. I tried it. :))

Hope you'll sleep better tonight :-D


P.S. : Ironically, files with largest number of changes are files
generated automatically by ans2wrs.py   :-p


Regards,
Sebastien Tandel
#!/usr/bin/python

######
#
#   This little script will attempt to delete the comma which could be put
#   after the final item of an array structure initialization (in C).
#
#
#   usage : $0 <filename>
#
#####

import re, sys

if (len(sys.argv) < 2):
  print "specify a filename"

sEndStructurePattern=".*};.*"
sCommaPattern=".*,\s*};.*"
sCommaReplacementPattern=",\s*};"
sCommaReplacementString="};"

sEndStructureOnlyPattern="\s*};.*"
sCommaEmptyPattern=".*,\s*$"
sCommaEmptyReplacementPattern=",\s*$"
sCommaEmptyReplacementString="\n"

lineUnchanged=1
blankLines=[]
uLineNumber=0
sPrevLine=""
hFile=open(sys.argv[1])
sLine = hFile.readline()
while (sLine != ''):
  # check if it's the end of a structure (definion or initialization, we can't
  # differentiate here!)
  endStructurePattern = re.match(sEndStructurePattern, sLine)
  if (endStructurePattern != None):
    #check if there are a initialization here on this line
    commaMatch = re.match(sCommaPattern, sLine)
    if (commaMatch != None):
      sLine = re.sub(sCommaReplacementPattern, sCommaReplacementString, sLine)
      lineUnchanged=0
    else:
      # if there is nothing others before the }; then check the previous line
      endStructureOnlyMatch = re.match(sEndStructureOnlyPattern, sLine)
      if (endStructureOnlyMatch != None):
	commaMatch = re.match(sCommaEmptyPattern, sPrevLine)
	if (commaMatch != None):
	  sPrevLine = re.sub(sCommaEmptyReplacementPattern, sCommaEmptyReplacementString, sPrevLine)
	  lineUnchanged=0

  if (sLine.strip() != ''):
    sys.stdout.write(sPrevLine)
    for blank in blankLines:
      sys.stdout.write(blank)
    blankLines=[]
    sPrevLine = sLine
  else:
    blankLines.append(sLine)

  sLine = hFile.readline()
  uLineNumber += 1
sys.stdout.write(sPrevLine)
for blank in blankLines:
  sys.stdout.write(blank)
hFile.close()
sys.exit(lineUnchanged)

Attachment: rev20177-commas-deleted.diff.gz
Description: application/gzip