-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline.c
More file actions
47 lines (40 loc) · 931 Bytes
/
line.c
File metadata and controls
47 lines (40 loc) · 931 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
38
39
40
41
42
43
44
45
46
47
#include "line.h"
#include <stdlib.h>
#include <string.h>
struct Line* line_new(const char *str, size_t len)
{
struct Line *l = calloc(1, sizeof(struct Line));
if (str) {
strncpy(l->str, str, len);
}
return l;
}
void line_free(struct Line *line)
{
line->str[0] = '\0';
line->next = line->prev = NULL;
free(line);
}
size_t line_len(struct Line *line)
{
return strlen(line->str);
}
void line_pushback(struct Line *line, const char *str, size_t cnt)
{
strncat(line->str, str, cnt);
}
void line_insert(struct Line *line, size_t ndx, char chr)
{
if (ndx < strlen(line->str)) {
char *split = line->str + ndx;
memmove(split + 1, split, strlen(split));
}
line->str[ndx] = chr;
}
void line_erase(struct Line *line, size_t ndx)
{
if (ndx < strlen(line->str)) {
char *split = line->str + ndx;
memmove(split, split + 1, strlen(split));
}
}