Browse Source

Go and Back functions work properly

stew
Sarah Inzerillo 3 years ago
parent
commit
15f40dd27a
  1. 0
      source.sh
  2. 533
      src/Buffer.cpp
  3. 8
      src/Buffer.h
  4. 18
      src/FileBrowser.cpp

0
source.sh

533
src/Buffer.cpp

@ -1,334 +1,199 @@
// Buffer.cpp
#include "Buffer.h"
using namespace std;
void Buffer::display() const
{
<<<<<<< HEAD
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';
}
=======
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(4) << i+1 << " " << v_lines_[i];
cout << '\n';
}
>>>>>>> 9159d27c4f65480ce2606eeb041b3ce03a1b1048
}
/*
string* Buffer::get_lines() const
{
<<<<<<< HEAD
string* lines = new string[window_height_];
int line = 0;
while (line < window_height_ && line + ix_top_line_ < v_lines_.size()) {
lines[line - ix_top_line_] = v_lines_[line];
++line;
}
return lines;
=======
string* lines = new string[window_height_];
int line_num = 0;
while (line_num < window_height_ && line_num + ix_top_line_ < v_lines_.size()) {
lines[line_num] = v_lines_[line_num + ix_top_line_];
//cout << v_lines_[line_num] << endl;
++line_num;
}
//cout << "----------" << endl << endl;;
return lines;
>>>>>>> 9159d27c4f65480ce2606eeb041b3ce03a1b1048
}
*/
vector<string> Buffer::split_line(string & str) {
//Initialize the substring and set the positions in the string where they need to exist
vector<string> v = {};
string str_part = str;
size_t curr_pos = str_part.find(" ");
size_t total_pos = curr_pos;
bool searching = true;
// Split up the lines
while (str.length() > maximum_length_)
{
//Re-initialize the substring and set the positions in the string where they need to exist
str_part = str;
curr_pos = str_part.find(" ");
total_pos = curr_pos;
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;
}
}
str_part = str.substr(0, total_pos);
str = str.substr(total_pos + 1, str.length());
v.push_back(str_part);
}
v.push_back(str);
return v;
}
size_t Buffer::get_tag_(const string & line)
{
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;
}
bool Buffer::open(const string & new_file_name)
{
<<<<<<< HEAD
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.
int curr_link = 0;
std::string curr_l, line;
while(getline(file, line))
{
// Remove any newlines that are in the file.
for(auto char_loc = line.find_first_of('\n'); char_loc != -1; char_loc = line.find_first_of('\n'))
line.erase(char_loc, 1);
// Replace break tags with newlines.
for(auto char_loc = line.find("<br>"); char_loc != -1; char_loc = line.find("<br>"))
line.replace(char_loc, 4, "\n");
// Find link tags and process them.
// 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);
// Seperate the link path and link name into seperate 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;
// Seperate 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_l += line;
// However, if the paragraph is empty, then it shouldn't be added.
if(curr_l != "")
{
v_lines_.push_back(curr_l);
curr_l = "";
}
// 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_l != "")
{
v_lines_.push_back(curr_l);
curr_l = "";
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;
}
// Append any data left on this line to the v_line being read. Add a space so that content from two lines can be seperated.
curr_l += " " + line;
}
// Push the contents of curr_p as it has the last line in the file.
v_lines_.push_back(curr_l);
file_name_ = new_file_name;
ix_top_line_ = 0;
v_hist_.push_back(file_name_);
curr_link_itr = v_hist_.end() - 1;
return true;
}
bool Buffer::go(const int & link)
{
string f_name = v_links_[link-1].first;
v_links_.clear();
if(!open(f_name))
return false;
else
{
return true;
}
}
bool Buffer::back()
{
if(v_hist_.empty())
return false;
curr_link_itr = prev(curr_link_itr);
v_hist_.erase(curr_link_itr + 1);
if(v_hist_.size() == 0)
return false;
open(*curr_link_itr);
return true;
}
=======
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.
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 - 1);
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 != "")
{
v_lines_.push_back(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;
// TODO: What is this section
if (curr_line.length() > maximum_length_) {
vector<string> v_split_line = split_line(curr_line);
for (auto e: v_split_line) {
cout << e << endl;
v_lines_.push_back(e);
}
curr_line = "";
}
}
// Push the contents of curr_p as it has the last line in the file.
v_lines_.push_back(curr_line);
ix_top_line_ = 0;
file_name_ = new_file_name;
return true;
}
bool Buffer::go(int link)
{
string f_name = v_links_[link - 1].second;
if(!open(f_name))
return false;
else
return true;
}
void Buffer::back()
{
}
>>>>>>> 9159d27c4f65480ce2606eeb041b3ce03a1b1048
// 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(4) << i+1 << " " << v_lines_[i];
cout << '\n';
}
}
vector<string> Buffer::split_line(string & str) {
//Initialize the substring and set the positions in the string where they need to exist
vector<string> v = {};
string str_part = str;
size_t curr_pos = str_part.find(" ");
size_t total_pos = curr_pos;
bool searching = true;
// Split up the lines
while (str.length() > maximum_length_)
{
//Re-initialize the substring and set the positions in the string where they need to exist
str_part = str;
curr_pos = str_part.find(" ");
total_pos = curr_pos;
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;
}
}
str_part = str.substr(0, total_pos);
str = str.substr(total_pos + 1, str.length());
v.push_back(str_part);
}
v.push_back(str);
return v;
}
size_t Buffer::get_tag_(const string & line)
{
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;
}
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.
int curr_link = 0;
std::string curr_l, line;
while(getline(file, line))
{
// Remove any newlines that are in the file.
for(auto char_loc = line.find_first_of('\n'); char_loc != -1; char_loc = line.find_first_of('\n'))
line.erase(char_loc, 1);
// Replace break tags with newlines.
for(auto char_loc = line.find("<br>"); char_loc != -1; char_loc = line.find("<br>"))
line.replace(char_loc, 4, "\n");
// Find link tags and process them.
// 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);
// Seperate the link path and link name into seperate 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;
// Seperate 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_l += line;
// However, if the paragraph is empty, then it shouldn't be added.
if(curr_l != "")
{
v_lines_.push_back(curr_l);
curr_l = "";
}
// 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_l != "")
{
v_lines_.push_back(curr_l);
curr_l = "";
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;
}
// Append any data left on this line to the v_line being read. Add a space so that content from two lines can be seperated.
curr_l += " " + line;
}
// Push the contents of curr_p as it has the last line in the file.
v_lines_.push_back(curr_l);
file_name_ = new_file_name;
ix_top_line_ = 0;
if(add_to_hist_ == 1){
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;
v_links_.clear();
add_to_hist_ = 1;
if(!open(f_name))
return false;
else
{
return true;
}
}
bool Buffer::back()
{
if(v_hist_.empty())
return false;
cout << endl;
curr_link_itr = curr_link_itr - 1;
v_hist_.erase(curr_link_itr + 1);
add_to_hist_ = 0;
open(*curr_link_itr);
return true;
}

