Browse Source

Changed docs

master
stew3254 3 years ago
parent
commit
7cac47424c
  1. 166
      docs/design.md
  2. 2
      docs/status_report.txt
  3. 276
      src/Buffer.cpp
  4. 16
      src/Buffer.h
  5. 50
      src/FileBrowser.cpp
  6. 4
      src/FileBrowser.h
  7. 22
      src/main.cpp

166
docs/design.md

@ -1,81 +1,85 @@
# File Browser
## DESIGN DOCUMENT
### COMPONENT: class FileBrowser
A simple file browser. Acts as the interface between the user and its components.
#### Public method:
* void display()
Updates the contents of the screen with the file contents that the user should see as well as UI and prompts surronding it.
* void execute(char command, bool & done)
Executes the command that the user inputs, represented by a single character. The user can open a file, scroll through the contents of the file, open links, open previously opened files and exit the program.
* void run()
Runs the browser. It obtains size parameters from the user and then enters the browser's primary loop.
**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 browser. 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:
* Buffer()
(Compiler-generated.) Creates an empty buffer.
* back()
Opens the previously open file.
* void display() const
Displays the lines of text that the user is currently viewing.
* const string & file_name() const
Returns the name of the file.
* bool go(int link)
Goes to the specified link in the current file. This opens the new link file and adds it to the open history.
* void max_links()
Returns the maximum number of links on the page. This is used by the file browser.
* void move_to_next_page()
Self-explanatory.
* void move_to_previous_page()
Self-explanatory.
* bool open(const string & file_name)
Executes the corresponding file viewer command on the buffer. If a file is currently open, it adds the previous file to the open history. Lines in the file are formatted. Paragraph tags(<p>) define bodies of text which are seperated by other bodies of text by a line. Break tags(<br>) become a newline in the viewer. Anchor tags(<a>) are links. The method open returns true if successful.
* void set_maximum_length(int l)
Self-explanatory.
* 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, which should be formatted and ready to display without further processing. The links that are processed in the open command are stored as a pair in another 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. A utility function that handles the processing of lines that would be longer than the browser's set width into multible lines.
# File Browser
## DESIGN DOCUMENT
### COMPONENT: class FileBrowser
A simple file browser. Acts as the interface between the user and its components.
#### Public method:
* void display()
Updates the contents of the screen with the file contents that the user should see as well as UI and prompts surronding it.
* void execute(char command, bool & done)
Executes the command that the user inputs, represented by a single character. The user can open a file, scroll through the contents of the file, open links, open previously opened files and exit the program.
* void run()
Runs the browser. It obtains size parameters from the user and then enters the browser's primary loop.
**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 browser. 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:
* Buffer()
(Compiler-generated.) Creates an empty buffer.
* back()
Opens the previously open file. Removes the current file from the history before going back.
* void display() const
Displays the lines of text that the user is currently viewing.
* const string & file_name() const
Returns the name of the file.
* bool go(int link)
Goes to the specified link in the current file. This opens the new link file and adds it to the open history.
* void max_links()
Returns the maximum number of links on the page. This is used by the file browser.
* void move_to_next_page()
Self-explanatory.
* void move_to_previous_page()
Self-explanatory.
* bool open(const string & file_name)
Executes the corresponding file viewer command on the buffer. If a file is currently open, it adds the previous file to the open history. Lines in the file are formatted. Paragraph tags(<p>) define bodies of text which are seperated by other bodies of text by a line. Break tags(<br>) become a newline in the viewer. Anchor tags(<a>) are links. This incorperates the split_line function as well. The method open returns true if successful, otherwise false.
* void Buffer::split_line(string str)
Takes a copy of the line given to it, and then splits the line up into subtrings that are less than the maximum length. If a word is longer than the maximum length, it prints that word on a line on its own.
* void set_maximum_length(int l)
Self-explanatory.
* 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, which should be formatted and ready to display without further processing. The links that are processed in the open command are stored as a pair in another 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. A utility function that handles the processing of lines that would be longer than the browser's set width into multible lines.

2
docs/status_report.txt

@ -1,3 +1,3 @@
Ryan Stewart, Cameron Weinfurt, Sarah Inzerillo
Sarah Inzerillo, Ryan Stewart, and Cameron Weinfurt,
Our file fiewer is complete and runs as expected.

276
src/Buffer.cpp

