mirror of
https://github.com/mborgerson/xemu.git
synced 2025-12-01 16:10:01 +00:00
util/mstring: Use GString
This commit is contained in:
@ -2,69 +2,83 @@
|
|||||||
#define MSTRING_H
|
#define MSTRING_H
|
||||||
|
|
||||||
#include "qemu/osdep.h"
|
#include "qemu/osdep.h"
|
||||||
#include <string.h>
|
#include "glib.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int ref;
|
GString *gstr;
|
||||||
gchar *string;
|
int refcnt;
|
||||||
} MString;
|
} MString;
|
||||||
|
|
||||||
void mstring_append_fmt(MString *mstring, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
|
static inline void mstring_ref(MString *mstr)
|
||||||
MString *mstring_from_fmt(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
|
|
||||||
void mstring_append_va(MString *mstring, const char *fmt, va_list va) __attribute__ ((format (printf, 2, 0)));
|
|
||||||
|
|
||||||
static inline
|
|
||||||
void mstring_ref(MString *mstr)
|
|
||||||
{
|
{
|
||||||
mstr->ref++;
|
mstr->refcnt++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline void mstring_unref(MString *mstr)
|
||||||
void mstring_unref(MString *mstr)
|
|
||||||
{
|
{
|
||||||
mstr->ref--;
|
mstr->refcnt--;
|
||||||
if (!mstr->ref) {
|
if (mstr->refcnt == 0) {
|
||||||
g_free(mstr->string);
|
g_string_free(mstr->gstr, true);
|
||||||
g_free(mstr);
|
g_free(mstr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(MString, mstring_unref)
|
||||||
void mstring_append(MString *mstr, const char *str)
|
|
||||||
{
|
|
||||||
gchar *n = g_strconcat(mstr->string, str, NULL);
|
|
||||||
g_free(mstr->string);
|
|
||||||
mstr->string = n;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline
|
static inline MString *mstring_new(void)
|
||||||
MString *mstring_new(void)
|
|
||||||
{
|
{
|
||||||
MString *mstr = g_malloc(sizeof(MString));
|
MString *mstr = g_malloc(sizeof(MString));
|
||||||
mstr->ref = 1;
|
mstr->refcnt = 1;
|
||||||
mstr->string = g_strdup("");
|
mstr->gstr = g_string_new("");
|
||||||
return mstr;
|
return mstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline MString *mstring_from_str(const char *str)
|
||||||
MString *mstring_from_str(const char *str)
|
|
||||||
{
|
{
|
||||||
MString *mstr = g_malloc(sizeof(MString));
|
MString *mstr = g_malloc(sizeof(MString));
|
||||||
mstr->ref = 1;
|
mstr->refcnt = 1;
|
||||||
mstr->string = g_strdup(str);
|
mstr->gstr = g_string_new(str);
|
||||||
return mstr;
|
return mstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline __attribute__((format(printf, 1, 2))) MString *
|
||||||
const gchar *mstring_get_str(MString *mstr)
|
mstring_from_fmt(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
return mstr->string;
|
MString *mstr = g_malloc(sizeof(MString));
|
||||||
|
mstr->refcnt = 1;
|
||||||
|
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
// FIXME: Use g_string_new_take (GLib 2.78+)
|
||||||
|
g_autofree gchar *str = g_strdup_vprintf(fmt, args);
|
||||||
|
mstr->gstr = g_string_new(str);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return mstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline void mstring_append(MString *mstr, const char *str)
|
||||||
size_t mstring_get_length(MString *mstr)
|
|
||||||
{
|
{
|
||||||
return strlen(mstr->string);
|
g_string_append(mstr->gstr, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline __attribute__((format(printf, 2, 3))) void
|
||||||
|
mstring_append_fmt(MString *mstr, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
g_string_append_vprintf(mstr->gstr, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline const gchar *mstring_get_str(MString *mstr)
|
||||||
|
{
|
||||||
|
return mstr->gstr->str;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline size_t mstring_get_length(MString *mstr)
|
||||||
|
{
|
||||||
|
return mstr->gstr->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -68,7 +68,6 @@ if host_os == 'windows'
|
|||||||
util_ss.add(files('miniz/miniz.c'))
|
util_ss.add(files('miniz/miniz.c'))
|
||||||
endif
|
endif
|
||||||
util_ss.add(files('fast-hash.c'))
|
util_ss.add(files('fast-hash.c'))
|
||||||
util_ss.add(files('mstring.c'))
|
|
||||||
|
|
||||||
if have_user
|
if have_user
|
||||||
util_ss.add(files('selfmap.c'))
|
util_ss.add(files('selfmap.c'))
|
||||||
|
|||||||
@ -1,49 +0,0 @@
|
|||||||
#include "qemu/osdep.h"
|
|
||||||
#include "qemu/mstring.h"
|
|
||||||
|
|
||||||
#include <stdarg.h>
|
|
||||||
|
|
||||||
void mstring_append_fmt(MString *qstring, const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
va_start(ap, fmt);
|
|
||||||
mstring_append_va(qstring, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
}
|
|
||||||
|
|
||||||
MString *mstring_from_fmt(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
MString *ret = mstring_new();
|
|
||||||
va_list ap;
|
|
||||||
va_start(ap, fmt);
|
|
||||||
mstring_append_va(ret, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mstring_append_va(MString *qstring, const char *fmt, va_list va)
|
|
||||||
{
|
|
||||||
char scratch[256];
|
|
||||||
|
|
||||||
va_list ap;
|
|
||||||
va_copy(ap, va);
|
|
||||||
const int len = vsnprintf(scratch, sizeof(scratch), fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
if (len == 0) {
|
|
||||||
return;
|
|
||||||
} else if (len < sizeof(scratch)) {
|
|
||||||
mstring_append(qstring, scratch);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* overflowed out scratch buffer, alloc and try again */
|
|
||||||
char *buf = g_malloc(len + 1);
|
|
||||||
va_copy(ap, va);
|
|
||||||
vsnprintf(buf, len + 1, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
mstring_append(qstring, buf);
|
|
||||||
g_free(buf);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user