Don't remove newlines when parsing config, menu and XBM

Removing newlines in rc.xml and menu.xml caused parser error with
following content:

<!--
 -
 - Some comments
 -
-->

...though it is a valid XML.

Let's not do that. I moved `grab_file()` to `buf.c` and renamed it to
`buf_from_file()`, because it now directly touches `struct buf` and
I don't like having a source file only for one function.
This commit is contained in:
tokyo4j 2025-10-15 16:36:01 +09:00 committed by Hiroaki Yamamoto
parent eebf5b3e4e
commit 7f67b9c866
8 changed files with 50 additions and 101 deletions

View file

@ -6,6 +6,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <wlr/util/log.h>
#include "common/macros.h"
#include "common/mem.h"
#include "common/string-helpers.h"
@ -203,3 +204,37 @@ buf_move(struct buf *dst, struct buf *src)
*dst = *src;
*src = BUF_INIT;
}
struct buf
buf_from_file(const char *filename)
{
struct buf buf = BUF_INIT;
FILE *stream = fopen(filename, "r");
if (!stream) {
return buf;
}
if (fseek(stream, 0, SEEK_END) == -1) {
wlr_log_errno(WLR_ERROR, "fseek(%s)", filename);
fclose(stream);
return buf;
}
long size = ftell(stream);
if (size == -1) {
wlr_log_errno(WLR_ERROR, "ftell(%s)", filename);
fclose(stream);
return buf;
}
rewind(stream);
buf_expand(&buf, size + 1);
if (fread(buf.data, 1, size, stream) == (size_t)size) {
buf.len = size;
buf.data[size] = '\0';
} else {
wlr_log_errno(WLR_ERROR, "fread(%s)", filename);
buf_reset(&buf);
}
fclose(stream);
return buf;
}

View file

@ -1,36 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Read file into memory
*
* Copyright Johan Malm 2020
*/
#define _POSIX_C_SOURCE 200809L
#include "common/grab-file.h"
#include "common/buf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct buf
grab_file(const char *filename)
{
char *line = NULL;
size_t len = 0;
FILE *stream = fopen(filename, "r");
if (!stream) {
return BUF_INIT;
}
struct buf buffer = BUF_INIT;
while ((getline(&line, &len, stream) != -1)) {
char *p = strrchr(line, '\n');
if (p) {
*p = '\0';
}
buf_add(&buffer, line);
}
free(line);
fclose(stream);
return buffer;
}

View file

@ -6,7 +6,6 @@ labwc_sources += files(
'fd-util.c',
'file-helpers.c',
'font.c',
'grab-file.c',
'graphic-helpers.c',
'lab-scene-rect.c',
'match.c',