hose pages are located all over the world.) Details The files in this web of files are plain text files that may contain anchors that provide links to other files. More precisely, an anchor is a string of the form 1 where filename is the name of another file and text is a piece of text. For example, here’s a line of text that contains an anchor: More is available. To keep things simple, assume that every anchor is separated from adjacent text by white space and that the strings filename and text cannot contain white space. Your browser should work exactly like the file viewer, except for the following: 3. The browser should have another new command called back that reopens the previous file. This requires the browser to keep a record of the files that have been visited. For example, if the user visits files A, B and then C, the back command would return to B. In other words, files
can you help with a C++ code for the following:
This is to create a prototype for a Web browser. You’ll do this by transforming the file viewer created (SHOWN BELOW) into a browser for a web of files that are all located on a single computer. (This is in contrast to the real World Wide Web whose pages are located all over the world.) Details The files in this web of files are plain text files that may contain anchors that provide links to other files. More precisely, an anchor is a string of the form 1 where filename is the name of another file and text is a piece of text. For example, here’s a line of text that contains an anchor: More is available. To keep things simple, assume that every anchor is separated from adjacent text by white space and that the strings filename and text cannot contain white space. Your browser should work exactly like the file viewer, except for the following:
3. The browser should have another new command called back that reopens the previous file. This requires the browser to keep a record of the files that have been visited. For example, if the user visits files A, B and then C, the back command would return to B. In other words, files should get added to the “history” only when they are visited by following a link (with the go command) or by using the open command, not by using the back command.
4. The browser should format the contents of each file as follows. Each file is assumed to consist of a sequence of “words” separated by white space. Note that, in this context, a word may include punctuation. For example, the following line contains six words: Each component performs one well−defined task. When a file is displayed, all the words should be separated by a single blank space and arranged so that the lines are as long as possible but no longer than a maximum length specified by the user. If ever a word is longer than that length, it should be displayed on a line by itself. The program should ask the user for the maximum line length right after it asks for the window height. The maximum line length should be a number of characters, not words.
5. Besides anchors, files can contain two other elements that affect how the text is displayed. The line-break element, written, causes a new line to begin immediately, even if the maximum line length hasn’t been 3 reached. The new-paragraph element, written, causes a new paragraph to be started. When displayed, consecutive paragraphs should be separated by a single blank line. Blank lines in the input file should be ignored. To keep things simple, assume that the and elements are separated from surrounding text by white space.
// WebBrowser.cpp
#include "WebBrowser.h"
using namespace std;
void WebBrowser::display()
{
const string long_separator(50, '-');
const string short_separator(8, '-');
system(clear_command);
if (!error_message_.empty()) {
cout << "ERROR: " + error_message_ << endl;
error_message_.clear();
}
string file_name = buffer_.file_name();
if (file_name.empty())
cout << "<no file opened>\n";
else
cout << file_name << endl;
cout << long_separator << endl;
buffer_.display_with_anchors();
cout << long_separator << endl;
cout << " go back open quit\n";
cout << short_separator << endl;
}
void WebBrowser::execute_command(char command, bool & done)
{
switch (command) {
case 'g': {
cout << "link number: ";
int link_number;
cin >> link_number;
cin.get(); // '\n'
string file_name = buffer_.get_link_filename(link_number);
if (!file_name.empty()) {
history_.push(buffer_.file_name());
buffer_.open(file_name);
} else {
error_message_ = "Invalid link number: " + to_string(link_number);
}
break;
}
case 'b': {
if (!history_.empty()) {
string previous_file_name = history_.top();
history_.pop();
buffer_.open(previous_file_name);
}
break;
}
case 'o': {
cout << "file name: ";
string file_name;
getline(cin, file_name);
if (!buffer_.open(file_name))
error_message_ = "Could not open " + file_name;
break;
}
case 'q': {
done = true;
break;
}
}
}
void WebBrowser::run()
{
cout << "Window height? ";
cin >> window_height_;
cin.get(); // '\n'
cout << '\n';
buffer_.set_window_height(window_height_);
cout << "Maximum line length? ";
cin >> max_line_length_;
cin.get(); // '\n'
cout << '\n';
buffer_.set_max_line_length(max_line_length_);
bool done = false;
while (!done) {
display();
cout << "command: ";
char command;
cin >> command;
cin.get(); // '\n'
execute_command(command, done);
cout << endl;
}
}
Step by step
Solved in 3 steps with 3 images