@ -19,164 +19,164 @@ void Buffer::display() const
//Pass the string by copy because I don't want to change the original, but still need to manipulate it
void Buffer::split_line(string str)
{
// Split up the lines and then push them onto the line vector
while (str.length() > maximum_length_)
{
//Initialize the substring and set the positions in the string where they need to exist
string str_part = str;
size_t curr_pos = str_part.find(" ");
size_t total_pos = curr_pos;
bool searching = true;
//While searching for the next space in a string
while (searching)
{
str_part = str_part.substr(curr_pos + 1, str_part.length());
curr_pos = str_part.find(" ");
if (curr_pos == -1)
{
searching = false;
}
else if (total_pos + curr_pos + 1 > maximum_length_)
{
searching = false;
}
else
{
total_pos += curr_pos + 1;
}
}
//Check if the string part can still be chopped
if (str_part.length() != str.length())
{
str_part = str.substr(0, total_pos);
str = str.substr(total_pos + 1, str.length());
v_lines_.push_back(str_part);
}
//Break out of the loop because there is no way to make the string shorter
//I also didn't see why I can't use break and would have to use a boolean
else
{
break;
}
}
v_lines_.push_back(str);
// Split up the lines and then push them onto the line vector
while (str.length() > maximum_length_)
{
//Initialize the substring and set the positions in the string where they need to exist
string str_part = str;
size_t curr_pos = str_part.find(" ");
size_t total_pos = curr_pos;
bool searching = true;
//While searching for the next space in a string
while (searching)
{
str_part = str_part.substr(curr_pos + 1, str_part.length());
curr_pos = str_part.find(" ");
if (curr_pos == -1)
{
searching = false;
}
else if (total_pos + curr_pos + 1 > maximum_length_)
{
searching = false;
}
else
{
total_pos += curr_pos + 1;
}
}
//Check if the string part can still be chopped
if (str_part.length() != str.length())
{
str_part = str.substr(0, total_pos);
str = str.substr(total_pos + 1, str.length());
v_lines_.push_back(str_part);
}
//Break out of the loop because there is no way to make the string shorter
//I also didn't see why I can't use break and would have to use a boolean
else
{
break;
}
}
v_lines_.push_back(str);
}
size_t Buffer::get_tag_(const string & line)
{
size_t p_tag = line.find("<p>");
size_t b_tag = line.find("<br>");
size_t p_tag = line.find("<p>");
size_t b_tag = line.find("<br>");
if (p_tag > b_tag)
return b_tag;
else
return p_tag;
if (p_tag > b_tag)
return b_tag;
else
return p_tag;
}
bool Buffer::open(const string & new_file_name, bool add_to_hist_)
{
std::ifstream file(new_file_name);
if (!file)
return false;
v_links_.clear();
v_lines_.clear();
//Note: the vector is cleared only after we know the file
//opened successfully.
int curr_link = 0;
std::string curr_line, line;
while(getline(file, line))
{
//Find tags and process them.
for(auto tag_loc = line.find("<a "); tag_loc != -1; tag_loc = line.find("<a", tag_loc + 1))
{
++curr_link;
//Find the length of the tag and pull out the data from the tag.
auto tag_len = line.find_first_of('>', tag_loc) - tag_loc;
std::string link_tag = line.substr(tag_loc + 3, tag_len - 3);
//Separate the link path and link name into separate strings. Assuming no spaces in the link path.
auto second_space_loc = link_tag.find_first_of(' ', 0);
std::string file_name = link_tag.substr(0, second_space_loc);
std::string link_name = link_tag.substr(second_space_loc + 1);
//Adds the link as a pair to a vector of links.
v_links_.push_back({file_name, link_name});
//Reformat the link tag to match specification.
line.replace(tag_loc, tag_len + 1, "<" + link_name + ">[" + to_string(curr_link) + "]");
}
//Search for all paragraphs and breaks in the line and add them to the v_lines vector.
for(auto _tag = get_tag_(line); _tag != -1; _tag = get_tag_(line))
{
//Check to see if we have a paragraph tag, so we can add a blank line.
bool is_p = _tag == line.find("<p>");
int tag_len = is_p ? 3 : 4;
//Separate out text that should stay in the previous line and text that goes in the new line. Delete the tag in the process.
std::string extra_text = line.substr(_tag + tag_len);
line.erase(_tag);
curr_line += line;
//However, if the tag is located at the start of the file, there will be no data, so it shouldn't be added as it will be an empty line.
if(curr_line != "")
{
split_line(curr_line);
curr_line = "";
if(is_p)
{
v_lines_.push_back("");
}
}
//Move any remaining data on the line back into the line variable so that it can be processed.
line = extra_text;
}
curr_line += line;
}
// Push the contents of curr_line as it has the last line in the file.
split_line(curr_line);
ix_top_line_ = 0;
file_name_ = new_file_name;
if(add_to_hist_)
{
v_hist_.push_back(file_name_);
curr_link_itr = v_hist_.end() - 1;
}
return true;
std::ifstream file(new_file_name);
if (!file)
return false;
v_links_.clear();
v_lines_.clear();
//Note: the vector is cleared only after we know the file
//opened successfully.
int curr_link = 0;
std::string curr_line, line;
while(getline(file, line))
{
//Find tags and process them.
for(auto tag_loc = line.find("<a "); tag_loc != -1; tag_loc = line.find("<a", tag_loc + 1))
{
++curr_link;
//Find the length of the tag and pull out the data from the tag.
auto tag_len = line.find_first_of('>', tag_loc) - tag_loc;
std::string link_tag = line.substr(tag_loc + 3, tag_len - 3);
//Separate the link path and link name into separate strings. Assuming no spaces in the link path.
auto second_space_loc = link_tag.find_first_of(' ', 0);
std::string file_name = link_tag.substr(0, second_space_loc);
std::string link_name = link_tag.substr(second_space_loc + 1);
//Adds the link as a pair to a vector of links.
v_links_.push_back({file_name, link_name});
//Reformat the link tag to match specification.
line.replace(tag_loc, tag_len + 1, "<" + link_name + ">[" + to_string(curr_link) + "]");
}
//Search for all paragraphs and breaks in the line and add them to the v_lines vector.
for(auto _tag = get_tag_(line); _tag != -1; _tag = get_tag_(line))
{
//Check to see if we have a paragraph tag, so we can add a blank line.
bool is_p = _tag == line.find("<p>");
int tag_len = is_p ? 3 : 4;
//Separate out text that should stay in the previous line and text that goes in the new line. Delete the tag in the process.
std::string extra_text = line.substr(_tag + tag_len);
line.erase(_tag);
curr_line += line;
//However, if the tag is located at the start of the file, there will be no data, so it shouldn't be added as it will be an empty line.
if(curr_line != "")
{
split_line(curr_line);
curr_line = "";
if(is_p)
{
v_lines_.push_back("");
}
}
//Move any remaining data on the line back into the line variable so that it can be processed.
line = extra_text;
}
curr_line += line;
}
// Push the contents of curr_line as it has the last line in the file.
split_line(curr_line);
ix_top_line_ = 0;
file_name_ = new_file_name;
if(add_to_hist_)
{
v_hist_.push_back(file_name_);
curr_link_itr = v_hist_.end() - 1;
}
return true;
}
bool Buffer::go(int link)
{
string f_name = v_links_[link-1].first;
string f_name = v_links_[link-1].first;
if(open(f_name))
return true;
else
return false;
if(open(f_name))
return true;
else
return false;
}
bool Buffer::back()
{
if(v_hist_.size() <= 1)
return false;
if(v_hist_.size() <= 1)
return false;
curr_link_itr = curr_link_itr - 1;
curr_link_itr = curr_link_itr - 1;
v_hist_.erase(curr_link_itr + 1);
v_hist_.erase(curr_link_itr + 1);
open(*curr_link_itr, false);
open(*curr_link_itr, false);
return true;
return true;
}

