Question: PLEASE CODE IT IN C LANGUAGE bPointer bAddCh(bPointer const pBuffer, char ch) o TODO204 (function header) and TODO205 (function definition). o This function is responsible
PLEASE CODE IT IN C LANGUAGE
bPointer bAddCh(bPointer const pBuffer, char ch) o TODO204 (function header) and TODO205 (function definition). o This function is responsible to include a char in the buffer. Because of the limit (given by size), several actions should be done before simply include it in the end of the buffer. o Using a bitwise operation, the function resets the flags field r_flag bit to 0 (RESET) and tries to add the character ch to the character array of the given buffer pointed by pBuffer. o Before adding a char, it is required to test the limits: it is not possible to include a char when you are at the limit (MAXIMUM ALLOWED POSITIVE VALUE 1). o NOTE_07: If the buffer is operational and it is not full, the symbol can be stored in the character buffer. In this case, the function adds the character to the content of the character buffer, increments addCOffset by 1 and returns. o NOTE_08: If the character buffer is already full, the function will try to resize the buffer by increasing the current size to a new one. How the size is increased depends on the current operational mode of the buffer. If the operational mode is FIXMODE, the function returns NULL (remember that it is not possible to increase buffer in FIXED mode) If the operational mode is ADDMODE, it tries to increase the current size of the buffer to a new size by adding increment (converted to bytes) to size. If the result from the operation is positive and does not exceed the MAXIMUM ALLOWED POSITIVE VALUE 1, the function proceeds. If the result from the operation is equal to MAXIMUM ALLOWED POSITIVE VALUE, it assigns the MAXIMUM ALLOWED POSITIVE VALUE 1 to the new size and proceeds. o NOTE_09: Remember that the MAXIMUM ALLOWED POSITIVE VALUE is determined by the data type of the variable, which contains the buffer size. If the result from the operation is negative, it returns NULL. If the operational mode is MULMODE, it tries to increase the current size of the buffer to a new size in the following manner: If the current size can not be incremented anymore because it has already reached the maximum size of the buffer, the function returns NULL. The function tries to increase the current size using the following formulae: available space = maximum buffer size current size new increment = available space * inc_factor / 100 new size = current size + new increment TIP_01: To use this formula, check eventual casting to guarantee acceptable values. The value can vary according to the percentage of the current size. The maximum buffer size is the MAXIMUM ALLOWED POSITIVE VALUE 1. If the new size has been incremented successfully, no further adjustment of the new size is required. NOTE_10: If as a result of the calculations, the current size cannot be incremented, but it is still smaller than the MAXIMUM ALLOWED POSITIVE VALUE 1, then the new size is set to the value of MAXIMUM ALLOWED 1 and the function proceeds. o If the operation is successful, the function performs the following operations: The function tries to expand the character buffer calling realloc() with the new size, If the location in memory of the character buffer has been changed by the reallocation, the function sets r_flag bit to 1 using a bitwise operation, In short, it adds (appends) the character ch to the buffer content, It also changes the value of addCOffset by 1, and saves the newly calculated size, In the end, the function returns a pointer to the buffer structure. o Error Condition_02: 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, The function must not destroy the buffer or the contents of the buffer even when an error occurs it must simply return NULL leaving the existing buffer content intact.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
