SlideShare a Scribd company logo
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);
When this function is called, it should start searching from the current cursor position, looking
for the next occurrence of the character oldch in the rest of the buffer.
If it finds it, simply replace oldch with newch. Search should leave the cursor after the last
character replaced in the buffer.
If oldch does not occur between the cursor and the end of the buffer, then there is nothing to
replace and thus search should leave the cursor unchanged (in the original place)
int SearchStrBuffer(bufferADT buffer, char* str);
When this function is called, it should start searching from the current cursor position, looking
for the next occurrence of the string str in the rest of the buffer.
If it finds it, search should leave the cursor after the found string and return the value TRUE or 1
If the string str does not occur between the cursor and the end of the buffer, then search should
leave the cursor unchanged and return FALSE or 0
First, you need to add the above prototypes into buffer.h.
Second, you need to implement them in listbuf.c
Third, you need to update editor.c so that you can call those functions. So include the following
in editor.c:
o If user enters: Rxy, your editor.c should call ReplaceCharInBuffer (buffer, 'x', 'y') to replace
all the occurrences of character 'x' with character 'y' after the cursor.
o If user enters: Sxyz, your editor.c should call SearchStrBuffer (buffer, "xyz") to search string
"xyz" in the buffer after the cursor.
Solution
/*
* File: editor.c
* --------------
* This program implements a simple character editor, which
* is used to test the buffer abstraction. The editor reads
* and executes simple commands entered by the user.
*/
#include
#include
#include "genlib.h"
#include "buffer.h"
#include "simpio.h"
#include "string.h"
/* Private function prototypes */
static void ExecuteCommand(bufferADT buffer, string line);
static void HelpCommand(void);
/* Main program */
main()
{
bufferADT buffer;
buffer = NewBuffer();
while (TRUE) {
printf("*");
ExecuteCommand(buffer, GetLine());
DisplayBuffer(buffer);
}
FreeBuffer(buffer);
}
/*
* Function: ExecuteCommand
* Usage: ExecuteCommand(buffer, line);
* ------------------------------------
* This function parses the user command in the string line
* and executes it on the buffer.
*/
static void ExecuteCommand(bufferADT buffer, string line)
{
int i;
switch (toupper(line[0])) {
case 'R':
if(strlen(line) >= 3) {
ReplaceCharInBuffer(buffer, line[1], line[2]);
} else {
printf("Illegal command ");
}
break;
case 'S':
SearchStrBuffer(buffer, &line[1]);
break;
case 'I': for (i = 1; line[i] != '0'; i++) {
InsertCharacter(buffer, line[i]);
}
break;
case 'D': DeleteCharacter(buffer); break;
case 'F': MoveCursorForward(buffer); break;
case 'B': MoveCursorBackward(buffer); break;
case 'J': MoveCursorToStart(buffer); break;
case 'E': MoveCursorToEnd(buffer); break;
case 'H': HelpCommand(); break;
case 'Q': exit(0);
default: printf("Illegal command "); break;
}
}
/*
* Function: HelpCommand
* Usage: HelpCommand();
* ---------------------
* This function lists the available editor commands.
*/
static void HelpCommand(void)
{
printf("Use the following commands to edit the buffer: ");
printf(" I... Inserts text up to the end of the line ");
printf(" F Moves forward a character ");
printf(" B Moves backward a character ");
printf(" J Jumps to the beginning of the buffer ");
printf(" E Jumps to the end of the buffer ");
printf(" D Deletes the next character ");
printf(" R Replaces the specified character with the new character ");
printf(" S Searches to find the text up to the end of line ");
printf(" H Generates a help message ");
printf(" Q Quits the program ");
}
buffer.h
/*
* File: buffer.h
* --------------
* This file defines the interface for an editor buffer abstraction.
*/
#ifndef _buffer_h
#define _buffer_h
#include "genlib.h"
/*
* Type: bufferADT
* ---------------
* This type defines the abstract type used to represent
* an editor buffer.
*/
typedef struct bufferCDT *bufferADT;
/* Exported entries */
/*
* Function: NewBuffer
* Usage: buffer = NewBuffer();
* ----------------------------
* This function dynamically allocates enough memory for the
* underlying representation of a bufferADT and initializes
* it to represent an empty buffer.
*/
bufferADT NewBuffer(void);
/*
* Function: FreeBuffer
* Usage: FreeBuffer(buffer);
* --------------------------
* This function frees the storage associated with the buffer.
*/
void FreeBuffer(bufferADT buffer);
/*
* Functions: MoveCursorForward, MoveCursorBackward
* Usage: MoveCursorForward(buffer);
* MoveCursorBackward(buffer);
* ----------------------------------
* These functions move the cursor forward or backward one
* character, respectively. If you call MoveCursorForward
* at the end of the buffer or MoveCursorBackward at the
* beginning, the function call has no effect.
*/
void MoveCursorForward(bufferADT buffer);
void MoveCursorBackward(bufferADT buffer);
/*
* Functions: MoveCursorToStart, MoveCursorToEnd
* Usage: MoveCursorToStart(buffer);
* MoveCursorToEnd(buffer);
* -------------------------------
* These functions move the cursor to the start or the
* end of the buffer, respectively.
*/
void MoveCursorToStart(bufferADT buffer);
void MoveCursorToEnd(bufferADT buffer);
/*
* Function: InsertCharacter
* Usage: InsertCharacter(buffer, ch);
* -----------------------------------
* This function inserts the single character ch into the
* buffer at the current cursor position. The cursor is
* positioned after the inserted character, which allows
* for consecutive insertions.
*/
void InsertCharacter(bufferADT buffer, char ch);
/*
* Function: DeleteCharacter
* Usage: DeleteCharacter(buffer);
* -------------------------------
* This function deletes the character immediately after
* the cursor. If the cursor is at the end of the buffer,
* this function has no effect.
*/
void DeleteCharacter(bufferADT buffer);
/*
* Function: DisplayBuffer
* Usage: DisplayBuffer(buffer);
* -----------------------------
* This function displays the current contents of the buffer
* on the console.
*/
void DisplayBuffer(bufferADT buffer);
/*
* Function: ReplaceCharInBuffer
* Usage: ReplaceCharInBuffer(buffer, oldch, newch);
* -----------------------------
*/
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);
/*
* Function: SearchStrBuffer
* Usage: SearchStrBuffer(buffer, str);
* -----------------------------
*/
int SearchStrBuffer(bufferADT buffer, char* str);
#endif
listbuf.c
/*
* File: listbuf.c
* ---------------
* This file implements the buffer.h abstraction using a linked
* list to represent the buffer.
*/
#include
#include "genlib.h"
#include "strlib.h"
#include "buffer.h"
/* Types */
typedef struct cellT {
char ch;
struct cellT *link;
} cellT;
struct bufferCDT {
cellT *start;
cellT *cursor;
};
/*
* Implementation notes: NewBuffer
* -------------------------------
* This function allocates an empty editor buffer, represented
* as a linked list. To simplify the link list operation, this
* implementation adopts the useful programming tactic of
* keeping an extra "dummy" cell at the beginning of each list,
* so that the empty buffer has the following representation:
*
* +-------+ +------+
* | o---+-----====>| |
* +-------+ / +------+
* | o---+---/ | NULL |
* +-------+ +------+
*/
bufferADT NewBuffer(void)
{
bufferADT buffer;
buffer = New(bufferADT);
buffer->start = buffer->cursor = New(cellT *);
buffer->start->link = NULL;
return (buffer);
}
/*
* Implementation notes: FreeBuffer
* --------------------------------
* FreeBuffer must free every cell in the buffer as well as
* the buffer storage itself. Note that the loop structure
* is not exactly the standard idiom for processing every
* cell within a linked list, because it is not legal to
* free a cell and later look at its link field. To avoid
* selecting fields in the structure after it has been freed,
* you have to copy the link pointer before calling FreeBlock.
*/
void FreeBuffer(bufferADT buffer)
{
cellT *cp, *next;
cp = buffer->start;
while (cp != NULL) {
next = cp->link;
FreeBlock(cp);
cp = next;
}
FreeBlock(buffer);
}
void MoveCursorForward(bufferADT buffer)
{
if (buffer->cursor->link != NULL) {
buffer->cursor = buffer->cursor->link;
}
}
void MoveCursorBackward(bufferADT buffer)
{
cellT *cp;
if (buffer->cursor != buffer->start) {
cp = buffer->start;
while (cp->link != buffer->cursor) {
cp = cp->link;
}
buffer->cursor = cp;
}
}
void MoveCursorToStart(bufferADT buffer)
{
buffer->cursor = buffer->start;
}
void MoveCursorToEnd(bufferADT buffer)
{
while (buffer->cursor->link != NULL) {
MoveCursorForward(buffer);
}
}
void InsertCharacter(bufferADT buffer, char ch)
{
cellT *cp;
cp = New(cellT *);
cp->ch = ch;
cp->link = buffer->cursor->link;
buffer->cursor->link = cp;
buffer->cursor = cp;
}
void DeleteCharacter(bufferADT buffer)
{
cellT *cp;
if (buffer->cursor->link != NULL) {
cp = buffer->cursor->link;
buffer->cursor->link = cp->link;
FreeBlock(cp);
}
}
void DisplayBuffer(bufferADT buffer)
{
cellT *cp;
for (cp = buffer->start->link; cp != NULL; cp = cp->link) {
printf(" %c", cp->ch);
}
printf(" ");
for (cp = buffer->start; cp != buffer->cursor; cp = cp->link) {
printf(" ");
}
printf("^ ");
}
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch) {
cellT *cp;
for(cp = buffer->cursor; cp != NULL; cp = cp->link) {
if(cp->ch == oldch) {
cp->ch = newch;
buffer->cursor = cp;
return;
}
}
}
int SearchStrBuffer(bufferADT buffer, char *str) {
cellT *cp, *tmp;
int i;
for(cp = buffer->cursor; cp != NULL; cp = cp->link) {
tmp = cp;
i = 0;
while(tmp) {
if(tmp->ch == str[i++]) {
if(str[i] == '0') {
buffer->cursor = tmp;
return TRUE;
} else {
tmp = tmp->link;
}
} else {
break; // No point in searching further.
}
}
}
// If this point is reached, then the string was not found.
return FALSE;
}

