1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-01 05:35:20 +02:00

tools/libtxt/: move alloc_sprintf from font.c to util.h, for later sharing

This commit is contained in:
Werner Almesberger 2012-07-01 20:32:56 -03:00
parent 8cfe660a2e
commit d33449e128
3 changed files with 43 additions and 26 deletions

View File

@ -13,7 +13,8 @@
LIB = libtxt.a LIB = libtxt.a
CFLAGS = -g -Wall # _GNU_SOURCE for vasprintf
CFLAGS = -g -Wall -D_GNU_SOURCE
OBJS = edit.o font.o OBJS = edit.o font.o

View File

@ -11,14 +11,12 @@
*/ */
#define _GNU_SOURCE /* for vasprintf */
#include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "util.h"
#include "libtxt.h" #include "libtxt.h"
@ -45,28 +43,6 @@ struct font {
}; };
/* ----- Helper functions -------------------------------------------------- */
static const char *alloc_sprintf(const char *fmt, ...)
{
va_list ap;
char *tmp, *res;
int n;
va_start(ap, fmt);
n = vasprintf(&tmp, fmt, ap);
va_end(ap);
if (n < 0)
abort();
res = malloc(n+1);
if (!res)
abort();
memcpy(res, tmp, n+1);
return res;
}
/* ----- XBM image --------------------------------------------------------- */ /* ----- XBM image --------------------------------------------------------- */

40
tools/libtxt/util.h Normal file
View File

@ -0,0 +1,40 @@
/*
* tools/libtxt/util.h - Utility functions
*
* Written 2012 by Werner Almesberger
* Copyright 2012 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef UTIL_H
#define UTIL_H
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static const char *alloc_sprintf(const char *fmt, ...)
{
va_list ap;
char *tmp, *res;
int n;
va_start(ap, fmt);
n = vasprintf(&tmp, fmt, ap);
va_end(ap);
if (n < 0)
abort();
res = malloc(n+1);
if (!res)
abort();
memcpy(res, tmp, n+1);
return res;
}
#endif /* !UTIL_H */