16
src/Buffer.h

@ -12,7 +12,7 @@
class Buffer
{
public:
public:
bool back();
void display() const;
const std::string & file_name() const { return file_name_; }
@ -24,7 +24,7 @@ public:
void set_maximum_length(int l) { maximum_length_ = l; }
void set_window_height(int h) { window_height_ = h; }
private:
private:
std::size_t get_tag_(const std::string & line);
void split_line(std::string str);
@ -41,16 +41,16 @@ private:
inline void Buffer::move_to_next_page()
{
ix_top_line_ += window_height_;
if (ix_top_line_ >= v_lines_.size())
ix_top_line_ -= window_height_;
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;
ix_top_line_ -= window_height_;
if (ix_top_line_ < 0)
ix_top_line_ = 0;
}
#endif

50
src/FileBrowser.cpp

@ -84,31 +84,31 @@ void FileBrowser::execute_command(char command, bool & done)
void FileBrowser::run()
{
cout << "Window height? ";
cin >> window_height_;
cin.get(); // '\n'
buffer_.set_window_height(window_height_);
cout << "Maximum length? ";
cin >> maximum_length_;
cin.get(); // '\n'
buffer_.set_maximum_length(maximum_length_);
cout << endl;
bool done = false;
while (!done)
{
display();
cout << "command: ";
char command;
cin >> command;
cin.get(); // '\n'
execute_command(command, done);
cout << endl;
}
cout << "Window height? ";
cin >> window_height_;
cin.get(); // '\n'
buffer_.set_window_height(window_height_);
cout << "Maximum length? ";
cin >> maximum_length_;
cin.get(); // '\n'
buffer_.set_maximum_length(maximum_length_);
cout << endl;
bool done = false;
while (!done)
{
display();
cout << "command: ";
char command;
cin >> command;
cin.get(); // '\n'
execute_command(command, done);
cout << endl;
}
}

4
src/FileBrowser.h

@ -12,10 +12,10 @@
class FileBrowser
{
public:
public:
void run();
private:
private:
void display();
void execute_command(char command, bool & done);

22
src/main.cpp

@ -1,11 +1,11 @@
// Launches the the browser
#include "FileBrowser.h"
int main()
{
FileBrowser browser;
browser.run();
return 0;
}
// Launches the the browser
#include "FileBrowser.h"
int main()
{
FileBrowser browser;
browser.run();
return 0;
}
Loading…
Cancel
Save