DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

C++ Style Comment Stripper

Snippets Manager user avatar by
Snippets Manager
·
Feb. 19, 07 · · Code Snippet
Like (0)
Save
Tweet
650 Views

Join the DZone community and get the full member experience.

Join For Free
// This scirpt either removes the C++ style comments or places them
// just before the line in which they occur
// for ex:
//
//      int i=0, sum;    //This is some comment(embedded)
//
// after conversion:
//      //This is some comment(embedded)
//      int i=0, sum;


# comm_stripper
#
# Description :
#
#   This scirpt either removes the C++ style comments or places them
#   just before the line in which they occur
#   for ex:
#
#       int i=0, sum;    //This is some comment(embedded)
#
#   after conversion:
#       //This is some comment(embedded)
#       int i=0, sum;

import re
import os

#comm_re = re.compile(r"(\s*)(.*)(//.*)")

comm_re = re.compile(r"(\s*)(.*?)(//.*)")
ext_re  = re.compile(r"\.(cpp|h|hpp|c|icc)$")
out_ext = ".out"

TRUE  = 1
FALSE = 0

def comm_strip(file_name, keep_comment=TRUE):
    lines = file(file_name).readlines()
    out_lines = []

    out_filename = file_name + out_ext
    fout = open(out_filename, "w")

    for line in lines:
        match = comm_re.match(line.rstrip())

        if(match):
            if keep_comment:
                #print "%s%s" % (match.group(1), match.group(3))
                out_lines.append("%s%s\n" % (match.group(1), match.group(3)))
            if match.group(2):
                #print "%s%s" % (match.group(1), match.group(2).rstrip())
                out_lines.append("%s%s\n" % (match.group(1), match.group(2).rstrip()))
        else:
            #print line.rstrip()
            out_lines.append(line)

    out_lines.append("\n\n")
    fout.writelines(out_lines)
    print "File %s => %s" % (file_name, out_filename)
    print "-"*80

print os.getcwd()
file_names = os.listdir(".")
flag=TRUE

for file_name in file_names:
    match_ext = ext_re.search(file_name)

    if match_ext :
        comm_strip(file_name, flag)

print "Done!"

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Debugging Deadlocks and Race Conditions
  • How to Determine if Microservices Architecture Is Right for Your Business?
  • JUnit 5 Tutorial: Nice and Easy [Video]
  • The Evolution of Configuration Management: IaC vs. GitOps

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo