This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrope.c
99 lines (79 loc) · 1.72 KB
/
rope.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rope.h"
typedef struct rope_segment {
struct rope_segment *next;
int colour;
size_t length;
char data[];
} rope_segment;
rope *rope_create(void)
{
rope *rope = malloc(sizeof(rope));
rope_init(rope);
return rope;
}
void rope_init(rope *rope)
{
rope->first = rope->last = NULL;
rope->length = 0;
}
void rope_destroy(rope *rope)
{
rope_clear(rope);
free(rope);
}
void rope_clear(rope *rope)
{
rope_segment *segment = rope->first;
while(segment)
{
rope_segment *next = segment->next;
free(segment);
segment = next;
}
rope_init(rope);
}
static
rope_segment *rope_segment_create(rope_segment *next, int colour, const char *data, size_t length)
{
rope_segment *segment = malloc(sizeof(rope_segment) + length + 1);
segment->length = length;
segment->colour = colour;
segment->next = next;
memcpy(segment->data, data, length);
segment->data[length] = '\0';
return segment;
}
void rope_append(rope *rope, int colour, const char *data)
{
rope_append_raw(rope, colour, data, strlen(data));
}
void rope_append_raw(rope *rope, int colour, const char *data, size_t length)
{
if (length <= 0)
{
return;
}
rope_segment *segment = rope_segment_create(NULL, colour, data, length);
if (!rope->first)
{
rope->first = segment;
}
if (rope->last)
{
rope->last->next = segment;
}
rope->last = segment;
rope->length += length;
}
void rope_puts(const rope *rope)
{
for (rope_segment *segment = rope->first; segment; segment = segment->next)
{
fprintf(stdout, "\e[%im", segment->colour + 31);
fwrite(segment->data, segment->length, 1, stdout);
}
fprintf(stdout, "\e[0m\n");
}