More Related Content

PDF
The Task For this assignment you will write a rudimentary text edi.pdf
PDF
Write a C++ program 1. Study the function process_text() in file.pdf
PDF
Program In C You are required to write an interactive C program that.pdf
PDF
String
PPT
String & its application
PPTX
Keypoints c strings
PDF
C programming Create a system managing a mini library system. Eve.pdf
The Task For this assignment you will write a rudimentary text edi.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
Program In C You are required to write an interactive C program that.pdf
String
String & its application
Keypoints c strings
C programming Create a system managing a mini library system. Eve.pdf

Similar to void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf (20)

DOCX
Design problem
PPT
CS201- Introduction to Programming- Lecture 17
PPT
Unit5 C
PDF
need help with my computer science lab assignemnt. this assignment i.pdf
DOC
35787646 system-software-lab-manual
PPTX
Presentation of file handling in C language
PPT
File handling in c
DOCX
Compiler Design and Construction COSC 5353Project Instructions -
PDF
ANSI C REFERENCE CARD
DOCX
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
PDF
2Bytesprog2 course_2014_c1_sets
PDF
2Bytesprog2 course_2014_c3_txtfiles
PDF
IN C++ languageWrite a simple word processor that will accept text.pdf
KEY
Sbaw091006
PPTX
Programming Fundamentals lecture-22.pptx
PPTX
String handling
DOCX
Mouse programming in c
PDF
Buffer manager
PPT
strings
Design problem
CS201- Introduction to Programming- Lecture 17
Unit5 C
need help with my computer science lab assignemnt. this assignment i.pdf
35787646 system-software-lab-manual
Presentation of file handling in C language
File handling in c
Compiler Design and Construction COSC 5353Project Instructions -
ANSI C REFERENCE CARD
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c3_txtfiles
IN C++ languageWrite a simple word processor that will accept text.pdf
Sbaw091006
Programming Fundamentals lecture-22.pptx
String handling
Mouse programming in c
Buffer manager
strings
Ad

