Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 10th, 2006, 7:57 AM   #1
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
[Python] Simple Comment Stripper

For C++-style comments.

Being unfamiliar with most Python idioms, this code is almost certainly quite a kludge.


Anyway, here 'tis:

def Confirm(msg):
    while 1:
        try:
            answer = raw_input(msg)
        except ValueError:
            print "ValueError exception encountered! Try again."
            continue
        except:
            print "Error occured! Try again."
            continue
        if answer != "Yes" and answer != "yes" and answer != "YES" and answer != "Y" and answer != "y" and answer != "No" and answer != "NO" and answer != "no" and answer != "n" and answer != "N":
            print "Invalid answer! Try again."
            continue
        break
    if answer == "Yes" or answer == "yes" or answer == "YES" or answer == "Y" or answer == "y":
        answer = 1
    else:
        answer = 0
    return answer

def GetString(msg):
    while 1:
        try:
            return raw_input(msg)
        except ValueError:
            print "ValueError exception encountered! Try again."
            continue
        except:
            print "Error occured! Try again."
            continue
        break
    print "Loop error occured!"
    return None

def ScanAndWrite(file_in, file_out):
    print "File to read from (input file):", file_in
    print "File to write to (output file):", file_out
    if Confirm("Ok? (Y/N)\n") == 0:
        return
    print ""
    fin = open(file_in, "r")
    fout = open(file_out, "w")
    startFlag, writeFlag, endFlag = 0, 0, 0
    temp = ""
    for line in fin:
        iter, startFlag = 0, 0
        if writeFlag == 0:
            for i in line:
                if startFlag == 1:
                    if i == '/':
                        print line[iter-1:]
                        fout.write(line[iter-1:] + '\n')
                    else:
                        startFlag = 0
                if i == '/':
                    startFlag = 1
                iter += 1
        startFlag = 0
        for i in line:
            if startFlag == 1:
                if i == '*':
                    if writeFlag == 0:
                        temp += "/"
                    writeFlag += 1
                else:
                    startFlag = 0
            if writeFlag > 0:
                temp += i
                if endFlag == 1:
                    if i == '/':
                        if writeFlag > 0:
                            writeFlag -= 1
                    else:
                        endFlag = 0
                if i == '*':
                    endFlag = 1
                if writeFlag == 0:
                    print temp + '\n'
                    fout.write(temp + "\n\n")
                    temp = ""
            if i == '/':
                startFlag = 1

def Wait(msg):
    while 1:
        try:
            raw_input(msg)
        except:
            return
        return

def main():
    file_in = GetString("Input file: ")
    if file_in == None:
        Wait("Filename error (file_in). Exiting...")
    while 1:
        file_out = GetString("Output file: ")
        if file_out == None:
            Wait("Filename error (file_out). Exiting...")
        elif file_out == file_in:
            print "Warning: Output file cannot be the same as input file! Try again."
            continue
        break
    print ""
    ScanAndWrite(file_in, file_out)
    Wait("Exiting...")
    return

main()

Took about an hour to make.

Last edited by UnKnown X; Feb 10th, 2006 at 8:07 AM.
UnKnown X is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC