diff options
Diffstat (limited to 'common/readline.c')
-rw-r--r-- | common/readline.c | 72 |
1 files changed, 0 insertions, 72 deletions
diff --git a/common/readline.c b/common/readline.c deleted file mode 100644 index 58652429..00000000 --- a/common/readline.c +++ /dev/null | |||
@@ -1,72 +0,0 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include "readline.h" | ||
3 | #include "log.h" | ||
4 | #include <stdlib.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | char *read_line(FILE *file) { | ||
8 | size_t length = 0, size = 128; | ||
9 | char *string = malloc(size); | ||
10 | char lastChar = '\0'; | ||
11 | if (!string) { | ||
12 | wlr_log(WLR_ERROR, "Unable to allocate memory for read_line"); | ||
13 | return NULL; | ||
14 | } | ||
15 | while (1) { | ||
16 | int c = getc(file); | ||
17 | if (c == '\n' && lastChar == '\\'){ | ||
18 | --length; // Ignore last character. | ||
19 | lastChar = '\0'; | ||
20 | continue; | ||
21 | } | ||
22 | if (c == EOF || c == '\n' || c == '\0') { | ||
23 | break; | ||
24 | } | ||
25 | if (c == '\r') { | ||
26 | continue; | ||
27 | } | ||
28 | lastChar = c; | ||
29 | if (length == size) { | ||
30 | char *new_string = realloc(string, size *= 2); | ||
31 | if (!new_string) { | ||
32 | free(string); | ||
33 | wlr_log(WLR_ERROR, "Unable to allocate memory for read_line"); | ||
34 | return NULL; | ||
35 | } | ||
36 | string = new_string; | ||
37 | } | ||
38 | string[length++] = c; | ||
39 | } | ||
40 | if (length + 1 == size) { | ||
41 | char *new_string = realloc(string, length + 1); | ||
42 | if (!new_string) { | ||
43 | free(string); | ||
44 | return NULL; | ||
45 | } | ||
46 | string = new_string; | ||
47 | } | ||
48 | string[length] = '\0'; | ||
49 | return string; | ||
50 | } | ||
51 | |||
52 | char *peek_line(FILE *file, int line_offset, long *position) { | ||
53 | long pos = ftell(file); | ||
54 | size_t length = 0; | ||
55 | char *line = NULL; | ||
56 | for (int i = 0; i <= line_offset; i++) { | ||
57 | ssize_t read = getline(&line, &length, file); | ||
58 | if (read < 0) { | ||
59 | free(line); | ||
60 | line = NULL; | ||
61 | break; | ||
62 | } | ||
63 | if (read > 0 && line[read - 1] == '\n') { | ||
64 | line[read - 1] = '\0'; | ||
65 | } | ||
66 | } | ||
67 | if (position) { | ||
68 | *position = ftell(file); | ||
69 | } | ||
70 | fseek(file, pos, SEEK_SET); | ||
71 | return line; | ||
72 | } | ||