CS253 HW2: Document Class                
Changes                
Modified the test code to #include <iostream>.
                
Description                
For this assignment, you will write a class called Doc
that
represents a simple document. This document is a collection of lines of
text, indented by varying amounts. Documents can be read or written to
files & I/O streams, and manipulated programatically.
                
Methods                
Doc
must have the following public methods:                
- default constructor
-
The default ctor creates an empty
Doc
.
- Copy constructor
-
- Assignment operator
-
Copy all information from another object of the same class.
- Destructor
-
Destroy.
-
Doc(path)
-
Same as
.read(path)
, below.
-
Doc(istream)
-
Same as
.read(istream)
, below.
-
.read(path)
-
Read everything in the file represented by the
std::string
argument as a document, replacing any existing data. Produce an error
message, mentioning the file name, if the file can’t be read. For
each line, strip trailing spaces and carriage return ('\r'
)
characters. The newline does not become part of the Doc
object.
Any remaining leading spaces become the indentation amount for this
line. The input line " x "
must be stored as the number
3
and the string "x"
. If a tab character ('\t'
) is read,
produce an error.
Leading & trailing (at the start or the end of the document) empty
(after stripping trailing space/return) lines are ignored, and do not
participate in any other methods.
-
.read(istream)
-
Like the previous method, but read from the given input stream.
-
.write(path)
-
Write the document to the file represented by the std::string
argument. The indentation for each line is written as the appropriate
number of spaces. Each line is terminated by a newline, even the
last one.
-
.write(ostream)
-
Like the previous method, but write to the given output stream.
-
.add(string)
-
Add a line to the end of the
Doc
. It is assumed that the line
doesn’t contain a newline. Treat leading spaces as the indentation,
which are not stored as part of the string. The trailing space/return
stripping of .read()
, and ignoring leading/trailing empty lines,
must not happen.
-
.size()
-
Return a size_t indicating the number of lines in the object.
-
.empty()
-
Return true iff the document contains no lines.
-
.indent(size_t)
-
Return, as a size_t, the indentation of the line indicated by the
argument. The first line is line zero. If the number given is too
large to indicate a line, produce an error mentioning the bad line
number and how many lines the object has.
-
.data(size_t)
-
Return, as a std::string, the part of the line indicated by the
argument, without the indentation. The first line is line zero. If
the number given is too large to indicate a line, produce an error
mentioning the bad line number and how many lines the object has.
The types and names in the method descriptions, above, do not
determine the C++ declarations of those methods. They only serve to
informally describe what sort of arguments a method might take.
You might pass certain arguments by reference, use const, declare
return types, etc.
                
Const-correctness, for arguments, methods, and operators, is your job.
For example, it must be possible to call .size()
on a const
object, or to copy a const object to a non-const object.
                
You may define other methods or data, public or private, as you see fit.
You may define other classes, as you see fit. However, to use the
Doc
class, the user need only #include "Doc.h"
, not
any other header files.
                
Hints                
For the ctor and .read()
method that take paths, what directory is
the file in!? Don’t worry about it. Just take the filename that was
given to you, and pass it to ifstream. It will either work or it
won’t. If it’s a relative path, then it’s relative to the current
directory. If it’s an absolute path, then it’s an absolute path. This
is not your problem.
                
Non-Requirements                
Several things are not specified by this assignment. That means
that the answer to these questions is “It’s up to you.”
- What is the return type of
.add()
?
- How should I store the data?
- Should I have one
.cc
file, or several?
Debugging                
If you encounter “STACK FRAME LINK OVERFLOW”, then try this:
export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a
Libraries                
libhw2.a
is a library file. It contains a number of
*.o
(object) files. It must contain Doc.o
, but it may also
contain whatever other *.o
files you need. The CMakeLists.txt
shown creates libhw2.a
. It does not contain main().
                
To be explicit, the provided CMakeLists.txt
does:
- creates
Doc.o
by compiling Doc.cc
- creates a library
libhw2.a
from Doc.o
- creates
test.o
by compiling test.cc
- creates an executable called
test
by linking test.o
with libhw2.a
.
The tar file must contain at least all the files required to do this.
                
Testing                
You will have to write a main() function to test your code. Put it in a
separate file, and do not make it part of libhw2.a
.
Particularly, do not put main() in Doc.h
or Doc.cc
. You
will also have to create Doc.h
, and put it into hw2.tar
.
We will test your program by doing something like this:
                
