Browse Source

Fixed comments

master
stew3254 2 years ago
parent
commit
366ab02726
  1. 259
      src/Buffer.cpp

259
src/Buffer.cpp

@ -7,62 +7,61 @@ 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';
}
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';
}
}
//Pass the string by copy because we 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;
// TODO: Explain this loop in a tiny bit of detail
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. TODO: Add what is happening after the check.
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;
}
}
// TODO: What is being pushed and why?
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;
//Searches for the last space between words that is less than the maximum length.
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, and then push it back to the buffer for display.
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.
else
{
break;
}
}
//After the string has been parsed, add to to our buffer.
v_lines_.push_back(str);
}
//Finds the next paragraph or break tag in the line provided and returns the position of it.
@ -79,85 +78,85 @@ size_t Buffer::get_tag_(const string & line)
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 vectors are cleared only after we know the file
//opened successfully.
int curr_link = 0;
std::string curr_line, line;
while(getline(file, line))
{
//Find anchor 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. These two tags mark where a line in the viewer is defined before spliting, so they will be given to the split_line function which will push each segment of the line onto 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 know how big the current tag being processed is..
bool is_p = _tag == line.find("<p>");
int tag_len = is_p ? 3 : 4;
//Separate out text that should stay in the line to be split and text that goes in the next line. Delete the tag in the process.
std::string extra_text = line.substr(_tag + tag_len);
line.erase(_tag);
curr_line += line;
//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 = "";
//Add a blank line if a paragraph tag was just processed.
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 in the next line.
line = extra_text;
}
//Add any data that has been unprocessed up to this point.
curr_line += line;
}
// The contents of curr_line are the final parts of the last paragraph, so there will be no break or paragraph tags to push it onto the vector. The line is told to be processed here to allow this final content to get added in.
split_line(curr_line);
ix_top_line_ = 0;
file_name_ = new_file_name;
//If we used the open or go command to open a file, we should add this to the history vector.
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 vectors are cleared only after we know the file
//opened successfully.
int curr_link = 0;
std::string curr_line, line;
while(getline(file, line))
{
//Find anchor 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. These two tags mark where a line in the viewer is defined before spliting, so they will be given to the split_line function which will push each segment of the line onto 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 know how big the current tag being processed is..
bool is_p = _tag == line.find("<p>");
int tag_len = is_p ? 3 : 4;
//Separate out text that should stay in the line to be split and text that goes in the next line. Delete the tag in the process.
std::string extra_text = line.substr(_tag + tag_len);
line.erase(_tag);
curr_line += line;
//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 = "";
//Add a blank line if a paragraph tag was just processed.
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 in the next line.
line = extra_text;
}
//Add any data that has been unprocessed up to this point.
curr_line += line;
}
//The contents of curr_line are the final parts of the last paragraph, so there will be no break or paragraph tags to push it onto the vector. The line is told to be processed here to allow this final content to get added in.
split_line(curr_line);
ix_top_line_ = 0;
file_name_ = new_file_name;
//If we used the open or go command to open a file, we should add this to the history vector.
if(add_to_hist_)
{
v_hist_.push_back(file_name_);
curr_link_itr = v_hist_.end() - 1;
}
return true;
}
bool Buffer::go(int link)

Loading…
Cancel
Save