You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.6 KiB

  1. // Buffer.cpp
  2. #include "Buffer.h"
  3. using namespace std;
  4. /*
  5. void Buffer::display() const
  6. {
  7. int ix_stop_line_ = ix_top_line_ + window_height;
  8. for (int i = ix_top_line_; i < ix_stop_line_; ++i) {
  9. if (i < v_lines_.size())
  10. cout << std::setw(6) << i+1 << " " << v_lines_[i];
  11. cout << '\n';
  12. }
  13. }
  14. */
  15. string* Buffer::get_lines() const
  16. {
  17. string* lines = new string[window_height];
  18. int line_num = 0;
  19. while (line_num < window_height && line_num + ix_top_line_ < v_lines_.size()) {
  20. lines[line_num] = v_lines_[line_num + ix_top_line_];
  21. //cout << v_lines_[line_num] << endl;
  22. ++line_num;
  23. }
  24. //cout << "----------" << endl << endl;;
  25. return lines;
  26. }
  27. auto get_tag(const string & line)
  28. {
  29. auto p_tag = line.find("<p>");
  30. auto b_tag = line.find("<br>");
  31. if (p_tag > b_tag)
  32. return b_tag;
  33. else
  34. return p_tag;
  35. }
  36. bool Buffer::open(const string & new_file_name)
  37. {
  38. std::ifstream file(new_file_name);
  39. if (!file)
  40. return false;
  41. v_lines_.clear();
  42. // Note: the vector is cleared only after we know the file
  43. // opened successfully.
  44. int curr_link = 0;
  45. std::string curr_l, line;
  46. while(getline(file, line))
  47. {
  48. // Find tags and process them.
  49. for(auto tag_loc = line.find("<a "); tag_loc != -1; tag_loc = line.find("<a", tag_loc + 1))
  50. {
  51. ++curr_link;
  52. // Find the length of the tag and pull out the data from the tag.
  53. auto tag_len = line.find_first_of('>', tag_loc) - tag_loc;
  54. std::string link_tag = line.substr(tag_loc + 3, tag_len - 3);
  55. // Seperate the link path and link name into seperate strings. Assuming no spaces in the link path.
  56. auto second_space_loc = link_tag.find_first_of(' ', 0);
  57. std::string file_name = link_tag.substr(0, second_space_loc - 1);
  58. std::string link_name = link_tag.substr(second_space_loc + 1);
  59. // Adds the link as a pair to a vector of links.
  60. v_links_.push_back({file_name, link_name});
  61. // Reformat the link tag to match specification.
  62. line.replace(tag_loc, tag_len + 1, "<" + link_name + ">[" + to_string(curr_link) + "]");
  63. }
  64. // Search for all paragraphs and breaks in the line and add them to the v_lines vector.
  65. for(auto _tag = get_tag(line); _tag != -1; _tag = get_tag(line))
  66. {
  67. // Check to see if we have a paragraph tag, so we can add a blank line.
  68. bool is_p = _tag == line.find("<p>");
  69. int tag_len = is_p ? 3 : 4;
  70. // Seperate out text that should stay in the previous line and text that goes in the new line. Delete the tag in the process.
  71. std::string extra_text = line.substr(_tag + tag_len);
  72. line.erase(_tag);
  73. curr_l += line;
  74. // 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.
  75. if(curr_l != "")
  76. {
  77. v_lines_.push_back(curr_l);
  78. curr_l = "";
  79. if(is_p)
  80. {
  81. v_lines_.push_back("");
  82. }
  83. }
  84. // Move any remaining data on the line back into the line variable so that it can be processed.
  85. line = extra_text;
  86. }
  87. // 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.
  88. curr_l += " " + line;
  89. }
  90. // Push the contents of curr_p as it has the last line in the file.
  91. v_lines_.push_back(curr_l);
  92. file_name_ = new_file_name;
  93. ix_top_line_ = 0;
  94. return true;
  95. }
  96. bool Buffer::go(int link)
  97. {
  98. string f_name = v_links_[link - 1].second;
  99. if(!open(f_name))
  100. return false;
  101. else
  102. return true;
  103. }
  104. void Buffer::back()
  105. {
  106. }