From 05e5c237566142c7fcdfefb115aec69a5c7eca48 Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Wed, 22 Mar 2023 08:05:10 -0600 Subject: [PATCH] comments comments comments --- PA004/CleanFile.java | 97 +++++++++++++++++++++++++++++--------------- PA004/clean_file.pl | 51 ++++++++++++++++------- PA004/clean_file.py | 30 ++++++++++++++ 3 files changed, 130 insertions(+), 48 deletions(-) diff --git a/PA004/CleanFile.java b/PA004/CleanFile.java index 068c375..16e91f1 100644 --- a/PA004/CleanFile.java +++ b/PA004/CleanFile.java @@ -4,42 +4,73 @@ import java.nio.file.Paths; import java.util.regex.Pattern; import java.util.regex.Matcher; +/** + * Ziad Arafat + * 03/22/2023 + * + * This program will clean a corrupted file based on a specific pattern of + * Corrupted data + */ + public class CleanFile { - private static final String CORRUPTION_REGEX = "\\x03(?:\\x03|(?:(?![\\x0A\\x02]).)*\\x0A)*?(?:(?![\\x0A\\x02]).)*\\x02"; + // Regex pattern to match the corruption in the input text + private static final String CORRUPTION_REGEX = "\\x03(?:\\x03|(?:(?![\\x0A\\x02]).)*\\x0A)*?(?:(?![\\x0A\\x02]).)*\\x02"; - public static void main(String[] args) { - if (args.length < 1) { - System.out.println("Usage: CleanFile []"); - return; + public static void main(String[] args) { + + // Check the number of arguments provided + if (args.length < 1) { + System.out.println("Usage: CleanFile []"); + return; + } + + // Store input and output file paths + String inputFilePath = args[0]; + String outputFilePath = args.length > 1 ? args[1] : null; + + String dirtyText; + // Read the content of the input file + try { + dirtyText = new String( + Files.readAllBytes(Paths.get(inputFilePath)) + ); + } catch (IOException e) { + System.out.println("Error reading input file: " + + e.getMessage() + ); + return; + } + + // Clean the input text + String sanitizedText = sanitizedString(dirtyText); + + // Write the cleaned text to the output file if provided, otherwise print it + if (outputFilePath != null) { + try { + Files.write(Paths.get(outputFilePath), + sanitizedText.getBytes()); + } catch (IOException e) { + System.out.println("Error writing output file: " + + e.getMessage() + ); + } + } else { + System.out.print(sanitizedText); + } } - String inputFilePath = args[0]; - String outputFilePath = args.length > 1 ? args[1] : null; - - String dirtyText; - try { - dirtyText = new String(Files.readAllBytes(Paths.get(inputFilePath))); - } catch (IOException e) { - System.out.println("Error reading input file: " + e.getMessage()); - return; + /** + * Remove the corrupted parts from the input text based on the + * CORRUPTION_REGEX pattern. + * + * @param dirtyString The input text containing the corrupted parts. + * @return The cleaned text with the corrupted parts removed. + */ + private static String sanitizedString(String dirtyString) { + Pattern pattern = Pattern.compile(CORRUPTION_REGEX, + Pattern.DOTALL + ); + Matcher matcher = pattern.matcher(dirtyString); + return matcher.replaceAll(""); } - - String sanitizedText = sanitizedString(dirtyText); - - if (outputFilePath != null) { - try { - Files.write(Paths.get(outputFilePath), sanitizedText.getBytes()); - } catch (IOException e) { - System.out.println("Error writing output file: " + e.getMessage()); - } - } else { - System.out.print(sanitizedText); - } - } - - private static String sanitizedString(String dirtyString) { - Pattern pattern = Pattern.compile(CORRUPTION_REGEX, Pattern.DOTALL); - Matcher matcher = pattern.matcher(dirtyString); - return matcher.replaceAll(""); - } } diff --git a/PA004/clean_file.pl b/PA004/clean_file.pl index 06dcaf1..052f241 100644 --- a/PA004/clean_file.pl +++ b/PA004/clean_file.pl @@ -5,35 +5,56 @@ use warnings; use Getopt::Long qw(GetOptions); use File::Slurp; -# Constants -my $corruption_regex = qr/\x03(?:\x03|(?:(?![\x0A\x02]).)*\x0A)*?(?:(?![\x0A\x02]).)*\x02/s; +=pod +Ziad Arafat +03/22/2023 +This program will clean a corrupted file based on a specific pattern of +Corrupted data +=cut + +# Constants +my $corruption_regex = + qr/\x03(?:\x03|(?:(?![\x0A\x02]).)*\x0A)*?(?:(?![\x0A\x02]).)*\x02/s; + +# Function to sanitize the input string by removing the corrupted parts sub sanitized_string { - my ($dirty_string) = @_; - my $cleaned_string = $dirty_string; - $cleaned_string =~ s/$corruption_regex//g; - return $cleaned_string; + my ($dirty_string) = @_; + my $cleaned_string = $dirty_string; + $cleaned_string =~ s/$corruption_regex//g; + return $cleaned_string; } # MAIN + +# Variables for input and output file paths my $input_file_path; my $output_file_path; +# Parse command line options +`` my $result = GetOptions( - 'input_file_path=s' => \$input_file_path, - 'output_file_path=s' => \$output_file_path, + 'input_file_path=s' => \$input_file_path, + 'output_file_path=s' => \$output_file_path, ); # Print usage help text if not enough arguments are provided -unless ($result && $input_file_path) { - print "Usage: $0 --input_file_path INPUT_FILE_PATH [--output_file_path OUTPUT_FILE_PATH]\n"; - exit 1; +unless ( $result && $input_file_path ) { + print +"Usage: $0 --input_file_path INPUT_FILE_PATH [--output_file_path OUTPUT_FILE_PATH]\n"; + exit 1; } +# Read the content of the input file my $dirty_text = read_file($input_file_path); + +# Sanitize the input text my $sanitized_text = sanitized_string($dirty_text); -if (defined $output_file_path) { - write_file($output_file_path, $sanitized_text); -} else { - print $sanitized_text; + +# Write the sanitized text to the output file if provided, otherwise print it. +if ( defined $output_file_path ) { + write_file( $output_file_path, $sanitized_text ); +} +else { + print $sanitized_text; } diff --git a/PA004/clean_file.py b/PA004/clean_file.py index 2d5f283..deef938 100755 --- a/PA004/clean_file.py +++ b/PA004/clean_file.py @@ -1,14 +1,35 @@ #!/usr/bin/python3 +""" +Ziad Arafat +03/22/2023 + +This program will clean a corrupted file based on a specific pattern of +Corrupted data + +""" + import argparse import re # Constants + +# Regex pattern to match the corruption in the input text CORRUPTION_REGEX = r'\x03(?:\x03|(?:(?![\x0A\x02]).)*\x0A)*?(?:(?![\x0A\x02]).)*\x02' def sanitized_string(dirty_string): + """ + Remove the corrupted parts from the input text based on the + CORRUPTION_REGEX pattern. + + Args: + dirty_string (str): The input text containing the corrupted parts. + + Returns: + str: The cleaned text with the corrupted parts removed. + """ pattern = re.compile(CORRUPTION_REGEX, re.DOTALL) # pattern = CORRUPTION_REGEX cleaned_string = re.sub( @@ -20,15 +41,21 @@ def sanitized_string(dirty_string): return cleaned_string # MAIN + +# Setup argument parser parser = argparse.ArgumentParser( description='A program to clean a file based on a specific \ pattern' ) + +# Add input file path argument parser.add_argument( 'input_file_path', type=str, help='path to a input file to be cleaned' ) + +# Add optional output file path argument parser.add_argument( '-o', '--output_file_path', type=str, @@ -36,14 +63,17 @@ parser.add_argument( default=None ) +# Parse the arguments and store them args = parser.parse_args() +# If an output file is provided, write the sanitized text to the output file if args.output_file_path is not None: with open(args.input_file_path, 'r') as input_file, \ open(args.output_file_path, 'w') as output_file: dirty_text = input_file.read() sanitized_text = sanitized_string(dirty_text) output_file.write(sanitized_text) +# If no output file is provided, print the sanitized text to the console else: with open(args.input_file_path, 'r') as input_file: dirty_text = input_file.read()