stew3254 3 years ago
parent
commit
2402848ec2
  1. 57
      src/Buffer.cpp

57
src/Buffer.cpp

@ -31,6 +31,17 @@ string* Buffer::get_lines() const
return lines;
}
auto get_tag(const string & line)
{
auto p_tag = line.find("<p>");
auto 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);
@ -42,14 +53,10 @@ bool Buffer::open(const string & new_file_name)
// opened successfully.
int curr_link = 0;
std::string curr_p, line;
std::string curr_l, line;
while(getline(file, line))
{
// Replace break tags with newlines. This is like HTML in that the tags are directly replaced with newline characters.
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;
@ -70,31 +77,41 @@ bool Buffer::open(const string & new_file_name)
line.replace(tag_loc, tag_len + 1, "<" + link_name + ">[" + to_string(curr_link) + "]");
}
// Search for all paragraphs in the line and add them to the v_lines vector.
for(auto p_tag = line.find("<p>"); p_tag != -1; p_tag = line.find("<p>"))
// 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))
{
// Seperate out text that should stay in the previous paragraph and text that goes in the new paragraph. Delete the tag in the process.
std::string extra_text = line.substr(p_tag + 3);
line.erase(p_tag);
curr_p += 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 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_p != "")
// 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_p);
curr_p = "";
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 current paragraph being read. Add a space so that content from two lines can be seperated.
curr_p += " " + line;
// 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 paragraph in the file.
v_lines_.push_back(curr_p);
// 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;

Loading…
Cancel
Save