-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
55 lines (49 loc) · 943 Bytes
/
io.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @(#)io.c 1.0 01/01/2019
* Copyright GNU GPLv3
* @author KPETO Lonv'ha <mmkpeto5@gmail.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include "io.h"
/**
* @inherit
*/
char* readLine() {
char* line = (char*)malloc(DEFAULT_CAPACITY* sizeof(char));
int capacity = DEFAULT_CAPACITY;
int index = 0;
char c;
while ( (c = getchar()) != EOL ) {
if(is_full_buffer(index, capacity))
expand(line, capacity);
*(line + index++) = c;
}
shrink(line, index);
return line;
}
/**
* @inherit
*/
int is_full_buffer(int index, int capacity) {
return (index == (capacity - 1));
}
/**
* @inherit
*/
void expand(char* line, int capacity) {
int nc = capacity << 1;
realloc(line, nc * DEFAULT_CAPACITY);
}
/**
* @inherit
*/
void shrink(char* line, int length) {
realloc(line, length);
}
/**
* Print help text
*/
void help() {
fprintf(stdout, "Help goes here.");
}