CS253: Software Development with C++

Spring 2022

HW 5

CS253 HW5: Options!                

Changes                

Behavior is undefined if case-independent is in effect, and two -c arguments differ only by case, e.g., -c yip -i -c YIp. You may treat this as an error, ignore it, or any other behavior.                 

Look into toupper() for case conversion for case-independent matching. It converts a single char; not an entire string.                 

Description                

For this assignment, you will build upon your previous work in HW3, adding command-line options parsed with getopt(). This is a complete program, not a library.                 

Words                

Only replace a complete word. We define a word as a sequence of letters, a…zA…Z, delimited (bordered) by non-letters, or the start/end of the line. If we’re censoring the word “foo”, then input such as “fooYMP” must not be changed, but “foo4194303” must be.                 

Arguments                

The first command-line arguments should be options :                 

-r replacement
The argument is the replacement string for censoring. It may have any value. If this option is not given, use the eight-character string “CENSORED”.
-c one-word-to-censor
Add this word to the list of words to censor.
-i
If this option is given, words to censor are considered to be case-independent. -c hAMbonE -i will censor the word “HamBone” in the input. If this options is not given, matching is case-dependent, as in HW3.
-v
Announce each filename to standard output, just before reading the data inside it. Before reading standard input, announce that in some way that includes the words “standard input”.

After the options comes an optional list of filenames, as in HW3. If any filenames are given, read from them, and write their censored contents to standard output. Otherwise, read from standard input, instead, and write its censored contents to standard output.                 

This is the Colorado State University CS253 web page https://cs.colostate.edu/~cs253/Spring22/HW5 fetched by unknown <unknown> with Linux UID 65535 at 2024-11-21T20:00:22 from IP address 18.117.172.189. Registered CSU students are permitted to copy this web page for personal use, but it is forbidden to repost the information from this web page to the internet. Doing so is a violation of the rules in the CS253 syllabus, will be considered cheating, and will get you an F in CS253.

Sample Runs                

Here are sample runs, where % is my prompt.                 

% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(hw5)

# Are we in the wrong directory?
if (CMAKE_SOURCE_DIR MATCHES "[Hh][Ww]([0-9])$"
   AND NOT PROJECT_NAME MATCHES "${CMAKE_MATCH_1}$")
    message(FATAL_ERROR "Building ${PROJECT_NAME} in ${CMAKE_SOURCE_DIR}")
endif()

# Using -Wall is required:
add_compile_options(-Wall)

# These compile flags are highly recommended, but not required:
add_compile_options(-Wextra -Wpedantic)

# Optional super-strict mode:
add_compile_options(-fmessage-length=80 -fno-diagnostics-show-option
    -fstack-protector-all -g -O3 -std=c++17 -Walloc-zero -Walloca
    -Wctor-dtor-privacy -Wduplicated-cond -Wduplicated-branches -Werror
    -Wextra-semi -Wfatal-errors -Winit-self -Wlogical-op -Wold-style-cast
    -Wshadow -Wunused-const-variable=1 -Wzero-as-null-pointer-constant)

# add_compile_options must be BEFORE add_executable.

# Create the executable from the source file main.cc:
add_executable(${PROJECT_NAME} main.cc match.cc)

# Create a tar file every time:
add_custom_target(${PROJECT_NAME}.tar ALL COMMAND
    tar -cf ${PROJECT_NAME}.tar *.cc match.h CMakeLists.txt)
% cmake . && make
… cmake output appears here …
… make output appears here …
% cat lim1
There was a young woman named Bright,
Whose speed was much faster than light.
% cat lim2
She set out one day,
In a relative way,
And returned on the previous night.
% ./hw5 -c briGHT -c was lim1 lim2
There CENSORED a young woman named Bright,
Whose speed CENSORED much faster than light.
She set out one day,
In a relative way,
And returned on the previous night.
% ./hw5 -c bright -r@ -ic was lim1 lim2
There @ a young woman named @,
Whose speed @ much faster than light.
She set out one day,
In a relative way,
And returned on the previous night.
% echo -e "three . one four one five nine\ntwo six five" | ./hw5 -vc one
Reading standard input.
three . CENSORED four CENSORED five nine
two six five

Debugging                

If you encounter “STACK FRAME LINK OVERFLOW”, then try this:

    export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a

Hints                

Requirements                

Tar file                

    cmake . && make

How to submit your work:                

In Canvas, check in the file hw5.tar to the assignment “HW5”. It’s due 11:59ᴘᴍ MT Saturday, with a five-day late period.                 

How to receive negative points:                

Turn in someone else’s work.