More from arihantmobilepoint15 (20)

PDF
In a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdf
PDF
I need help with questions 7 and 8 I think what I circled is correct.pdf
PDF
How to write main program for converting infix to postfix that allow.pdf
PDF
How can I upload a picture in here I tried with image properties ic.pdf
PDF
For some DNAs it is possible to separate the two strands, after dena.pdf
PDF
Explain how stem cells obtained from IVF leftovers and somatic cell .pdf
PDF
Does it appear that there is a difference in mean population size....pdf
PDF
Discussion Post The Constitution 19 19 Think about the discussion in.pdf
PDF
Disease data for two independent groups are summarized into 2 by 2 t.pdf
PDF
Darwinian evolution, aka Darwinism, was merged with population g.pdf
PDF
Determine the theoretical probability of flipping a TAIL on a coin. .pdf
PDF
Construct a java method.   This method chooses the number of sti.pdf
PDF
Complete the sentences with the correct terms. Solution1. Phot.pdf
PDF
BIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdf
PDF
Can a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdf
PDF
All chordates have while all vertebrates have homeothermya notochor.pdf
PDF
6. What factor describes the overall thermal performance of a buildin.pdf
PDF
8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf
PDF
After reading about the history of welfare in the USA, describe in y.pdf
PDF
You are examining a copolymer for its potential as a material for .pdf
In a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdf
I need help with questions 7 and 8 I think what I circled is correct.pdf
How to write main program for converting infix to postfix that allow.pdf
How can I upload a picture in here I tried with image properties ic.pdf
For some DNAs it is possible to separate the two strands, after dena.pdf
Explain how stem cells obtained from IVF leftovers and somatic cell .pdf
Does it appear that there is a difference in mean population size....pdf
Discussion Post The Constitution 19 19 Think about the discussion in.pdf
Disease data for two independent groups are summarized into 2 by 2 t.pdf
Darwinian evolution, aka Darwinism, was merged with population g.pdf
Determine the theoretical probability of flipping a TAIL on a coin. .pdf
Construct a java method.   This method chooses the number of sti.pdf
Complete the sentences with the correct terms. Solution1. Phot.pdf
BIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdf
Can a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdf
All chordates have while all vertebrates have homeothermya notochor.pdf
6. What factor describes the overall thermal performance of a buildin.pdf
8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf
After reading about the history of welfare in the USA, describe in y.pdf
You are examining a copolymer for its potential as a material for .pdf
Ad

