Browse Source

Adding direcrtories and adding FileViewer code.

pull/1/head
Cameron Weinfurt 4 years ago
commit
7e819e7889
  1. 0
      README.md
  2. 34
      src/Buffer.cpp
  3. 43
      src/Buffer.h
  4. 37
      src/FileViewer.cbp
  5. 84
      src/FileViewer.cpp
  6. 71
      src/FileViewer.depend
  7. 29
      src/FileViewer.h
  8. 30
      src/FileViewer.layout
  9. 51
      src/design.txt
  10. 31
      src/preface.txt
  11. 19
      src/sample_interface.txt
  12. 55
      src/specification.txt
  13. 13
      src/test.cpp

0
README.md

34
src/Buffer.cpp

@ -0,0 +1,34 @@
// Buffer.cpp
#include "Buffer.h"
using namespace std;
void Buffer::display() const
{
int ix_stop_line_ = ix_top_line_ + window_height_;
for (int i = ix_top_line_; i < ix_stop_line_; ++i) {
if (i < v_lines_.size())
cout << std::setw(6) << i+1 << " " << v_lines_[i];
cout << '\n';
}
}
bool Buffer::open(const string & new_file_name)
{
std::ifstream file(new_file_name);
if (!file)
return false;
v_lines_.clear();
// Note: the vector is cleared only after we know the file
// opened successfully.
string line;
while (getline(file, line))
v_lines_.push_back(line);
file_name_ = new_file_name;
ix_top_line_ = 0;
return true;
}

43
src/Buffer.h

@ -0,0 +1,43 @@
// Buffer.h
#ifndef _Buffer_h_
#define _Buffer_h_
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
class Buffer
{
public:
void display() const;
const std::string & file_name() const { return file_name_; }
void move_to_next_page();
void move_to_previous_page();
bool open(const std::string & file_name);
void set_window_height(int h) { window_height_ = h; }
private:
std::vector<std::string> v_lines_;
int ix_top_line_ = 0;
std::string file_name_;
int window_height_;
};
inline void Buffer::move_to_next_page()
{
ix_top_line_ += window_height_;
if (ix_top_line_ >= v_lines_.size())
ix_top_line_ -= window_height_;
}
inline void Buffer::move_to_previous_page()
{
ix_top_line_ -= window_height_;
if (ix_top_line_ < 0)
ix_top_line_ = 0;
}
#endif

37
src/FileViewer.cbp

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="FileViewer" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Release">
<Option output="./FileViewer" prefix_auto="1" extension_auto="1" />
<Option object_output="./" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="Buffer.cpp" />
<Unit filename="Buffer.h" />
<Unit filename="FileViewer.cpp" />
<Unit filename="FileViewer.h" />
<Unit filename="test.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

84
src/FileViewer.cpp

@ -0,0 +1,84 @@
// FileViewer.cpp
#include "FileViewer.h"
using namespace std;
void FileViewer::display()
{
const string long_separator(50, '-');
const string short_separator(8, '-');
system(clear_command);
if (!error_message_.empty()) {
cout << "ERROR: " + error_message_ << endl;
error_message_.clear();
}
string file_name = buffer_.file_name();
if (file_name.empty())
cout << "<no file opened>\n";
else
cout << file_name << endl;
cout << long_separator << endl;
buffer_.display();
cout << long_separator << endl;
cout << " next previous open quit\n";
cout << short_separator << endl;
}
void FileViewer::execute_command(char command, bool & done)
{
switch (command) {
case 'n': {
buffer_.move_to_next_page();
break;
}
case 'o': {
cout << "file name: ";
string file_name;
getline(cin, file_name);
if (!buffer_.open(file_name))
error_message_ = "Could not open " + file_name;
break;
}
case 'p': {
buffer_.move_to_previous_page();
break;
}
case 'q': {
done = true;
break;
}
}
}
void FileViewer::run()
{
cout << "Window height? ";
cin >> window_height_;
cin.get(); // '\n'
cout << '\n';
buffer_.set_window_height(window_height_);
bool done = false;
while (!done) {
display();
cout << "command: ";
char command;
cin >> command;
cin.get(); // '\n'
execute_command(command, done);
cout << endl;
}
}

71
src/FileViewer.depend

