2018-05-31 12:23:11 +10:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2015-08-05 11:30:40 +10:00
|
|
|
#include "readline.h"
|
2016-12-16 09:08:56 +11:00
|
|
|
#include "log.h"
|
2015-08-05 11:30:40 +10:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
char *read_line(FILE *file) {
|
2016-01-28 23:56:46 +11:00
|
|
|
size_t length = 0, size = 128;
|
2015-08-05 11:30:40 +10:00
|
|
|
char *string = malloc(size);
|
2016-06-06 08:17:27 +10:00
|
|
|
char lastChar = '\0';
|
2015-08-05 11:30:40 +10:00
|
|
|
if (!string) {
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_ERROR, "Unable to allocate memory for read_line");
|
2015-08-05 11:30:40 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
int c = getc(file);
|
2016-06-06 08:17:27 +10:00
|
|
|
if (c == '\n' && lastChar == '\\'){
|
|
|
|
--length; // Ignore last character.
|
|
|
|
lastChar = '\0';
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-05 11:30:40 +10:00
|
|
|
if (c == EOF || c == '\n' || c == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == '\r') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-06 08:17:27 +10:00
|
|
|
lastChar = c;
|
2015-08-18 18:32:54 +10:00
|
|
|
if (length == size) {
|
2015-08-20 10:24:47 +10:00
|
|
|
char *new_string = realloc(string, size *= 2);
|
|
|
|
if (!new_string) {
|
|
|
|
free(string);
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_ERROR, "Unable to allocate memory for read_line");
|
2015-08-05 11:30:40 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-20 10:24:47 +10:00
|
|
|
string = new_string;
|
2015-08-05 11:30:40 +10:00
|
|
|
}
|
2015-08-18 18:32:54 +10:00
|
|
|
string[length++] = c;
|
2015-08-05 11:30:40 +10:00
|
|
|
}
|
2015-08-18 18:32:54 +10:00
|
|
|
if (length + 1 == size) {
|
2015-08-20 10:24:47 +10:00
|
|
|
char *new_string = realloc(string, length + 1);
|
|
|
|
if (!new_string) {
|
|
|
|
free(string);
|
2015-08-05 11:30:40 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-20 10:24:47 +10:00
|
|
|
string = new_string;
|
2015-08-05 11:30:40 +10:00
|
|
|
}
|
2015-08-18 18:32:54 +10:00
|
|
|
string[length] = '\0';
|
2015-08-05 11:30:40 +10:00
|
|
|
return string;
|
|
|
|
}
|
2016-01-28 23:56:46 +11:00
|
|
|
|
2018-06-02 08:35:16 +10:00
|
|
|
char *peek_line(FILE *file, int line_offset, long *position) {
|
2018-05-31 12:23:11 +10:00
|
|
|
long pos = ftell(file);
|
2018-06-02 08:35:16 +10:00
|
|
|
size_t length = 0;
|
|
|
|
char *line = NULL;
|
|
|
|
for (int i = 0; i <= line_offset; i++) {
|
2018-05-31 12:23:11 +10:00
|
|
|
ssize_t read = getline(&line, &length, file);
|
|
|
|
if (read < 0) {
|
2018-06-02 22:05:43 +10:00
|
|
|
free(line);
|
|
|
|
line = NULL;
|
2018-05-31 05:06:25 +10:00
|
|
|
break;
|
|
|
|
}
|
2018-05-31 23:07:35 +10:00
|
|
|
if (read > 0 && line[read - 1] == '\n') {
|
2018-05-31 12:23:11 +10:00
|
|
|
line[read - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (position) {
|
|
|
|
*position = ftell(file);
|
2018-05-31 05:06:25 +10:00
|
|
|
}
|
|
|
|
fseek(file, pos, SEEK_SET);
|
|
|
|
return line;
|
|
|
|
}
|