Recently uploaded (20)

PDF
Trump Administration's workforce development strategy
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Classroom Observation Tools for Teachers
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
RMMM.pdf make it easy to upload and study
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Pharma ospi slides which help in ospi learning
PDF
Computing-Curriculum for Schools in Ghana
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Lesson notes of climatology university.
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Trump Administration's workforce development strategy
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
human mycosis Human fungal infections are called human mycosis..pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Classroom Observation Tools for Teachers
Yogi Goddess Pres Conference Studio Updates
O5-L3 Freight Transport Ops (International) V1.pdf
Complications of Minimal Access Surgery at WLH
O7-L3 Supply Chain Operations - ICLT Program
Chinmaya Tiranga quiz Grand Finale.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
RMMM.pdf make it easy to upload and study
Weekly quiz Compilation Jan -July 25.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Pharma ospi slides which help in ospi learning
Computing-Curriculum for Schools in Ghana
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Lesson notes of climatology university.
Supply Chain Operations Speaking Notes -ICLT Program

void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf

  • 1. void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch); When this function is called, it should start searching from the current cursor position, looking for the next occurrence of the character oldch in the rest of the buffer. If it finds it, simply replace oldch with newch. Search should leave the cursor after the last character replaced in the buffer. If oldch does not occur between the cursor and the end of the buffer, then there is nothing to replace and thus search should leave the cursor unchanged (in the original place) int SearchStrBuffer(bufferADT buffer, char* str); When this function is called, it should start searching from the current cursor position, looking for the next occurrence of the string str in the rest of the buffer. If it finds it, search should leave the cursor after the found string and return the value TRUE or 1 If the string str does not occur between the cursor and the end of the buffer, then search should leave the cursor unchanged and return FALSE or 0 First, you need to add the above prototypes into buffer.h. Second, you need to implement them in listbuf.c Third, you need to update editor.c so that you can call those functions. So include the following in editor.c: o If user enters: Rxy, your editor.c should call ReplaceCharInBuffer (buffer, 'x', 'y') to replace all the occurrences of character 'x' with character 'y' after the cursor. o If user enters: Sxyz, your editor.c should call SearchStrBuffer (buffer, "xyz") to search string "xyz" in the buffer after the cursor. Solution /* * File: editor.c * -------------- * This program implements a simple character editor, which * is used to test the buffer abstraction. The editor reads * and executes simple commands entered by the user. */ #include #include #include "genlib.h" #include "buffer.h"
  • 2. #include "simpio.h" #include "string.h" /* Private function prototypes */ static void ExecuteCommand(bufferADT buffer, string line); static void HelpCommand(void); /* Main program */ main() { bufferADT buffer; buffer = NewBuffer(); while (TRUE) { printf("*"); ExecuteCommand(buffer, GetLine()); DisplayBuffer(buffer); } FreeBuffer(buffer); } /* * Function: ExecuteCommand * Usage: ExecuteCommand(buffer, line); * ------------------------------------ * This function parses the user command in the string line * and executes it on the buffer. */ static void ExecuteCommand(bufferADT buffer, string line) { int i; switch (toupper(line[0])) { case 'R': if(strlen(line) >= 3) { ReplaceCharInBuffer(buffer, line[1], line[2]); } else { printf("Illegal command "); } break; case 'S':
  • 3. SearchStrBuffer(buffer, &line[1]); break; case 'I': for (i = 1; line[i] != '0'; i++) { InsertCharacter(buffer, line[i]); } break; case 'D': DeleteCharacter(buffer); break; case 'F': MoveCursorForward(buffer); break; case 'B': MoveCursorBackward(buffer); break; case 'J': MoveCursorToStart(buffer); break; case 'E': MoveCursorToEnd(buffer); break; case 'H': HelpCommand(); break; case 'Q': exit(0); default: printf("Illegal command "); break; } } /* * Function: HelpCommand * Usage: HelpCommand(); * --------------------- * This function lists the available editor commands. */ static void HelpCommand(void) { printf("Use the following commands to edit the buffer: "); printf(" I... Inserts text up to the end of the line "); printf(" F Moves forward a character "); printf(" B Moves backward a character "); printf(" J Jumps to the beginning of the buffer "); printf(" E Jumps to the end of the buffer "); printf(" D Deletes the next character "); printf(" R Replaces the specified character with the new character "); printf(" S Searches to find the text up to the end of line "); printf(" H Generates a help message "); printf(" Q Quits the program "); }
  • 4. buffer.h /* * File: buffer.h * -------------- * This file defines the interface for an editor buffer abstraction. */ #ifndef _buffer_h #define _buffer_h #include "genlib.h" /* * Type: bufferADT * --------------- * This type defines the abstract type used to represent * an editor buffer. */ typedef struct bufferCDT *bufferADT; /* Exported entries */ /* * Function: NewBuffer * Usage: buffer = NewBuffer(); * ---------------------------- * This function dynamically allocates enough memory for the * underlying representation of a bufferADT and initializes * it to represent an empty buffer. */ bufferADT NewBuffer(void); /* * Function: FreeBuffer * Usage: FreeBuffer(buffer); * -------------------------- * This function frees the storage associated with the buffer. */ void FreeBuffer(bufferADT buffer); /* * Functions: MoveCursorForward, MoveCursorBackward
  • 5. * Usage: MoveCursorForward(buffer); * MoveCursorBackward(buffer); * ---------------------------------- * These functions move the cursor forward or backward one * character, respectively. If you call MoveCursorForward * at the end of the buffer or MoveCursorBackward at the * beginning, the function call has no effect. */ void MoveCursorForward(bufferADT buffer); void MoveCursorBackward(bufferADT buffer); /* * Functions: MoveCursorToStart, MoveCursorToEnd * Usage: MoveCursorToStart(buffer); * MoveCursorToEnd(buffer); * ------------------------------- * These functions move the cursor to the start or the * end of the buffer, respectively. */ void MoveCursorToStart(bufferADT buffer); void MoveCursorToEnd(bufferADT buffer); /* * Function: InsertCharacter * Usage: InsertCharacter(buffer, ch); * ----------------------------------- * This function inserts the single character ch into the * buffer at the current cursor position. The cursor is * positioned after the inserted character, which allows * for consecutive insertions. */ void InsertCharacter(bufferADT buffer, char ch); /* * Function: DeleteCharacter * Usage: DeleteCharacter(buffer); * ------------------------------- * This function deletes the character immediately after * the cursor. If the cursor is at the end of the buffer,
  • 6. * this function has no effect. */ void DeleteCharacter(bufferADT buffer); /* * Function: DisplayBuffer * Usage: DisplayBuffer(buffer); * ----------------------------- * This function displays the current contents of the buffer * on the console. */ void DisplayBuffer(bufferADT buffer); /* * Function: ReplaceCharInBuffer * Usage: ReplaceCharInBuffer(buffer, oldch, newch); * ----------------------------- */ void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch); /* * Function: SearchStrBuffer * Usage: SearchStrBuffer(buffer, str); * ----------------------------- */ int SearchStrBuffer(bufferADT buffer, char* str); #endif listbuf.c /* * File: listbuf.c * --------------- * This file implements the buffer.h abstraction using a linked * list to represent the buffer. */ #include #include "genlib.h" #include "strlib.h"
  • 7. #include "buffer.h" /* Types */ typedef struct cellT { char ch; struct cellT *link; } cellT; struct bufferCDT { cellT *start; cellT *cursor; }; /* * Implementation notes: NewBuffer * ------------------------------- * This function allocates an empty editor buffer, represented * as a linked list. To simplify the link list operation, this * implementation adopts the useful programming tactic of * keeping an extra "dummy" cell at the beginning of each list, * so that the empty buffer has the following representation: * * +-------+ +------+ * | o---+-----====>| | * +-------+ / +------+ * | o---+---/ | NULL | * +-------+ +------+ */ bufferADT NewBuffer(void) { bufferADT buffer; buffer = New(bufferADT); buffer->start = buffer->cursor = New(cellT *); buffer->start->link = NULL; return (buffer); } /* * Implementation notes: FreeBuffer * --------------------------------
  • 8. * FreeBuffer must free every cell in the buffer as well as * the buffer storage itself. Note that the loop structure * is not exactly the standard idiom for processing every * cell within a linked list, because it is not legal to * free a cell and later look at its link field. To avoid * selecting fields in the structure after it has been freed, * you have to copy the link pointer before calling FreeBlock. */ void FreeBuffer(bufferADT buffer) { cellT *cp, *next; cp = buffer->start; while (cp != NULL) { next = cp->link; FreeBlock(cp); cp = next; } FreeBlock(buffer); } void MoveCursorForward(bufferADT buffer) { if (buffer->cursor->link != NULL) { buffer->cursor = buffer->cursor->link; } } void MoveCursorBackward(bufferADT buffer) { cellT *cp; if (buffer->cursor != buffer->start) { cp = buffer->start; while (cp->link != buffer->cursor) { cp = cp->link; } buffer->cursor = cp; } }
  • 9. void MoveCursorToStart(bufferADT buffer) { buffer->cursor = buffer->start; } void MoveCursorToEnd(bufferADT buffer) { while (buffer->cursor->link != NULL) { MoveCursorForward(buffer); } } void InsertCharacter(bufferADT buffer, char ch) { cellT *cp; cp = New(cellT *); cp->ch = ch; cp->link = buffer->cursor->link; buffer->cursor->link = cp; buffer->cursor = cp; } void DeleteCharacter(bufferADT buffer) { cellT *cp; if (buffer->cursor->link != NULL) { cp = buffer->cursor->link; buffer->cursor->link = cp->link; FreeBlock(cp); } } void DisplayBuffer(bufferADT buffer) { cellT *cp; for (cp = buffer->start->link; cp != NULL; cp = cp->link) { printf(" %c", cp->ch); } printf(" "); for (cp = buffer->start; cp != buffer->cursor; cp = cp->link) {
  • 10. printf(" "); } printf("^ "); } void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch) { cellT *cp; for(cp = buffer->cursor; cp != NULL; cp = cp->link) { if(cp->ch == oldch) { cp->ch = newch; buffer->cursor = cp; return; } } } int SearchStrBuffer(bufferADT buffer, char *str) { cellT *cp, *tmp; int i; for(cp = buffer->cursor; cp != NULL; cp = cp->link) { tmp = cp; i = 0; while(tmp) { if(tmp->ch == str[i++]) { if(str[i] == '0') { buffer->cursor = tmp; return TRUE; } else { tmp = tmp->link; } } else { break; // No point in searching further. } } } // If this point is reached, then the string was not found. return FALSE; }