@ -0,0 +1,71 @@
# depslib dependency file v1.0
1392679174 source:c:\users\alexis\documents\cs142\prep\fileviewer\fileviewer.cpp
"FileViewer.h"
<iostream>
<string>
1520631963 c:\users\alexis\documents\cs142\prep\fileviewer\fileviewer.h
<cstdlib>
<iostream>
<string>
"Buffer.h"
1520627022 c:\users\alexis\documents\cs142\prep\fileviewer\buffer.h
<fstream>
<iomanip>
<iostream>
<string>
<vector>
1392677214 source:c:\users\alexis\documents\cs142\prep\fileviewer\test.cpp
"FileViewer.h"
1520627231 source:c:\users\alexis\documents\cs142\prep\fileviewer\buffer.cpp
"Buffer.h"
1508255405 source:c:\users\alexis\documents\cs142\cours\fileviewer\buffer.cpp
"Buffer.h"
<fstream>
<iomanip>
<iostream>
<string>
1508255313 c:\users\alexis\documents\cs142\cours\fileviewer\buffer.h
<vector>
<string>
1508255837 source:c:\users\alexis\documents\cs142\cours\fileviewer\fileviewer.cpp
"FileViewer.h"
<iostream>
<string>
1508255790 c:\users\alexis\documents\cs142\cours\fileviewer\fileviewer.h
<string>
"Buffer.h"
1392680814 source:c:\users\alexis\documents\cs142\cours\fileviewer\test.cpp
"FileViewer.h"
1488751156 source:c:\users\alexis\documents\cs142\web\code\fileviewer\buffer.cpp
"Buffer.h"
<fstream>
<iomanip>
<iostream>
<string>
1488751111 c:\users\alexis\documents\cs142\web\code\fileviewer\buffer.h
<vector>
<string>
1392680820 source:c:\users\alexis\documents\cs142\web\code\fileviewer\fileviewer.cpp
"FileViewer.h"
<iostream>
<string>
1392680420 c:\users\alexis\documents\cs142\web\code\fileviewer\fileviewer.h
<string>
"Buffer.h"
1392677212 source:c:\users\alexis\documents\cs142\web\code\fileviewer\test.cpp
"FileViewer.h"

29
src/FileViewer.h

@ -0,0 +1,29 @@
// FileViewer.h
#ifndef _FileViewer_h_
#define _FileViewer_h_
#include <cstdlib>
#include <iostream>
#include <string>
#include "Buffer.h"
const char clear_command[] = "cls"; // for Windows
//const char clear_command[] = "clear"; // for Linux and possibly Mac's
class FileViewer
{
public:
void run();
private:
void display();
void execute_command(char command, bool & done);
Buffer buffer_;
int window_height_;
std::string error_message_;
};
#endif

30
src/FileViewer.layout

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Release" />
<File name="Buffer.cpp" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="395" topLine="3" />
</Cursor>
</File>
<File name="Buffer.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="449" topLine="3" />
</Cursor>
</File>
<File name="FileViewer.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="461" topLine="0" />
</Cursor>
</File>
<File name="FileViewer.cpp" open="1" top="1" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="458" topLine="5" />
</Cursor>
</File>
<File name="test.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="133" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

51
src/design.txt

@ -0,0 +1,51 @@
File Viewer
DESIGN DOCUMENT
COMPONENT: class FileViewer
A simple file viewer. See program specification for details.
Public method:
o void run()
Runs the viewer.
Implementation note: Holds the lines of text in a Buffer object. The displaying of the buffer contents and the execution of the commands is delegated to the Buffer object.
Collaborator: Buffer.
COMPONENT: class Buffer
A buffer for a simple file viewer. Holds the lines of text and executes commands on them. Displays the contents of the buffer. See program specification for details on commands.
Public methods:
o Buffer()
(Compiler-generated.) Creates an empty buffer.
o void display() const
Displays the lines of text that the user is currently viewing.
o const string & file_name() const
Returns the name of the file.
o void move_to_next_page()
void move_to_previous_page()
bool open(const string & file_name)
Executes the corresponding file viewer command on the buffer. See program specification for details. The method open returns true if successful.
o void set_window_height(int h)
Self-explanatory.
Implementation note: Stores each line of text as a string and all the lines in a vector. Also stores the index of the line currently displayed at the top of the window, as well as the name of the file and the window height.

