Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* standard input/output */
- #include <malloc.h> /* for dynamic memory allocation*/
- #include <limits.h> /* implementation-defined data type ranges and limits */
- #include "buffer.h"
- Buffer * b_create(short init_capacity, char inc_factor, char o_mode){
- /*test parameters*/
- if (init_capacity > SHRT_MAX || init_capacity < 1){
- /*Invaild capacity */
- return NULL;
- }
- if ((o_mode != 'f') && (o_mode != 'm') && (o_mode != 'a')){
- /*not vaild o_mode*/
- return NULL;
- }
- pBuffer pBD = (Buffer *)calloc(init_capacity, sizeof(Buffer));
- if (pBD == NULL){
- perror("Error");
- return NULL;
- }
- pBD->cb_head = (char *)malloc(init_capacity *sizeof(char));
- if (pBD->cb_head == NULL){
- perror("Error");
- return NULL;
- }
- switch (o_mode)
- {
- case'f':
- pBD->mode = 0;
- pBD->inc_factor = 0;
- break;
- case'a':
- if ((unsigned char)inc_factor > UCHAR_MAX || (unsigned char)inc_factor < 1){
- /*invalid range*/
- b_free(pBD);
- return NULL;
- }
- pBD->mode = 1;
- pBD->inc_factor = inc_factor;
- break;
- case'm':
- if (inc_factor > M_INC_FACTOR_MAX || inc_factor < 1){
- /*invalid range*/
- b_free(pBD);
- return NULL;
- }
- pBD->mode = -1;
- pBD->inc_factor = inc_factor;
- break;
- default:
- return NULL;
- break;
- }
- pBD->addc_offset = 0;
- pBD->eob = 0;
- pBD->getc_offset = 0;
- pBD->capacity = init_capacity;
- return pBD;
- }
- pBuffer b_addc(pBuffer const pBD, char symbol){
- pBD->r_flag = 0;
- /*if function is buffer not at capactiy add the symbol to the char array and incrment the addc_offeset by 1 and return*/
- if (b_isfull(pBD) == 1){
- /*if buffer is full it must be increased depending on operation mode*/
- /*old address*/
- char *old_mem_location = pBD->cb_head;
- if (pBD->mode == 0){
- /*operating mode expected to return null */
- return NULL;
- }
- if (pBD->mode == 1){
- if (pBD->capacity >= SHRT_MAX){
- /*buffer max size reached return null*/
- printf("Buffer at maxium Capacity");
- return NULL;
- }
- /*increase capacity by inc_factor*/
- short temp_inc_factor = (unsigned char)pBD->inc_factor;
- if ((pBD->capacity + temp_inc_factor) >= SHRT_MAX){
- pBD->capacity = SHRT_MAX;
- }
- else{
- pBD->capacity = pBD->capacity + temp_inc_factor;
- }
- }
- if (pBD->mode == -1){
- if (pBD->capacity >= SHRT_MAX){
- /*buffer max size reached return null*/
- printf("Buffer at maxium Capacity");
- return NULL;
- }
- short space_left = SHRT_MAX - pBD->capacity;
- /*don't need to cast in mode -1 max values are 1 to 100*/
- short temp_inc_factor = space_left * pBD->inc_factor / 100;
- if (temp_inc_factor <= 0 || ((temp_inc_factor + pBD->capacity) >= SHRT_MAX)){
- pBD->cb_head = realloc(pBD->cb_head, (SHRT_MAX *sizeof(char)));
- pBD->capacity = SHRT_MAX;
- }
- else {
- /*increase the capacity*/
- pBD->capacity += temp_inc_factor;
- }
- }
- /*Try to expand the memory*/
- pBD->cb_head = (char *)realloc(pBD->cb_head, (pBD->capacity *sizeof(char)));
- if (pBD->cb_head == NULL){
- return NULL;
- }
- if (old_mem_location != pBD->cb_head){
- /*If realloc moved the memory location set r_flag to 1*/
- pBD->r_flag = SET_R_FLAG;
- }
- }
- /*Add the symbol to the cb_head array*/
- pBD->cb_head[pBD->addc_offset] = symbol;
- pBD->addc_offset++;
- return pBD;
- }
- int b_reset(Buffer* const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- /*will allow char array to be overwritten*/
- pBD->addc_offset = 0;
- if (pBD->addc_offset != 0){
- /*failed to set property*/
- return R_FAIL1;
- }
- return 0;
- }
- void b_free(Buffer* const pBD){
- free(pBD->cb_head);
- free(pBD);
- }
- int b_isfull(Buffer* const pBD){
- if (pBD == NULL){
- /*Run time Error*/
- return R_FAIL1;
- }
- if (pBD->addc_offset == pBD->capacity){
- /*Buffer is full*/
- pBD->eob = 1;
- return 1;
- }
- pBD->eob = 0;
- return 0;
- }
- short b_size(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- return(pBD->addc_offset);
- }
- short b_capacity(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- return pBD->capacity;
- }
- short b_setmark(Buffer * const pBD, short mark){
- if (pBD == NULL || mark > pBD->addc_offset){
- return R_FAIL1;
- }
- pBD->mark_offset = mark;
- return 0;
- }
- short b_mark(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- return pBD->mark_offset;
- }
- int b_mode(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- return (int)pBD->mode;
- }
- size_t b_incfactor(Buffer * const pBD){
- if (pBD == NULL){
- return 256;
- }
- return (unsigned char)pBD->inc_factor;
- }
- int b_load(FILE * const fi, Buffer * const pBD){
- /* if the current character cannot be put in the buffer, the */
- int input;
- while (1)
- {
- input = fgetc(fi);
- if (feof(fi) || input == -1)
- {
- return 0;
- }
- if (b_addc(pBD, (char)input) == NULL){
- return LOAD_FAIL;
- }
- }
- return 0;
- }
- int b_isempty(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- if (pBD->addc_offset > 0){
- return 1;
- }
- return 0;
- }
- int b_eob(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- return pBD->eob;
- }
- char b_getc(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL2;
- }
- if (pBD->getc_offset == pBD->addc_offset){
- pBD->eob = 1;
- return R_FAIL1;
- }
- short offset_preincrement = pBD->getc_offset;
- pBD->eob = 0;
- pBD->getc_offset++;
- return (unsigned char)pBD->cb_head[offset_preincrement];
- }
- int b_print(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- if (b_isempty(pBD) == 0){
- printf("The Buffer is empty\n");
- return 0;
- }
- short temp = pBD->getc_offset;
- short count = 0;
- char letter;
- pBD->getc_offset = 0;
- while (pBD->eob != 1){
- letter = b_getc(pBD);
- if (letter >= EOF){
- printf("%c", letter);
- }
- count++;
- }
- printf("\n");
- pBD->getc_offset = temp;
- return count;
- }
- Buffer *b_pack(Buffer * const pBD){
- if (pBD == NULL){
- return NULL;
- }
- if (pBD->eob == 1){
- /*Buffer is already at the full althought you may want to expand mem here*/
- return pBD;
- }
- short new_capacity = pBD->addc_offset + 1;
- char * temp = pBD->cb_head;
- pBD->cb_head = (char*)realloc(pBD->cb_head, (new_capacity * sizeof(char)));
- if (temp != pBD->cb_head){
- /*memory had been reloacted*/
- pBD->r_flag = 1;
- }
- pBD->capacity = new_capacity;
- return pBD;
- }
- char b_rflag(Buffer *const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- return pBD->r_flag;
- }
- short b_retract(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- if (pBD->getc_offset == 0){
- return pBD->addc_offset;
- }
- pBD->getc_offset--;
- return pBD->getc_offset;
- }
- short b_retract_to_mark(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- if (pBD->getc_offset == pBD->mark_offset){
- return pBD->getc_offset;
- }
- pBD->getc_offset = pBD->mark_offset;
- return pBD->getc_offset;
- }
- short b_getcoffset(Buffer * const pBD){
- if (pBD == NULL){
- return R_FAIL1;
- }
- return pBD->getc_offset;
- }
- char * b_cbhead(Buffer * const pBD){
- if (pBD == NULL){
- return NULL;
- }
- return pBD->cb_head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement