-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputbuffer.c
More file actions
37 lines (30 loc) · 896 Bytes
/
inputbuffer.c
File metadata and controls
37 lines (30 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inputbuffer.h"
//basically constructor for InputBuffer
InputBuffer* newInputBuffer() {
//allocates memory for input buffer
InputBuffer* inputBuffer = (InputBuffer*)malloc(sizeof(InputBuffer));
inputBuffer->buffer = NULL;
inputBuffer->bufferLength = 0;
inputBuffer->inputLength = 0;
return inputBuffer;
}
void printPrompt() { printf("db > "); }
void readInput(InputBuffer* inputBuffer){
ssize_t bytesRead = getline(&(inputBuffer->buffer), &(inputBuffer->bufferLength), stdin);
if(bytesRead <= 0){
printf("Error reading input. \n");
exit(EXIT_FAILURE);
}
//Ignore Trailing New Line
inputBuffer->inputLength = bytesRead - 1;
inputBuffer->buffer[bytesRead - 1] = 0;
}
void closeInputBuffer(InputBuffer* inputBuffer){
//deallocate memory
free(inputBuffer->buffer);
free(inputBuffer);
}