memory-stream: Factor out memory-stream related code to a new file

Signed-off-by: Joshua Ashton <joshua@froggi.es>
This commit is contained in:
Joshua Ashton 2022-11-16 13:34:32 +00:00 committed by Pekka Paalanen
parent b21fdb3a0d
commit a10c478558
4 changed files with 56 additions and 33 deletions

24
include/memory-stream.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef MEMORY_STREAM_H
#define MEMORY_STREAM_H
/**
* Utility functions for memory streams.
*/
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
struct memory_stream {
FILE *fp;
char *str;
size_t str_len;
};
bool
memory_stream_open(struct memory_stream *m);
char *
memory_stream_close(struct memory_stream *m);
#endif

34
info.c
View file

@ -7,6 +7,7 @@
#include "edid.h"
#include "info.h"
#include "log.h"
#include "memory-stream.h"
/* Generated file pnp-id-table.c: */
const char *
@ -76,39 +77,6 @@ di_info_get_failure_msg(const struct di_info *info)
return info->failure_msg;
}
struct memory_stream {
FILE *fp;
char *str;
size_t str_len;
};
static bool
memory_stream_open(struct memory_stream *m)
{
*m = (struct memory_stream){ 0 };
m->fp = open_memstream(&m->str, &m->str_len);
return m->fp != NULL;
}
static char *
memory_stream_close(struct memory_stream *m)
{
char *str;
int ret;
ret = fclose(m->fp);
str = m->str;
*m = (struct memory_stream){ 0 };
if (ret != 0) {
free(str);
str = NULL;
}
return str;
}
static void
encode_ascii_byte(FILE *out, char ch)
{

30
memory-stream.c Normal file
View file

@ -0,0 +1,30 @@
#include <stdlib.h>
#include "memory-stream.h"
bool
memory_stream_open(struct memory_stream *m)
{
*m = (struct memory_stream){ 0 };
m->fp = open_memstream(&m->str, &m->str_len);
return m->fp != NULL;
}
char *
memory_stream_close(struct memory_stream *m)
{
char *str;
int ret;
ret = fclose(m->fp);
str = m->str;
*m = (struct memory_stream){ 0 };
if (ret != 0) {
free(str);
str = NULL;
}
return str;
}

View file

@ -57,6 +57,7 @@ di_lib = library(
'gtf.c',
'info.c',
'log.c',
'memory-stream.c',
pnp_id_table,
],
include_directories: include_directories('include'),