8
src/Buffer.h

@ -13,23 +13,16 @@
class Buffer
{
public:
void back();
void display() const;
const std::string & file_name() const { return file_name_; }
bool go(int link);
void move_to_next_page();
void move_to_previous_page();
int max_links();
<<<<<<< HEAD
bool go(const int & link);
bool back();
=======
>>>>>>> 9159d27c4f65480ce2606eeb041b3ce03a1b1048
bool open(const std::string & file_name);
//std::string* get_lines() const;
void set_maximum_length(int l) { maximum_length_ = l; }
void set_window_height(int h) { window_height_ = h; }
//Made window height public so the display function can be removed
private:
std::size_t get_tag_(const std::string & line);
@ -39,6 +32,7 @@ private:
std::vector<std::pair<std::string, std::string>> v_links_;
std::vector<std::string> v_hist_;
std::vector<std::string>::iterator curr_link_itr = v_hist_.end();
bool add_to_hist_ = 1;
int ix_top_line_ = 0;
int window_height_ = 0;
int maximum_length_ = 0;

18
src/FileBrowser.cpp

@ -23,14 +23,6 @@ void FileBrowser::display()
cout << file_name << endl;
cout << long_separator << endl;
/*
//Display the lines
auto lines = buffer_.get_lines();
for (int i = 0; i < buffer_.window_height; ++i)
cout << std::setw(4) << i+1 << " " << lines[i] << endl;
delete[] lines;
*/
buffer_.display();
cout << long_separator << endl;
cout << " next previous open\n";
@ -42,13 +34,9 @@ void FileBrowser::execute_command(char command, bool & done)
{
switch (command) {
case 'b': {
<<<<<<< HEAD
if(!buffer_.back())
error_message_ = "No pervious links to open";
=======
buffer_.back();
>>>>>>> 9159d27c4f65480ce2606eeb041b3ce03a1b1048
break;
break;
}
case 'g': {
@ -70,10 +58,6 @@ void FileBrowser::execute_command(char command, bool & done)
}
break;
}
<<<<<<< HEAD
=======
>>>>>>> 9159d27c4f65480ce2606eeb041b3ce03a1b1048
case 'n': {
buffer_.move_to_next_page();
break;

Loading…
Cancel
Save