Fix .gitignore and b_create()
This commit is contained in:
parent
9f21bf4b5c
commit
2c8fc9fd7f
|
@ -28,7 +28,3 @@ bld/
|
|||
## Ignore Visual Studio Code temporary files and project settings
|
||||
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
34
buffer.c
34
buffer.c
|
@ -3,7 +3,7 @@
|
|||
* Compiler: GCC 6.2.0
|
||||
* Author: Victor Fernandes, 040772243
|
||||
* Course: CST8152 - Compilers, Lab Section: 011
|
||||
* Date: September 12, 2016
|
||||
* Date: February 1, 2017
|
||||
* Professor: Svillen Ravev
|
||||
* A character buffer utility with three modes of self-incrementation
|
||||
through dynamic memory allocation, and ability to set a mark flag.
|
||||
|
@ -12,8 +12,6 @@
|
|||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include "buffer.h"
|
||||
|
@ -36,10 +34,12 @@ Buffer* b_create(short init_capacity, char inc_factor, char o_mode) {
|
|||
|
||||
/* BEGIN CONFIGURING BUFFER */
|
||||
|
||||
/* Check if init_capacitt is whithin acceptable range */
|
||||
/* Check if init_capacity is within acceptable range */
|
||||
if (init_capacity > SHRT_MAX || init_capacity < MIN_CAPACITY) {
|
||||
return NULL;
|
||||
}
|
||||
/* Leaving cb_head allocation for last in the event of bad input */
|
||||
|
||||
|
||||
/* Memory allocation */
|
||||
pBD = (Buffer *) calloc(1, sizeof(Buffer));
|
||||
|
@ -47,25 +47,29 @@ Buffer* b_create(short init_capacity, char inc_factor, char o_mode) {
|
|||
return NULL; /* Abort execution immediatelly if allocation fails */
|
||||
}
|
||||
|
||||
/* Attempt to allocate memory to cb_head */
|
||||
pBD->cb_head = (char *) malloc(sizeof(char) * init_capacity);
|
||||
/* Abort configuration if allocation fails and release memory */
|
||||
if (!pBD->cb_head) {
|
||||
free(pBD);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Check and set operation mode */
|
||||
if (o_mode == 'f' || inc_factor == FIX_INC_FACTOR) {
|
||||
pBD->mode = FIX_OP_MODE;
|
||||
pBD->inc_factor = FIX_INC_FACTOR;
|
||||
} else if (o_mode == 'a' && (inc_factor >= MIN_INC_FACTOR)) {
|
||||
} else if (o_mode == 'a'
|
||||
&& (inc_factor >= MIN_INC_FACTOR)
|
||||
&& (inc_factor <= MAX_ADD_INC_FACTOR)) {
|
||||
pBD->mode = ADD_OP_MODE;
|
||||
pBD->inc_factor = inc_factor;
|
||||
} else if (o_mode == 'm' && (inc_factor >= MIN_INC_FACTOR && inc_factor <= MAX_MUL_INC_FACTOR)) {
|
||||
} else if (o_mode == 'm'
|
||||
&& (inc_factor >= MIN_MUL_INC_FACTOR)
|
||||
&& (inc_factor <= MAX_MUL_INC_FACTOR)) {
|
||||
pBD->mode = MUL_OP_MODE;
|
||||
pBD->inc_factor = inc_factor;
|
||||
} else { /* Abort everything if parameters out of range (where applicable) */
|
||||
} else { /* Abort everything if any parameters are bad */
|
||||
free(pBD);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Attempt to initialize cb_head */
|
||||
pBD->cb_head = (char *) malloc(sizeof(char) * init_capacity);
|
||||
/* Abort configuration if allocation fails and release memory */
|
||||
if (!pBD->cb_head) {
|
||||
free(pBD);
|
||||
return NULL;
|
||||
}
|
||||
|
|
1
buffer.h
1
buffer.h
|
@ -43,6 +43,7 @@
|
|||
#define MIN_CAPACITY 1 /* minimum character buffer capacity */
|
||||
#define FIX_INC_FACTOR 0 /* fixed 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 MAX_ADD_INC_FACTOR 255 /* maximum additive increment factor constant */
|
||||
#define MAX_MUL_INC_FACTOR 100 /* maximum multiplicative increment factor constant */
|
||||
|
||||
|
|
Loading…
Reference in New Issue