shared: move bits_to_str() to string-helpers.h

This function is useful across multiple components of the project, so
move it to a helper header.

Signed-off-by: Leandro Ribeiro <leandro.ribeiro@collabora.com>
This commit is contained in:
Leandro Ribeiro 2024-10-07 13:37:49 -03:00
parent 5fcedcf5ef
commit 4cc489c6af
2 changed files with 42 additions and 28 deletions

View file

@ -41,6 +41,7 @@
#include "libweston-internal.h"
#include <libweston/weston-log.h>
#include <libweston/helpers.h>
#include "shared/string-helpers.h"
#include "shared/xalloc.h"
/**
@ -410,34 +411,6 @@ weston_eotf_mode_to_str(enum weston_eotf_mode e)
return "???";
}
static char *
bits_to_str(uint32_t bits, const char *(*map)(uint32_t))
{
FILE *fp;
char *str = NULL;
size_t size = 0;
unsigned i;
const char *sep = "";
fp = open_memstream(&str, &size);
if (!fp)
return NULL;
for (i = 0; bits; i++) {
uint32_t bitmask = 1u << i;
if (bits & bitmask) {
fprintf(fp, "%s%s", sep, map(bitmask));
sep = ", ";
}
bits &= ~bitmask;
}
fclose(fp);
return str;
}
/** A list of EOTF modes as a string
*
* \param eotf_mask Bitwise-or'd enum weston_eotf_mode values.

View file

@ -95,6 +95,47 @@ str_printf(char **str_out, const char *fmt, ...)
*str_out = NULL;
}
/**
* Utility to print combination of enum values as string
*
* Only works for enum whose values are defined as power of two. Given a bitmask
* in which each bit represents an enum value and a function that maps each enum
* value to a string, this function returns a string (comma separated) with all
* the enum values that are present in the bitmask.
*
* \param bits The bitmask of enum values.
* \param map Function that maps enum values to string.
* \return A string combining all the enum values from the bitmask, comma
* separated. Callers must free() it.
*/
static inline char *
bits_to_str(uint32_t bits, const char *(*map)(uint32_t))
{
FILE *fp;
char *str = NULL;
size_t size = 0;
unsigned i;
const char *sep = "";
fp = open_memstream(&str, &size);
if (!fp)
return NULL;
for (i = 0; bits; i++) {
uint32_t bitmask = 1u << i;
if (bits & bitmask) {
fprintf(fp, "%s%s", sep, map(bitmask));
sep = ", ";
}
bits &= ~bitmask;
}
fclose(fp);
return str;
}
static inline const char *
yesno(bool cond)
{