mkdir a-new-directory
cd the-new-directory
tar -x </some/where/else/hw2.tar
cmake . && make
cp /some/other/place/test-program.cc .
g++ -Wall test-program.cc libhw2.a
./a.out
We will supply a main program to do the testing that we want. You
should do something similar. It’s your choice whether to include your
test program in your hw2.tar
file.
However, cmake . && make
must work. If it fails because you
didn’t package test.cc
, but your CMakeLists.txt
requires
test.cc
, then your build failed, and you get no points.
Test your tar file, not just your code.
                
This is the Colorado State University CS253 web page
https://cs.colostate.edu/~cs253/Fall22/HW2
fetched by unknown <unknown> with Linux UID 65535
at 2024-08-30T16:16:05 from IP address 3.138.179.89.
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 Run                
Here is a sample run, where %
is my shell prompt:
                
% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(hw2)
# Are we in the wrong directory?
if (CMAKE_SOURCE_DIR MATCHES "[Hh][Ww]([0-9])$")
if (NOT PROJECT_NAME MATCHES "${CMAKE_MATCH_1}$")
message(FATAL_ERROR "Building ${PROJECT_NAME} in ${CMAKE_SOURCE_DIR}")
endif()
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
-Wconversion -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_library(${PROJECT_NAME} Doc.cc)
add_executable(test test.cc)
target_link_libraries(test ${PROJECT_NAME})
# Create a tar file every time:
add_custom_target(${PROJECT_NAME}.tar ALL COMMAND
tar -cf ${PROJECT_NAME}.tar *.cc *.h CMakeLists.txt)
% cmake . && make
… cmake output appears here …
… make output appears here …
% cat test.cc
#include "Doc.h"
#include <cassert>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ofstream("data") << "\r \r \n\nPeter \n Ray\r \n Egon \n \n\r\n";
Doc d("data");
assert(!d.empty());
assert(d.size()==3);
assert(d.indent(0) == 0);
assert(d.indent(1) == 3);
assert(d.indent(2) == 2);
assert(d.data(0) == "Peter");
assert(d.data(1) == "Ray");
assert(d.data(2) == "Egon");
d.add(" Winston ");
assert(d.size() == 4);
assert(d.indent(3) == 3);
assert(d.data(3) == "Winston ");
d.add("");
assert(d.size() == 5);
assert(d.indent(4) == 0);
assert(d.data(4) == "");
cout << "Done.\n";
const Doc vacant;
assert(vacant.empty() && vacant.size()==0);
d = vacant;
assert(d.empty() && d.size()==0);
return 0;
}
% ./test
Done.
Requirements                
- Error messages must:
- state the problem (crying “
error
” alone is not good enough)
- go to standard error
- terminate the program.
- An empty line, or a completely empty document, is not an error.
- It must be an compile-time error or warning to ignore the return value
of
.size()
, .empty()
, indent()
, or data()
.
- You may not have any
using namespace
declarations in your
header files.
- All copies (copy ctor, assignment operator) are “deep”.
Do not share data between copies—that’s not making a copy.
- You may not use any external programs. You may not use system(),
fork(), popen(), execl(), execvp(), etc.
- You may not use C-style I/O, such as printf(), scanf(), fopen(),
and getchar().
- You may not use endl. Use flush if needed.
- You may not use dynamic memory via new, delete, malloc(),
calloc(), realloc(), free(), strdup(), etc.
- It’s ok to implicitly use dynamic memory via containers such as
string or vector.
- You may not use the .eof() method.
- No global variables.
- For readability, don’t use ASCII int constants (
65
) instead of
char constants ('A'
) for printable characters.
- We will compile your code like this:
cmake . && make
- If that generates warnings, you will lose a point.
- If that generates errors, you will lose all points.
- There is no automated testing/pre-grading/re-grading.
- Test your code yourself. It’s your job.
- Even if you only change it a little bit.
- Even if all you do is add a comment.
- Test with the CSU compilers, not just your laptop’s compiler.
If you have any questions about the requirements, ask.
In the real world, your programming tasks will almost always be
vague and incompletely specified. Same here.
                
Tar file                
- The tar file for this assignment must be called:
hw2.tar
- It must contain:
- source files (
*.cc
), including Doc.cc
- header files (
*.h
), including Doc.h
CMakeLists.txt
, which will create the library file
libhw2.a
.
- These commands must produce the library lib
hw2.a
:
cmake . && make
- Your
CMakeLists.txt
must use at least -Wall
when compiling.
How to submit your work:                
In Canvas, check in the
file
hw2.tar
to the assignment “HW2”.
It’s due 11:59ᴘᴍ MT Saturday, with a 24-hour late period for a 25% penalty.
                
How to receive negative points:                
Turn in someone else’s work.