Starting from:

$30

NWEN241-Assignment 1 Text Editor Solved

Part I: Pure C Programming

You may only use the Standard C Library to perform the tasks in this part. You should implement the functions in editor.c.

Task 1.

 

Basics

In this task, you will implement a function with prototype

 

int editor_insert_char(char *editing_buffer, char to_insert, int pos)

 

which will insert the character to_insert at index pos of editing_buffer. The size of editing_buffer is EDITING_BUFLEN. When a character is inserted at index pos, each of the original characters at index pos until the end of buffer must be moved by one position to the right. The last character is thrown out.

The function should return 1 if the character insertion occurred, otherwise it should return 0.

To clarify, suppose that EDITING_BUFLEN is defined to be 21 and the contents of editing_buffer are

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
After invoking

 

int r = editor_insert_char(editing_buffer, ’s’, 9);

 

the contents of editing_buffer should be

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
and the value of r is 1.

Task 2.

 

Basics

In this task, you will implement a function with prototype

 

int editor_delete_char(char *editing_buffer, char to_delete, int offset)

 

which will delete the first occurrence of the character to_delete. The search should start from index offset of editing_buffer. The size of editing_buffer is EDITING_BUFLEN. When a character is deleted at index pos, each of the original characters at index pos until the end of buffer must be moved by one position to the left. A null character is inserted at the end of the buffer.

The function should return 1 if the character deletion occurred, otherwise it should return 0.

To clarify, suppose that EDITING_BUFLEN is defined to be 21 and the contents of editing_buffer are

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
After invoking

 

int r = editor_delete_char(editing_buffer, ’f’, 10);

 

the contents of editing_buffer should be

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
and the value of r is 1.

Task 3.

 

Completion

In this task, you will implement a function with prototype

 

int editor_replace_str(char *editing_buffer, const char *str, const char *replacement, int offset)

 

which will replace the first occurrence of the string str with replacement. The search should start from index offset of editing_buffer. The size of editing_buffer is EDITING_BUFLEN.

The replacement should not overwrite other contents in the buffer. This means that if replacement is longer than str, there is a need move the characters after str to the right. Likewise, if replacement is shorter than str, there is a need move the characters after str to the left. When moving characters to the right, throw out characters that will not fit in the buffer. When moving characters to the left, insert null characters in the vacated positions.

If the replacement text will go beyond the limits of editing_buffer, then replacement should only occur until the end of editing_buffer.

If the string replacement occurred, the function should return the index corresponding the last letter of replacement in editing_buffer, otherwise, it should return -1. If the replacement text will go beyond the limits of editing_buffer, the function should return EDITING_BUFLEN-1.

To clarify, suppose that EDITING_BUFLEN is defined to be 21 and the contents of editing_buffer are

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
After invoking

 

int r = editor_replace_str(editing_buffer, "brown", "blue", 0);

 

the contents of editing_buffer should be

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
and the value of r should be 13 (which is the index of ’e’ in "blue").

Task 4.

 

Challenge

In this task, you will implement a function with prototype

 

void editor_view(char **viewing_buffer, const char *editing_buffer, int wrap)

 

which will essentially copy the contents of the editing_buffer to the viewing_buffer for display to the user. Note that the latter is a twodimensional array, with dimensions VIEWING_COLS columns and VIEWING_ROWS rows. VIEWING_COLS and VIEWING_ROWS are defined in editor.h. Prior to the copying, the function must set every character in the viewing_buffer to the null character.

The argument wrap controls the behaviour of the copying process from editing_buffer to viewing_buffer as follows:

•    Regardless of the value ofencountered, subsequent text after the newline charater is copied towrap, whenever a newline character is the next row. Note that the newline character itself is not copied to viewing_buffer.

•    Whenwhen the newline character iswrap is non-zero, the text must be wrapped. This means thatnot encountered before the end of the

current row (at column VIEWING_COLS-1 in viewing_buffer), subsequent text is copied to the next row. Note that column VIEWING_COLS-1 in viewing_buffer is never filled and will retain the null character.

•    Whennewline character iswrap is 0, the text is not wrapped. This means that when thenot encountered before the end of the current row

(at column VIEWING_COLS-1), succeeding text in the editing_buffer are discarded until a newline is encountered which will cause the succeeding text to be copied to the next row. Note that column VIEWING_COLS-1 in viewing_buffer is never filled and will retain the null character.

The copying process should terminate when a null character is encountered.

To clarify, suppose that EDITING_BUFLEN is defined to be 48 and the contents of editing_buffer are

 

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Suppose that VIEWING_COLS and VIEWING_ROWS are 11 and 8, respectively. After invoking

 

editor_view(viewing_buffer, editing_buffer, 1);

 

the resulting contents of viewing_buffer should be

 

 

