Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you update the given codes with their requirements: readerCreate (size, increment, mode); o This function creates a new reader in memory (on the program

Can you update the given codes with their requirements:

readerCreate (size, increment, mode); o This function creates a new reader in memory (on the program heap), trying to allocate memory for one reader structure using calloc(); o It tries to allocates memory for one dynamic character (content) calling malloc() with the given initial capacity (size); o The reader is supposed to use the parameters passed to initialize the fields size, increment and mode, but some adjustments must be done: If there is no value for size, default values (for size and increment) are used. If there is no value for increment, the mode must be fixed. If mode is different from valid options (Fixed, Additive or Multiplicative), no reader must be created. The flags must be initialized by default values defined in the header. o Return value: Reader pointer. o Flag: Set values to default. o Error Condition: If run-time error occurs, the function must return immediately after the error is discovered. Check for all possible errors which can occur at run time. Do not allow "memory leaks", "dangling" pointers, or "bad" parameters. o DEFENSIVE PROGRAMMING: The size and increment must be non-negative values and the mode must be one of the valid fields: 'f', 'a', 'm'.

Its code:

BufferPointer readerCreate(sofia_intg size, sofia_intg increment, sofia_intg mode) {

BufferPointer readerPointer;

/* TO_DO: Defensive programming */

/* TO_DO: Adjust the values according to parameters */

readerPointer = (BufferPointer)calloc(1, sizeof(Buffer));

if (!readerPointer)

return NULL;

readerPointer->content = (sofia_string)malloc(size);

/* TO_DO: Defensive programming */

/* TO_DO: Initialize the histogram */

readerPointer->size = size;

readerPointer->increment = increment;

readerPointer->mode = mode;

/* TO_DO: Initialize flags */

/* TO_DO: The created flag must be signalized as EMP */

/* NEW: Cleaning the content */

if (readerPointer->content)

readerPointer->content[0] = READER_TERMINATOR;

readerPointer->position.wrte = 0;

readerPointer->position.mark = 0;

readerPointer->position.read = 0;

return readerPointer;

}

readerAddChar (BufferPointer, ch) o This is the most important function in the code. This function is responsible to include a char in the reader. Because of the limit (given by size), several actions should be done before simply include it in the end of the reader. Because there is a possibility that the reader can be already full, while the maximum size is not achieved (for instance, READER_MAX_SIZE), if the mode is not fixed, it is required to reallocate additional space (using C function to realloc). o If the reader can include a char (it is not full), just include it in the current position given by wrte offset and increment this position. o Otherwise, if the reader is full, some actions must be done: Start using a bitwise operation the function resets the REL bit on flags. If mode is MODE_FIXED, it is not possible to allocate additional space and function ends. If the mode is MODE_ADDIT, it is required to increase the size by using a linear formula: newSize = BufferPointer->size + BufferPointer->increment; If mode is multiplicative (MODE_MULTI) the formula is given by: newSize = BufferPointer->size * BufferPointer->increment; In both cases, it is required to check if the result is valid (not negative and lower than READER_MAX_SIZE) and in case of error, no inclusion can be done. Otherwise, if it is detected that the memory address has been changed, the REL bit must be set (use comparison between addresses to check this). If everything is ok, you can use realloc() to increase the size of the content. However, remember to use defensive programming: call realloc using a temporary variable and avoid to destroy the original reader. Finally, add the character ch to the character array of the given content pointed by BufferPointer. o Return value: Reader pointer. o Flag: You need to manipulate the flags carefully: To check if it is possible to include a char (without reallocation), check the FUL bit. Due the risk that it is possible to do memory reallocation, start setting the REL bit. When adding the char, if the newSize is not valid (remember what it means), set the FUL bit. When doing the reallocation (remember each mode), if the memory position has been changed (think about how to discover this by comparison), set REL bit. o Error Condition: The function must return NULL on any error. Some of the possible errors are indicated above but you must check for all possible errors that can occur at run-time. Do not allow "memory leaks". Avoid creating "dangling pointers" and using "bad" parameters. TIP: The function must not destroy the reader or the contents of the reader even when an error occurs - it must simply return NULL leaving the existing reader content intact. DEFENSIVE PROGRAMMING: BufferPointer must exist (different from NULL) and the char to be included must be valid (think about ASCII table)

It's code:

BufferPointer readerAddChar(BufferPointer const readerPointer, sofia_char ch) {

sofia_string tempReader = NULL;

sofia_intg newSize = 0;

/* TO_DO: Defensive programming */

/* TO_DO: Reset Realocation */

/* TO_DO: Test the inclusion of chars */

if (readerPointer->position.wrte * (sofia_intg)sizeof(sofia_char) < readerPointer->size) {

/* TO_DO: This buffer is NOT full */

} else {

/* TO_DO: Reset Full flag */

switch (readerPointer->mode) {

case MODE_FIXED:

return NULL;

case MODE_ADDIT:

/* TO_DO: Adjust new size */

/* TO_DO: Defensive programming */

break;

case MODE_MULTI:

/* TO_DO: Adjust new size */

/* TO_DO: Defensive programming */

break;

default:

return NULL;

}

/* TO_DO: New reader allocation */

/* TO_DO: Defensive programming */

/* TO_DO: Check Relocation */

}

/* TO_DO: Add the char */

readerPointer->content[readerPointer->position.wrte++] = ch;

/* TO_DO: Updates histogram */

return readerPointer;

}

readerClear (BufferPointer) o The function retains the memory space currently allocated to the reader. o It reinitializes all appropriate data members of the given reader structure so that the reader will appear as just created. o In short, offsets and flags must be reset. o Flag: When you are cleaning the reader, adjust all bits from flag (similar during creation). o Return value: Boolean value defined by your language. o DEFENSIVE PROGRAMMING: BufferPointer must exist (different from NULL).

Its code:

bool readerClear(BufferPointer const readerPointer) { /* TO_DO: Defensive programming */ /* TO_DO: Adjust flags original */ readerPointer->position.wrte = readerPointer->position.mark = readerPointer->position.read = 0; return True; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Foundations of Financial Management

Authors: Stanley Block, Geoffrey Hirt, Bartley Danielsen, Doug Short, Michael Perretta

10th Canadian edition

1259261018, 1259261015, 978-1259024979

More Books

Students also viewed these Algorithms questions

Question

Where did the faculty member get his/her education? What field?

Answered: 1 week ago