Saturday, October 19, 2013

Program – Replace and Remove Lines

So far, only the insertion of program lines into the program had been implemented.  The removal  and replacement of program lines was implemented to complete the operations needed.  Some additional support functions were also needed.  The remove line routine just calls the remove routine of the QVector base class with the offset and size of the line if the size of the line if greater than zero (otherwise, no code needs to be deleted).

The replace line routine was much more involved.  If the replacement line size is zero, then the current line is deleted by calling the new remove routine.  If the replacement line is larger, then the program code vector is first resized for the net increase in the size of the line.  The code after the current line is then moved up by the net increase.  If the new line is smaller, then the code after the current line is moved down by the net decrease.  The program code vector is then resized by the net decrease in the line.  Finally, the contents of the replacement line is copied into the program.

The standard library memmove() function is used to move that program code, which is also used by the base QVector class.  The address of the code to move is obtained using the data() function of QVector (the data in the vector is guaranteed to be in continuous memory).  However, a word of caution learned while debugging: the data() function must be called after a call to the resize() function since this function may relocate the actual data.

When a line is inserted, removed or replaced with a different size line, the offset of every line after the line needs to be adjusted for the net change in program size.  To accomplish this, a new LineInfoList class was implemented based on the QList class.  The replace, insert and remove functions were reimplemented to adjust the offset of all lines after the affected line for the net change in program size.

[commit 49257e119d]

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)