Browse Source

Finished split lines function

stew
stew3254 3 years ago
parent
commit
d051435adc
  1. 11
      debug.sh
  2. 105
      src/Buffer.cpp
  3. 11
      src/Buffer.h
  4. 8
      src/FileBrowser.cpp
  5. 24
      test.txt

11
debug.sh

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

105
src/Buffer.cpp

@ -1,4 +1,4 @@
// Buffer.cpp
//Buffer.cpp
#include "Buffer.h"
@ -15,39 +15,18 @@ void Buffer::display() const
}
}
/*
string* Buffer::get_lines() const
{
string* lines = new string[window_height_];
int line_num = 0;
while (line_num < window_height_ && line_num + ix_top_line_ < v_lines_.size()) {
lines[line_num] = v_lines_[line_num + ix_top_line_];
//cout << v_lines_[line_num] << endl;
++line_num;
}
//cout << "----------" << endl << endl;;
return lines;
}
*/
vector<string> Buffer::split_line(string & str) {
//Initialize the substring and set the positions in the string where they need to exist
//Pass the string by copy because I don't want to change the original, but still need to manipulate it
vector<string> Buffer::split_line(string str) {
vector<string> v = {};
string str_part = str;
size_t curr_pos = str_part.find(" ");
size_t total_pos = curr_pos;
bool searching = true;
// Split up the lines
//Split up the lines
while (str.length() > maximum_length_)
{
//Re-initialize the substring and set the positions in the string where they need to exist
str_part = str;
curr_pos = str_part.find(" ");
total_pos = curr_pos;
searching = true;
//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;
//While searching for the next space in a string
while (searching)
@ -58,14 +37,6 @@ vector<string> Buffer::split_line(string & str) {
{
searching = false;
}
//Check
else if (curr_pos > maximum_length_)
{
searching = false;
total_pos = maximum_length_;
str = str.substr(0, total_pos - 1) + "- " + str.substr(total_pos - 1, str.length());
cout << str << endl;
}
else if (total_pos + curr_pos + 1 > maximum_length_)
{
searching = false;
@ -75,13 +46,24 @@ vector<string> Buffer::split_line(string & str) {
total_pos += curr_pos + 1;
}
}
str_part = str.substr(0, total_pos);
str = str.substr(total_pos + 1, str.length());
v.push_back(str_part);
//Check if the string part can still be chopped
if (str_part.length() != str.length()) {
str_part = str.substr(0, total_pos);
cout << str_part << endl;
str = str.substr(total_pos + 1, str.length());
v.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;
}
}
v.push_back(str);
//for (auto e: v)
// cout << e << endl;
return v;
}
@ -104,49 +86,49 @@ bool Buffer::open(const string & new_file_name)
return false;
v_lines_.clear();
// Note: the vector is cleared only after we know the file
// opened successfully.
//Note: the vector is cleared only after we know the file
//opened successfully.
int curr_link = 0;
std::string curr_line, line;
while(getline(file, line))
{
// Find 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.
//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.
//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 - 1);
std::string link_name = link_tag.substr(second_space_loc + 1);
// Adds the link as a pair to a vector of links.
//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.
//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 and add them to the v_lines vector.
//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))
{
// Check to see if we have a paragraph tag, so we can add a blank 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;
// Separate out text that should stay in the previous line and text that goes in the new line. Delete the tag in the process.
//Separate 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_line += line;
// 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.
//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_line != "")
{
//TODO This is causing some issues
@ -159,28 +141,29 @@ bool Buffer::open(const string & new_file_name)
}
}
// Move any remaining data on the line back into the line variable so that it can be processed.
//Move any remaining data on the line back into the line variable so that it can be processed.
line = extra_text;
}
curr_line += line;
// TODO: What is this section
//Checks if the current line length is greater than the max,
//then splits it up and adds it to the vector if it is
if (curr_line.length() > maximum_length_) {
vector<string> v_split_line = split_line(curr_line);
for (auto e: v_split_line) {
v_lines_.push_back(e);
}
curr_line = "";
curr_line = "";
}
}
// Push the contents of curr_p as it has the last line in the file.
//Push the contents of curr_p as it has the last line in the file.
v_lines_.push_back(curr_line);
ix_top_line_ = 0;
ix_top_line_ = 0;
file_name_ = new_file_name;
return true;
return true;
}
bool Buffer::go(int link)

11
src/Buffer.h

@ -17,18 +17,17 @@ public:
void display() const;
const std::string & file_name() const { return file_name_; }
bool go(int link);
int max_links();
long max_links() {return v_links_.size(); };
void move_to_next_page();
void move_to_previous_page();
bool open(const std::string & file_name);
//std::string* get_lines() const;
void set_maximum_length(int l) { maximum_length_ = l; }
void set_window_height(int h) { window_height_ = h; }
private:
std::size_t get_tag_(const std::string & line);
std::string file_name_;
std::vector<std::string> split_line(std::string & str);
std::vector<std::string> split_line(std::string str);
std::vector<std::string> v_lines_;
std::vector<std::pair<std::string, std::string>> v_links_;
int ix_top_line_ = 0;
@ -50,10 +49,4 @@ inline void Buffer::move_to_previous_page()
ix_top_line_ = 0;
}
inline int Buffer::max_links()
{
//return v_links.length();
return 1;
}
#endif

8
src/FileBrowser.cpp

@ -23,14 +23,6 @@ void FileBrowser::display()
cout << file_name << endl;
cout << long_separator << endl;
/*
//Display the lines
auto lines = buffer_.get_lines();
for (int i = 0; i < buffer_.window_height; ++i)
cout << std::setw(4) << i+1 << " " << lines[i] << endl;
delete[] lines;
*/
buffer_.display();
cout << long_separator << endl;
cout << " next previous open\n";

24
test.txt

@ -1,35 +1,35 @@
<p>This is the first line in the file.
<p>This is the second <br> and third line of 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 is split into<p>two paragraph tags.
<p>This line con<br>tains some<br > problematic< p> tags.
Derp. Derp. Derp. Derp. Derp.
<p>This is the first line in the file.
<p>This is the second <br> and eighth line of the file.
<p>This is the second<br>and eighth 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.
<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.
<p>This is the first line in the file.
<p>This is the second <br> and fourteenth line of the file.
<p>This is the second<br>and fourteenth 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 is split into<p>two paragraph tags.
<p>This line con<br>tains some<br > problematic< p> tags.
Derp. Derp. Derp. Derp. Derp.
<p>This is the first line in the file.
<p>This is the second <br> and twentieth line of the file.
<p>This is the second<br>and twentieth 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 is split into<p>two paragraph tags.
<p>This line con<br>tains some<br > problematic< p> tags.
Derp. Derp. Derp. Derp. Derp.
<p>This is the first line in the file.
<p>This is the second <br> and twenty sixth line of the file.
<p>This is the second<br>and twenty sixth 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 is split into<p>two paragraph tags.
<p>This line con<br>tains some<br > problematic< p> tags.
Derp. Derp. Derp. Derp. Derp.
<p>This is the first line in the file.
<p>This is the second <br> and thirty second line of the file.
<p>This is the second<br>and thirty second 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.

Loading…
Cancel
Save