Clean up redundant code and mixed measurements

This commit is contained in:
Victor Fernandes 2017-02-13 18:27:32 -05:00
parent 64e0f2d80a
commit 992f82573a
2 changed files with 103 additions and 117 deletions

View File

@ -206,19 +206,6 @@ short b_getcoffset(Buffer* const pBD) {
return pBD->getc_offset; return pBD->getc_offset;
} }
/* Returns the buffer's actual character buffer address
* Author: Victor Fernandes, 040772243
* Version: 0.0.1
* Called functions: N/A
* Parameters:
- pBuffer const pBD
* Return value: char*, NULL
*/
char* b_cbhead(Buffer* const pBD) {
if (!pBD || !pBD->cb_head) { return NULL; }
return pBD->cb_head;
}
/* Sets a mark offset on the buffer's mark flag and returns a pointer /* Sets a mark offset on the buffer's mark flag and returns a pointer
to cb_head at that offset to cb_head at that offset
* Author: Victor Fernandes, 040772243 * Author: Victor Fernandes, 040772243
@ -278,14 +265,14 @@ pBuffer b_addc(pBuffer const pBD, char symbol) {
pBD->r_flag = UNSET_R_FLAG; pBD->r_flag = UNSET_R_FLAG;
/* BEGIN BUFFER INCREMENT */ /* BEGIN BUFFER INCREMENT */
if (pBD->addc_offset >= pBD->capacity){ if (pBD->addc_offset == pBD->capacity){
if (pBD->mode == FIX_OP_MODE) { /* Fixed mode, won't increment */ if (pBD->mode == FIX_OP_MODE) { /* Fixed mode, won't increment */
return NULL; return NULL;
} }
else if (pBD->mode == ADD_OP_MODE) { /* Calculate new size for additive mode */ else if (pBD->mode == ADD_OP_MODE) { /* Calculate new size for additive mode */
new_cap = pBD->capacity + (unsigned char) pBD->inc_factor; new_cap = pBD->capacity + (unsigned char) pBD->inc_factor;
/* Make sure no short overflow happened */ /* Make sure no short overflow happened */
if (new_cap < MIN_CAPACITY){ if (new_cap < ZERO_CAPACITY){
return NULL; return NULL;
} }
@ -301,8 +288,9 @@ pBuffer b_addc(pBuffer const pBD, char symbol) {
new_inc = (short) (avail_space * (((double) pBD->inc_factor) / 100)); new_inc = (short) (avail_space * (((double) pBD->inc_factor) / 100));
/* Check if there is enough space for the new increment and /* Check if there is enough space for the new increment and
"trim" it if needed */ "trim" it if needed*/
if (new_inc >= avail_space || (new_inc <= MIN_CAPACITY && pBD->capacity < SHRT_MAX)) { /* Note: If the available space is 1 or 0, new_inc will evaluate to 0 (after truncation) */
if (new_inc == ZERO_CAPACITY) {
new_cap = SHRT_MAX; new_cap = SHRT_MAX;
} }
else { else {
@ -312,7 +300,7 @@ pBuffer b_addc(pBuffer const pBD, char symbol) {
/* Reallocate memory to character buffer */ /* Reallocate memory to character buffer */
old_addr = pBD->cb_head; /* Keep track of old pointer address to check if it changed */ old_addr = pBD->cb_head; /* Keep track of old pointer address to check if it changed */
tmp_addr = (char *)realloc(pBD->cb_head, sizeof(char) * new_cap); tmp_addr = (char *)realloc(pBD->cb_head, sizeof(char) * (unsigned short) new_cap);
if (tmp_addr == NULL){ if (tmp_addr == NULL){
return NULL; /* Abort everything if allocation fails */ return NULL; /* Abort everything if allocation fails */
@ -321,13 +309,12 @@ pBuffer b_addc(pBuffer const pBD, char symbol) {
pBD->cb_head = tmp_addr; pBD->cb_head = tmp_addr;
pBD->capacity = new_cap; pBD->capacity = new_cap;
if (old_addr == pBD->cb_head) { /* Compare the old and new addresses and set flag appropriately */ if (old_addr == pBD->cb_head) { /* Compare the old and new addresses and set flag appropriately */
pBD->r_flag = SET_R_FLAG; pBD->r_flag = (pBD->cb_head == tmp_addr);
} }
} /* END BUFFER INCREASE */ } /* END BUFFER INCREASE */
/* Finally, add new symbol to the buffer after increasing it (or not) */ /* Finally, add new symbol to the buffer after increasing it (or not) */
pBD->cb_head[pBD->addc_offset] = symbol; pBD->cb_head[pBD->addc_offset++] = symbol;
pBD->addc_offset++;
return pBD; return pBD;
} }

191
buffer.h
View File

@ -1,96 +1,95 @@
/* File Name: buffer.h /* File Name: buffer.h
* Version: 1.16.2 * Version: 1.16.2
* Author: S^R & Victor Fernandes * Author: S^R & Victor Fernandes
* Date: 1 February 2017 * Date: 1 February 2017
* Preprocessor directives, type declarations and prototypes * Preprocessor directives, type declarations and prototypes
necessary for buffer implementation necessary for buffer implementation
* as required for CST8152, Assignment #1, Winter 2017. * as required for CST8152, Assignment #1, Winter 2017.
* Function list: b_create, b_addc, b_reset, b_free, b_isfull, * Function list: b_create, b_addc, b_reset, b_free, b_isfull,
b_size, b_capacity, b_setmark, b_mark, b_mode, b_incfactor, b_size, b_capacity, b_setmark, b_mark, b_mode, b_incfactor,
b_load, b_isempty, b_eob, b_getc, b_print, b_pack, b_rflag, b_load, b_isempty, b_eob, b_getc, b_print, b_pack, b_rflag,
b_retract, b_retract_to_mark, b_getcoffset, b_cbhead b_retract, b_retract_to_mark, b_getcoffset, b_cbhead
*/ */
#ifndef BUFFER_H_ #ifndef BUFFER_H_
#define BUFFER_H_ #define BUFFER_H_
/*#pragma warning(1:4001) *//*to enforce C89 type comments - to make //comments an warning */ /*#pragma warning(1:4001) *//*to enforce C89 type comments - to make //comments an warning */
/*#pragma warning(error:4001)*//* to enforce C89 comments - to make // comments an error */ /*#pragma warning(error:4001)*//* to enforce C89 comments - to make // comments an error */
/* standard header files */ /* standard header files */
#include <stdio.h> /* standard input/output */ #include <stdio.h> /* standard input/output */
#include <malloc.h> /* for dynamic memory allocation.*/ #include <malloc.h> /* for dynamic memory allocation.*/
#include <limits.h> /* implementation-defined data type ranges and limits */ #include <limits.h> /* implementation-defined data type ranges and limits */
/* constant definitions */ /* constant definitions */
#ifndef TRUE #ifndef TRUE
#define TRUE 1 #define TRUE 1
#endif #endif
#ifndef FALSE #ifndef FALSE
#define FALSE 0 #define FALSE 0
#endif #endif
#define R_FAIL1 -1 /* fail return value */ #define R_FAIL1 -1 /* fail return value */
#define R_FAIL2 -2 /* fail return value */ #define R_FAIL2 -2 /* fail return value */
#define LOAD_FAIL -2 /* load fail error */ #define LOAD_FAIL -2 /* load fail error */
#define SET_R_FLAG 1 /* realloc flag set value */ #define SET_R_FLAG 1 /* realloc flag set value */
#define UNSET_R_FLAG 0 /* unset reallocation flag */ #define UNSET_R_FLAG 0 /* unset reallocation flag */
#define SET_EOB_FLAG 1 /* set end of buffer flag */ #define SET_EOB_FLAG 1 /* set end of buffer flag */
#define UNSET_EOB_FLAG 0 /* unset end of buffer flag */ #define UNSET_EOB_FLAG 0 /* unset end of buffer flag */
#define MUL_OP_MODE -1 /* multiplicative op mode */ #define MUL_OP_MODE -1 /* multiplicative op mode */
#define FIX_OP_MODE 0 /* fixed op mode */ #define FIX_OP_MODE 0 /* fixed op mode */
#define ADD_OP_MODE 1 /* additive op mode */ #define ADD_OP_MODE 1 /* additive op mode */
#define OFFSET_RESET 0 /* reset character offset */ #define OFFSET_RESET 0 /* reset character offset */
#define ZERO_CAPACITY 0 #define ZERO_CAPACITY 0
#define MIN_CAPACITY 1 /* minimum character buffer capacity */ #define MIN_CAPACITY 1 /* minimum character buffer capacity */
#define FIX_INC_FACTOR 0 /* fixed increment factor constant */ #define FIX_INC_FACTOR 0 /* fixed increment factor constant */
#define MIN_INC_FACTOR 1 /* minimum additive increment factor constant */ #define MIN_INC_FACTOR 1 /* minimum additive increment factor constant */
#define MIN_MUL_INC_FACTOR 1 /* minumum multiplicative increment factor constant */ #define MIN_MUL_INC_FACTOR 1 /* minumum multiplicative increment factor constant */
#define MAX_ADD_INC_FACTOR 255 /* maximum additive increment factor constant */ #define MAX_ADD_INC_FACTOR 255 /* maximum additive increment factor constant */
#define MAX_MUL_INC_FACTOR 100 /* maximum multiplicative increment factor constant */ #define MAX_MUL_INC_FACTOR 100 /* maximum multiplicative increment factor constant */
#define ERR_INC_FACTOR 256 /* return value in case of error on reading increment factor */ #define ERR_INC_FACTOR 256 /* return value in case of error on reading increment factor */
/* user data type declarations */ /* user data type declarations */
typedef struct BufferDescriptor { typedef struct BufferDescriptor {
char *cb_head; /* pointer to the beginning of character array (character buffer) */ char *cb_head; /* pointer to the beginning of character array (character buffer) */
short capacity; /* current dynamic memory size (in bytes) allocated to character buffer */ short capacity; /* current dynamic memory size (in bytes) allocated to character buffer */
short addc_offset; /* the offset (in chars) to the add-character location */ short addc_offset; /* the offset (in chars) to the add-character location */
short getc_offset; /* the offset (in chars) to the get-character location */ short getc_offset; /* the offset (in chars) to the get-character location */
short mark_offset; /* the offset (in chars) to the mark location */ short mark_offset; /* the offset (in chars) to the mark location */
char inc_factor; /* character array increment factor */ char inc_factor; /* character array increment factor */
char r_flag; /* reallocation flag */ char r_flag; /* reallocation flag */
char mode; /* operational mode indicator*/ char mode; /* operational mode indicator*/
int eob; /* end-of-buffer flag */ int eob; /* end-of-buffer flag */
} Buffer, *pBuffer; } Buffer, *pBuffer;
/*typedef Buffer *pBuffer;*/ /*typedef Buffer *pBuffer;*/
/* function declarations */ /* function declarations */
Buffer* b_create(short, char, char); Buffer* b_create(short, char, char);
pBuffer b_addc(pBuffer const, char); pBuffer b_addc(pBuffer const, char);
int b_reset(Buffer* const); int b_reset(Buffer* const);
void b_free(Buffer* const); void b_free(Buffer* const);
int b_isfull(Buffer* const); int b_isfull(Buffer* const);
short b_size(Buffer* const); short b_size(Buffer* const);
short b_capacity(Buffer* const); short b_capacity(Buffer* const);
char* b_setmark(Buffer* const, short); char* b_setmark(Buffer* const, short);
short b_mark(Buffer* const); short b_mark(Buffer* const);
int b_mode(Buffer* const); int b_mode(Buffer* const);
size_t b_incfactor(Buffer* const); size_t b_incfactor(Buffer* const);
int b_load(FILE* const, Buffer* const); int b_load(FILE* const, Buffer* const);
int b_isempty(Buffer* const); int b_isempty(Buffer* const);
int b_eob(Buffer* const); int b_eob(Buffer* const);
char b_getc(Buffer* const); char b_getc(Buffer* const);
int b_print(Buffer* const); int b_print(Buffer* const);
Buffer* b_pack(Buffer* const); Buffer* b_pack(Buffer* const);
char b_rflag(Buffer* const); char b_rflag(Buffer* const);
short b_retract(Buffer* const); short b_retract(Buffer* const);
short b_retract_to_mark(Buffer* const); short b_retract_to_mark(Buffer* const);
short b_getcoffset(Buffer* const); short b_getcoffset(Buffer* const);
char* b_cbhead(Buffer* const);
#endif
#endif