-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.c
More file actions
63 lines (56 loc) · 1.16 KB
/
Copy pathbuffer.c
File metadata and controls
63 lines (56 loc) · 1.16 KB
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
56
57
58
59
60
61
62
63
#include "buffer.h"
#include <stdlib.h>
#include <assert.h>
Text_Buff *
buff_create()
{
Text_Buff *t_buff = (Text_Buff *)malloc(sizeof(Text_Buff));
t_buff->capacity = 40;
t_buff->top = 0;
char *buffer = (char *)malloc(t_buff->capacity * sizeof(char));
assert(buffer!=0 && "malloc failed!\n");
buffer[0] = 0;
t_buff->buffer = buffer;
return t_buff;
}
int
buff_add(Text_Buff *buff, char c)
{
int ret_val = 0;
if (buff->top > buff->capacity - 2)
{
char *temp_buff;
buff->capacity *= 2;
temp_buff = realloc(buff->buffer, buff->capacity);
assert(temp_buff!=0 && "realloc failed\n");
buff->buffer = temp_buff;
}
buff->buffer[buff->top++] = c;
buff->buffer[buff->top] = 0;
return ret_val;
}
int
buff_clear(Text_Buff *buff)
{
int ret_val = 0;
buff->top = 0;
buff->buffer[0] = (char)0;
return ret_val;
}
int
buff_pop(Text_Buff *buff)
{
int ret_val = 0;
if (buff->top) buff->top--;
buff->buffer[buff->top] = 0;
return ret_val;
}
int
buff_free(Text_Buff *buff)
{
buff->top = 0;
buff->capacity = 0;
free(buff->buffer);
free(buff);
return 0;
}