Sarah Inzerillo 3 years ago
parent
commit
3ae4df8572
  1. 2
      build.sh
  2. 50
      docs/design.md
  3. 107
      src/Buffer.cpp
  4. 3
      src/Buffer.h
  5. BIN
      src/Buffer.h.gch
  6. BIN
      src/FileBrowser.h.gch

2
build.sh

@ -3,7 +3,7 @@ if [ ! -d obj ]; then
mkdir obj
fi
cd obj
g++ ../src/*.cpp -c
g++ ../src/*.cpp -c -g
cd ..
if [ ! -d bin ]; then
mkdir bin

50
docs/design.md

@ -6,13 +6,21 @@
### COMPONENT: class FileBrowser
A simple file browser. See program specification for details.
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.
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.
@ -29,41 +37,45 @@ A buffer for a simple file browser. Holds the lines of text and executes comman
(Compiler-generated.) Creates an empty buffer.
* void display() const
* back()
Opens the previously open file.
Displays the lines of text that the user is currently viewing.
* void display() const
Displays the lines of text that the user is currently viewing.
* const string & file_name() const
* const string & file_name() const
Returns the name of the file.
Returns the name of the file.
* bool go(int link)
* 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.
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()
* void max_links()
Returns the maximum number of links on the page. This is used by the file browser.
Returns the maximum number of links on the page. This is used by the file browser.
* void move_to_next_page()
* void move_to_next_page()
Self-explanatory.
Self-explanatory.
* void move_to_previous_page()
* void move_to_previous_page()
Self-explanatory.
Self-explanatory.
* bool open(const string & file_name)
* 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.
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)
* void set_maximum_length(int l)
Self-explanatory.
* void set_window_height(int h)
* 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.
**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.

107
src/Buffer.cpp

@ -16,57 +16,53 @@ void Buffer::display() const
}
//Pass the string by copy because I don't want to change the original, but still need to manipulate it
vector<string> Buffer::split_line(string str) {
vector<string> v = {};
//Split up the lines
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 os = str_part.find(" ");
size_t total_pos = os;
bool searching = true;
//While searching for the next space in a string
while (searching)
//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(os + 1, str_part.length());
os = str_part.find(" ");
if (os == -1)
{
searching = false;
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 + os + 1 > maximum_length_)
{
searching = false;
else if (total_pos + curr_pos + 1 > maximum_length_)
{
searching = false;
}
else
{
total_pos += curr_pos + 1;
}
else
{
total_pos += os + 1;
}
}
//Check if the string part can still be chopped
if (str_part.length() != str.length()) {
}
//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.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.push_back(str);
//for (auto e: v)
// cout << e << endl;
return v;
//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>");
@ -104,7 +100,7 @@ bool Buffer::open(const string & new_file_name, bool add_to_hist_)
//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 file_name = link_tag.substr(0, second_space_loc - 1);
std::string link_name = link_tag.substr(second_space_loc + 1);
//Adds the link as a pair to a vector of links.
@ -120,19 +116,18 @@ bool Buffer::open(const string & new_file_name, bool add_to_hist_)
//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;
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);
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 != "")
{
//TODO This is causing some issues
v_lines_.push_back(curr_line);
{
split_line(curr_line);
curr_line = "";
if(is_p)
@ -145,29 +140,17 @@ bool Buffer::open(const string & new_file_name, bool add_to_hist_)
line = extra_text;
}
curr_line += line;
//Checks if the current line length is greater than the max,
//then splits it up and adds it to the vector if it is
if (curr_line.length() > maximum_length_) {
vector<string> v_split_line = split_line(curr_line);
for (auto e: v_split_line) {
v_lines_.push_back(e);
}
curr_line = "";
}
}
//Push the contents of as it has the last line in the file.
v_lines_.push_back(curr_line);
ix_top_line_ = 0;
// 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;
}

3
src/Buffer.h

@ -26,8 +26,9 @@ public:
private:
std::size_t get_tag_(const std::string & line);
void split_line(std::string str);
std::string file_name_;
std::vector<std::string> split_line(std::string str);
std::vector<std::string> v_lines_;
std::vector<std::pair<std::string, std::string>> v_links_;
std::vector<std::string> v_hist_;

BIN
src/Buffer.h.gch

BIN
src/FileBrowser.h.gch

Loading…
Cancel
Save