Browse Source

Modified Buffer code to format input and added the vector for storing links.

pull/1/head
Cameron Weinfurt 4 years ago
parent
commit
99d9b03c22
  1. 4
      build.sh
  2. 49
      src/Buffer.cpp
  3. 2
      src/Buffer.h

4
build.sh

@ -1,3 +1,5 @@
#!/bin/bash
g++ src/*.cpp -o obj/*.o
cd obj
g++ ../src/*.cpp -c
cd ..
g++ obj/*.o -o bin/FileBrowser

49
src/Buffer.cpp

@ -24,9 +24,52 @@ bool Buffer::open(const string & new_file_name)
// Note: the vector is cleared only after we know the file
// opened successfully.
string line;
while (getline(file, line))
v_lines_.push_back(line);
int curr_link = 0;
std::string curr_p, 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.
for(auto tag_loc = line.find("<a"); tag_loc != -1; tag_loc = line.find("<a", tag_loc + 1))
{
++curr_link;
auto tag_len = line.find_first_of('>', tag_loc) - tag_loc;
std::string link_tag = line.substr(tag_loc, tag_len);
auto second_space_loc = link_tag.find_first_of(' ', 4);
std::string file_name = link_tag.substr(4, second_space_loc - 1);
std::string link_name = link_tag.substr(second_space_loc, tag_len - 1);
v_links_.push_back({file_name, link_name});
line.replace(tag_loc, tag_len, "<" + link_name + ">[" + to_string(curr_link) + "]");
}
// See if there is a new paragraph being made and make one if it is.
auto p_tag = line.find("<p>");
std::string extra_text;
if(p_tag != -1)
{
extra_text = line.substr(p_tag + 4);
line.erase(p_tag);
curr_p += line;
v_lines_.push_back(curr_p);
curr_p = extra_text;
}
else // Otherwise just append the data on this line to the current paragraph being read.
{
curr_p += line;
}
}
file_name_ = new_file_name;
ix_top_line_ = 0;

2
src/Buffer.h

@ -5,6 +5,7 @@
#include <fstream>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
@ -21,6 +22,7 @@ public:
private:
std::vector<std::string> v_lines_;
std::vector<std::pair<std::string, std::string>> v_links_;
int ix_top_line_ = 0;
std::string file_name_;
int window_height_;

Loading…
Cancel
Save