general questions

Can I modify textBuffer.h?

No.

Can I add my own structs and functions to textbuffer.c?

Yes! Make sure functions are declared static, and you document what the functions and structures you add are for.

Can I use functions from <string.h>?

Yes. It’s much, much harder if you don’t.

Will I need to check my code with Valgrind?

We’ll certainly be checking your submission with Valgrind for memory leaks.

Can TB be defined like link in the lecture examples?

If TB points directly to the head of the list, functions like mergeTB cannot function correctly, as they may change the head of the list.

If I’m about to abort(3), should I free(3) the textbuffer?

It’s a bad idea to clean up after yourself before abort(3)’ing if you have silly inputs, just in case the reason you got silly inputs spreads… Virtual memory also takes care of stopping memory leaks in this case.

newTB

How does newTB work?

If the input text is, for example, "Hi\nhow\nare\nthings\n\0", the buffer should contain the following lines: { "Hi", "how", "are", "things" }. You will have to process the input string, extract all the substrings separated by newline, and copy them into the entries of your buffer structure.

Should I leave the '\n' characters in?

Depending on your approach to splitting text, they may already be gone. The only other place you need the '\n' characters is in dumpTB, so you could probably get away without storing them…

Is it safe to assume that the text will always have a new line at the end?

Yes, text will always have a newline.

What should happen with multiple consecutive newlines?

Each newline marks a new node in the text buffer. You need to track empty lines.

Can I use strtok(3) or strsep(3)?

Yes! I (Jashank) recommend strsep(3), but both are valid. Note, however, that to use either, the input string needs to be mutable… and this isn’t guaranteed in the spec.

releaseTB

How should I write tests for releaseTB?

You cannot. You can’t write a black-box test for a destructor.

When you free(3) memory, all you’re saying is that you no longer need the block of memory you had a pointer to; it should be irrelevant to you whether that memory’s value changes or becomes invalid in some way, because YOU ARE ABSOLUTELY FORBIDDEN FROM ACCESSING THE MEMORY ONCE FREE’D. Use after free is an illegal and undefined operation. You have no way to invalidate the pointers (read: change any values outside your ADT, including outside pointer references to its state structure).

A good test that your releaseTB worked is that your program is still running after you do so.

One approach is to put a magic number at the beginning of your structure that indicates if it’s been releaseTB’d; if it has, you should abort(3) immediately because Something Very Bad Is Going On.

dumpTB

My textbuffer has no lines; what should dumpTB return?
NULL.

swapTB

Should I swap the string pointers or the whole nodes?

You should swap nodes, not the string pointers.

If somewhere in your textbuffer, you’ve got pointers to a particular node, and that node’s value changes, that node’s identity is lost.

Considering the context that we’re working on this data structure in – text editors – external code holding references inside your text buffer would get, understandably, quite upset if each node doesn’t persist its value.

With the exception of replaceText, each node’s value (in essence, the line of text it holds) should never, ever change.

mergeTB

What should happen if I mergeTB (tb1, 1, tb1)?

Attempts to merge a textbuffer with itself should be ignored.

Should I call releaseTB as well?

No! This will probably destroy both the source and destination textbuffers. However, you’ve moved the contents of the source textbuffer, so you can just free(3) as you would in releaseTB. You must not subsequently dereference it; that’s a use-after-free and (say it with me, folks!) use after free is illegal.

Can I concatenate text buffers with mergeTB?

The correct behaviour should be as follows, for mergeTB (dest, pos, src):

cutTB

What happens if I cut the whole textbuffer?
You have an empty textbuffer… and you give back a copy of the new textbuffer.

replaceText

Will str1 and str2 be the same size?

No. You might be replacing one string with a longer string and your original chunk of memory may not be big enough. You will need to solve this problem.

Should I search or replace the entire line or a substring?

You should search for, and replace, substrings.

Will the search or replacement strings include newlines?

No.

Are the search and replacement strings case-sensitive?

Yes.

What happens if I search for the empty string, ""?

Nothing is done in this situation. Nothing should be replaced.

What happens if I replace with the empty string?

Occurrences of the search string are removed.

diffTB

Does diffTB change either of its textbuffer arguments?
No. diffTB is non-destructive.

undoTB & redoTB

What is the effect of an undo of mergeTB?
You don’t have to re-create the original textbuffer; you have no way to give it back, anyway. Just reverse the effects of adding the new lines to the textbuffer.