31
src/preface.txt

@ -0,0 +1,31 @@
After a few computer science courses, students may start to
get the feeling that programs can always be written to
solve any computational problem. Writing the program may
be hard work. For example, it may involve learning a
difficult technique. And many hours of debugging. But
with enough time and effort, the program can be written.
So it may come as a surprise that this is not the case:
there are computational problems for which no program
exists. And these are not ill-defined problems (Can a
computer fall in love?) or uninteresting toy problems.
These are precisely defined problems with important
practical applications.
Theoretical computer science can be briefly described as
the mathematical study of computation. These notes will
introduce you to this branch of computer science by
focusing on computability theory and automata theory. You
will learn how to precisely define what computation is and
why certain computational problems cannot be solved. You
will also learn several concepts and techniques that have
important applications. Chapter 1 provides a more detailed
introduction to this rich and beautiful area of computer
science.
These notes were written for the course CS345 Automata
Theory and Formal Languages taught at Clarkson University.
The course is also listed as MA345 and CS541. The
prerequisites are CS142 (a second course in programming)
and MA211 (a course on discrete mathematics in which
students also gain experience writing mathematical proofs).

19
src/sample_interface.txt

@ -0,0 +1,19 @@
preface.txt
--------------------------------------------------
1 These notes are for a second course on computer programming and
2 software development. In a first course, you likely focused on
3 learning the basics: variables, control statements, input and
4 output, files, vectors (or arrays), functions and structures. You
5 may have also had an introduction to classes. These concepts are
6 critically important and they are sufficient for the creation of
7 many useful programs. But many other programs, especially large
8 ones, require more powerful concepts and techniques. And a deeper
9 understanding of classes and design principles.
10
11 In fact, the creation of large computer programs poses three basic
12 challenges. The overall goal of these notes is to teach you
--------------------------------------------------
next previous open quit
-------
command: o
file: introduction.txt

55
src/specification.txt

@ -0,0 +1,55 @@
File Viewer
SPECIFICATION
OVERVIEW
A simple file viewer that allows the user view the contents of a text file.
DETAILS
The program interacts with the user as shown in the following example:
preface.txt
--------------------------------------------------
1 These notes are for a second course on computer programming and
2 software development. In a first course, you likely focused on
3 learning the basics: variables, control statements, input and
4 output, files, vectors (or arrays), functions and structures. You
5 may have also had an introduction to classes. These concepts are
6 critically important and they are sufficient for the creation of
7 many useful programs. But many other programs, especially large
8 ones, require more powerful concepts and techniques. And a deeper
9 understanding of classes and design principles.
10
11 In fact, the creation of large computer programs poses three basic
12 challenges. The overall goal of these notes is to teach you
--------------------------------------------------
next previous open quit
-------
command: o
file: introduction.txt
The program begins by asking the user for a window height. This is the number of lines that will be displayed as each "page". The displayed lines are numbered starting at 1 for the first line of the file. If the number of lines on the last page is smaller than the window height, the rest of the window is filled with unnumbered empty lines.
Each page is displayed between two lines of 50 dashes. The name of the file is printed above the first line of dashes. If no file is currently open, the string "<no file opened>" is printed instead of the file name.
Below the second line of dashes, a menu of commands is displayed. Below that menu, the prompt "choice:" is displayed. The user types the first letter of a command, the command executes and everything is redisplayed. Some commands prompt the user for more information.
Here is a description of the various commands:
next: The next page is displayed. Does nothing if the last line of the file is already displayed.
previous: The previous page is displayed. Does nothing if the first line of the file is already displayed.
open: Asks for a file name (with prompt "file:") and displays that file. If a file named X does not open, the message "ERROR: Could not open X" is displayed just before the file name is redisplayed.
quit: Stops the program.
NOTES FOR LATER VERSIONS
Add more error-checking. For example, check that commands are entered properly and that the window height is a positive integer.

13
src/test.cpp

@ -0,0 +1,13 @@
// test.cpp
//
// Launches the text editor
#include "FileViewer.h"
int main()
{
FileViewer viewer;
viewer.run();
return 0;
}
Loading…
Cancel
Save