read_line(): Simplify by using getline()

This commit is contained in:
Michael Schupikov 2017-01-09 12:29:13 +01:00
parent f213180533
commit 22f37db15e
2 changed files with 37 additions and 48 deletions

View file

@ -1,51 +1,45 @@
#include "readline.h"
#include "log.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *read_line(FILE *file) {
size_t length = 0, size = 128;
char *string = malloc(size);
char lastChar = '\0';
if (!string) {
sway_log(L_ERROR, "Unable to allocate memory for read_line");
return NULL;
}
while (1) {
int c = getc(file);
if (c == '\n' && lastChar == '\\'){
--length; // Ignore last character.
lastChar = '\0';
continue;
}
if (c == EOF || c == '\n' || c == '\0') {
break;
}
if (c == '\r') {
continue;
}
lastChar = c;
if (length == size) {
char *new_string = realloc(string, size *= 2);
if (!new_string) {
free(string);
sway_log(L_ERROR, "Unable to allocate memory for read_line");
return NULL;
#include "log.h"
#include "readline.h"
#include "stringop.h"
/***************************************************************************//**
*
* \brief Read one line of the configuration file.
*
* \param file Pointer to the already open configuration file. If NULL is
* passed, no operation is performed.
*
* \return String containing one single line from the configuration file.
* This string is not necessarily in the format how it is expected
* for further processing of configuration commands. NULL is
* returned in case of failure or if NULL is passed for 'file'.
*
******************************************************************************/
inline char* read_line(FILE* restrict const file) {
char* line = NULL;
if (file) {
size_t line_length = 0u;
const ssize_t bytes_read = getline(&line, &line_length, file);
if (line) {
assert(line_length);
if (0 > bytes_read) {
free(line);
line = NULL;
} else {
strip_whitespace(line);
if (line[0] == '#') {
free(line);
line = NULL;
}
}
string = new_string;
}
string[length++] = c;
}
if (length + 1 == size) {
char *new_string = realloc(string, length + 1);
if (!new_string) {
free(string);
return NULL;
}
string = new_string;
}
string[length] = '\0';
return string;
return line;
}
char *read_line_buffer(FILE *file, char *string, size_t string_len) {

View file

@ -632,11 +632,6 @@ bool read_config(FILE *file, struct sway_config *config) {
continue;
}
line_number++;
strip_whitespace(line);
if (line[0] == '#') {
free(line);
continue;
}
struct cmd_results *res;
if (block == CMD_BLOCK_COMMANDS) {
// Special case