Browse Source

go and back functions :D

grissess
Sarah Inzerillo 3 years ago
parent
commit
ac9aa0df9c
  1. 6
      build.sh
  2. 1
      goshDarn.txt
  3. 2
      sarahTest.txt
  4. 2
      source.sh
  5. 72
      src/Buffer.cpp
  6. 1
      src/Buffer.h
  7. 6
      src/FileBrowser.cpp
  8. 9
      test.txt

6
build.sh

@ -1,5 +1,11 @@
#!/bin/bash
if [ ! -d obj ]; then
mkdir obj
fi
cd obj
g++ ../src/*.cpp -c
cd ..
if [ ! -d bin ]; then
mkdir bin
fi
g++ obj/*.o -o bin/FileBrowser

1
goshDarn.txt

@ -0,0 +1 @@
<p> yee haw couboy, you made it

2
sarahTest.txt

@ -0,0 +1,2 @@
<p> this is another freaking file!!
<p> here is another gosh darn <a goshDarn.txt flippin> file

2
source.sh

@ -1 +1 @@
alias run="make 1>/dev/null && ./FileBrowser"
alias run="./build.sh && bin/FileBrowser"

72
src/Buffer.cpp

@ -14,6 +14,29 @@ void Buffer::display() const
}
}
string* Buffer::get_lines() const
{
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;
}
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);
@ -25,9 +48,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))
{
// 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);
@ -37,45 +61,69 @@ bool Buffer::open(const string & new_file_name)
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 - 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 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))
{
std::string extra_text = line.substr(p_tag + 4);
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 is empty, then it shouldn't be added.
if(curr_p != "")
if(curr_1 != "")
{
v_lines_.push_back(curr_p);
curr_p = "";
curr_1 = "";
// 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 current paragraph being read.
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_1 += " " + 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;

1
src/Buffer.h

@ -21,6 +21,7 @@ public:
bool go(const int & link);
bool back();
bool open(const std::string & file_name);
std::string* get_lines() const;
void set_window_height(int h) { window_height_ = h; }
private:

6
src/FileBrowser.cpp

@ -9,7 +9,7 @@ void FileBrowser::display()
const string long_separator(50, '-');
const string short_separator(8, '-');
system("cls || clear");
//system("cls || clear");
if (!error_message_.empty()) {
cout << "ERROR: " + error_message_ << endl;
@ -21,8 +21,10 @@ void FileBrowser::display()
cout << "<no file opened>\n";
else
cout << file_name << endl;
cout << long_separator << endl;
auto lines = buffer_.get_lines();
delete[] lines;
buffer_.display();
cout << long_separator << endl;
cout << " next previous open\n";

9
test.txt

@ -1,6 +1,15 @@
<<<<<<< HEAD
<p> This is the first line in the file.
<p> This is the second <br> and third line of the file.
<p> This line has a link to the <a docs/specification.txt specification> file.
<p> This line is split into <p> two paragraph tags.
<p> This line contains some<br> problematic<p> tags.
<p> This has a link to sarahs <a sarahTest.txt anotherLink> file.
=======
<p>This is the first line in the file.
<p>This is the second <br> and third line of the file.
<p>This line has a link to the <a docs/specification.txt specification> file.
<p>This line is split into <p>two paragraph tags.
<p>This line con<br>tains some<br > problematic< p> tags.
Derp. Derp. Derp. Derp. Derp.
>>>>>>> d20176994a49dc433604ed3b1b96b8324f680782
Loading…
Cancel
Save