<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: comment code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 20 Aug 2008 16:50:46 GMT</pubDate>
    <description>DZone Snippets: comment code</description>
    <item>
      <title>C++ Style Comment Stripper</title>
      <link>http://snippets.dzone.com/posts/show/3546</link>
      <description>// This scirpt either removes the C++ style comments or places them&lt;br /&gt;// just before the line in which they occur&lt;br /&gt;// for ex:&lt;br /&gt;//&lt;br /&gt;//      int i=0, sum;    //This is some comment(embedded)&lt;br /&gt;//&lt;br /&gt;// after conversion:&lt;br /&gt;//      //This is some comment(embedded)&lt;br /&gt;//      int i=0, sum;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# comm_stripper&lt;br /&gt;#&lt;br /&gt;# Description :&lt;br /&gt;#&lt;br /&gt;#   This scirpt either removes the C++ style comments or places them&lt;br /&gt;#   just before the line in which they occur&lt;br /&gt;#   for ex:&lt;br /&gt;#&lt;br /&gt;#       int i=0, sum;    //This is some comment(embedded)&lt;br /&gt;#&lt;br /&gt;#   after conversion:&lt;br /&gt;#       //This is some comment(embedded)&lt;br /&gt;#       int i=0, sum;&lt;br /&gt;&lt;br /&gt;import re&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;#comm_re = re.compile(r"(\s*)(.*)(//.*)")&lt;br /&gt;&lt;br /&gt;comm_re = re.compile(r"(\s*)(.*?)(//.*)")&lt;br /&gt;ext_re  = re.compile(r"\.(cpp|h|hpp|c|icc)$")&lt;br /&gt;out_ext = ".out"&lt;br /&gt;&lt;br /&gt;TRUE  = 1&lt;br /&gt;FALSE = 0&lt;br /&gt;&lt;br /&gt;def comm_strip(file_name, keep_comment=TRUE):&lt;br /&gt;    lines = file(file_name).readlines()&lt;br /&gt;    out_lines = []&lt;br /&gt;&lt;br /&gt;    out_filename = file_name + out_ext&lt;br /&gt;    fout = open(out_filename, "w")&lt;br /&gt;&lt;br /&gt;    for line in lines:&lt;br /&gt;        match = comm_re.match(line.rstrip())&lt;br /&gt;&lt;br /&gt;        if(match):&lt;br /&gt;            if keep_comment:&lt;br /&gt;                #print "%s%s" % (match.group(1), match.group(3))&lt;br /&gt;                out_lines.append("%s%s\n" % (match.group(1), match.group(3)))&lt;br /&gt;            if match.group(2):&lt;br /&gt;                #print "%s%s" % (match.group(1), match.group(2).rstrip())&lt;br /&gt;                out_lines.append("%s%s\n" % (match.group(1), match.group(2).rstrip()))&lt;br /&gt;        else:&lt;br /&gt;            #print line.rstrip()&lt;br /&gt;            out_lines.append(line)&lt;br /&gt;&lt;br /&gt;    out_lines.append("\n\n")&lt;br /&gt;    fout.writelines(out_lines)&lt;br /&gt;    print "File %s =&gt; %s" % (file_name, out_filename)&lt;br /&gt;    print "-"*80&lt;br /&gt;&lt;br /&gt;print os.getcwd()&lt;br /&gt;file_names = os.listdir(".")&lt;br /&gt;flag=TRUE&lt;br /&gt;&lt;br /&gt;for file_name in file_names:&lt;br /&gt;    match_ext = ext_re.search(file_name)&lt;br /&gt;&lt;br /&gt;    if match_ext :&lt;br /&gt;        comm_strip(file_name, flag)&lt;br /&gt;&lt;br /&gt;print "Done!"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 19 Feb 2007 14:27:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3546</guid>
      <author>abhiwin (Abhi Win)</author>
    </item>
  </channel>
</rss>