editor_view(viewing_buffer, editing_buffer, 0);

 

the resulting contents of viewing_buffer should be

 

Part II: C++ Programming

You may use the C++ Standard Library to perform the tasks in this part.

Task 5.

 

Basics

In this task, you will define a class that extends the EditingBuffer class defined in editor.hh. You should declare the class in myeditor.hh and name it MyEditingBuffer. This class should be in same namespace as EditingBuffer. The access mode of the inherited members should be preserved.

You are free to declare additional member variables and functions that are necessary to perform the subsequent tasks. Provide sufficient comment, and ensure that these additional member variables and functions are not publicly accessible.

Task 6.

 

Basics

In this task, you will implement the member function

 

bool replace(char c, char replacement, int offset)

 

which will replace the first occurrence of the character c with replacement in the buffer. The search should start from index offset of buffer. The length of buffer is BUFFER_LEN which is defined in editor.hh.

The function should return true if the character insertion occurred, otherwise false.

To clarify, suppose that BUFFER_LEN is defined to be 21 and the contents of buffer are

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
After invoking

 

MyEditingBuffer meb; ...

bool r = meb.replace(’b’, ’B’, 5);

 

the resulting contents of the buffer should be

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
and the value of r should true.

You should implement this member function in myeditor.cc.

Task 7.

 

Completion

In this task, you will implement the member function

 

int replace(std::string str, std::string replacement, int offset)

 

which will replace the first occurrence of the string str with replacement. The search should start from index offset of buffer.

The replacement should not overwrite other contents in the buffer. This means that if replacement is longer than str, there is a need move the characters after str to the right. Likewise, if replacement is shorter than str, there is a need move the characters after str to the left. When moving characters to the right, throw out characters that will not fit in the buffer. When moving characters to the left, insert null characters in the vacated positions.

If the replacement text will go beyond the limits of buffer, then replacement should only occur until the end of buffer.

If the string replacement occurred, the function should return the index corresponding the last letter of str in editing_buffer, otherwise, it should return -1. If the replacement text will go beyond the limits of buffer, the function should return BUFFER_LEN-1.

To clarify, suppose that BUFFER_LEN is defined to be 21 and the contents of buffer are

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
After invoking

 

MyEditingBuffer meb; ... std::string s("brown"); std::string t("violet"); int r = meb.replace(s, t, 8);

 

the resulting contents of the buffer should be

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
and the value of r should be 15 (which is the index of ’t’ in "violet"). You should implement this member function in myeditor.cc.

Task 8.

 

Challenge

In this task, you will implement the member function

 

void justify(char **viewingBuffer, int rows, int cols)

 

which will justify the contents of buffer by adjusting the space in between words. The justified text should be stored in viewingBuffer, a two-dimensional array which has rows rows and cols columns.

To implement the text justification, the contents of buffer must first be copied to viewingBuffer such that the copied text is wrapped. You may reuse your implementation in Task 4 for this purpose.

After copying, the contents of viewingBuffer must be justified, i.e., the character at column cols-2 of every row must not be a whitespace if possible. This can be done by adding whitespaces in between words. When justifying, follow these guidelines:

1.   Do not justify empty rows (rows consisting of null characters) and single-word rows.

2.   Do not split words, i.e., do not insert spaces in between the letters of a word.

3.   You can split a single-word row if the single word is too long to fit one row.

4.   Do not merge words.

5.    

 
 
 

If necessary, discard characters that will not fit in the buffer. 

To clarify, suppose that BUFFER_LEN is defined to be 48 and the contents of buffer are

 

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
After invoking

 

MyEditingBuffer meb; ... meb.justify(viewingBuffer, 8, 11);

the resulting contents of viewingBuffer should be

 

You should implement this member function in myeditor.cc.

Part III: Systems Program Design

In Parts I and II, you implemented some functionality for manipulating the contents of the editing buffer. In this part, you will design a C++ program for manipulating the viewing buffer.

Task 9.

 

Challenge

The viewing buffer is a block of computer memory that contains part of the text document shown in the computer display. It can be viewed as a twodimensional array of characters, where the each row corresponds to a line of text. In practice, the number of rows and columns should be determined by the size of the display/terminal. For simplicity, fix the number of rows and columns to 25 and 80, respectively.

In designing the program, assume that a main buffer contains the entire text document being edited. Assume that the main buffer variable is global and is declared as char main_buffer[1000000] somewhere.

The following functionality are required to manipulate the viewing buffer:

•    Load parts of text document from main buffer to the viewing buffer(from a given line/row position).

•    Scroll up viewing buffer content by a given number of lines/rows.

•    Scroll down viewing buffer content by a given number of lines/rows.

•    Enable wrapping

•    Disable wrapping

•    Show line numbering

•    Hide line numbering

More products