Date:2012-07-02 01:32:56 (11 years 8 months ago)
Author:Werner Almesberger
Commit:d33449e1283c9eaf9332d2def67b2ef698cef28b
Message:tools/libtxt/: move alloc_sprintf from font.c to util.h, for later sharing

Files: tools/libtxt/Makefile (1 diff)
tools/libtxt/font.c (2 diffs)
tools/libtxt/util.h (1 diff)

Change Details

tools/libtxt/Makefile
1313
1414LIB = libtxt.a
1515
16CFLAGS = -g -Wall
16# _GNU_SOURCE for vasprintf
17CFLAGS = -g -Wall -D_GNU_SOURCE
1718
1819OBJS = edit.o font.o
1920
tools/libtxt/font.c
1111 */
1212
1313
14#define _GNU_SOURCE /* for vasprintf */
15#include <stdarg.h>
1614#include <stdint.h>
1715#include <stdlib.h>
18#include <stdio.h>
1916#include <string.h>
2017#include <errno.h>
2118
19#include "util.h"
2220#include "libtxt.h"
2321
2422
...... 
4543};
4644
4745
48/* ----- Helper functions -------------------------------------------------- */
49
50
51static const char *alloc_sprintf(const char *fmt, ...)
52{
53    va_list ap;
54    char *tmp, *res;
55    int n;
56
57    va_start(ap, fmt);
58    n = vasprintf(&tmp, fmt, ap);
59    va_end(ap);
60    if (n < 0)
61        abort();
62    res = malloc(n+1);
63    if (!res)
64        abort();
65    memcpy(res, tmp, n+1);
66    return res;
67}
68
69
7046/* ----- XBM image --------------------------------------------------------- */
7147
7248
tools/libtxt/util.h
1/*
2 * tools/libtxt/util.h - Utility functions
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef UTIL_H
14#define UTIL_H
15
16#include <stdarg.h>
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20
21
22static const char *alloc_sprintf(const char *fmt, ...)
23{
24    va_list ap;
25    char *tmp, *res;
26    int n;
27
28    va_start(ap, fmt);
29    n = vasprintf(&tmp, fmt, ap);
30    va_end(ap);
31    if (n < 0)
32        abort();
33    res = malloc(n+1);
34    if (!res)
35        abort();
36    memcpy(res, tmp, n+1);
37    return res;
38}
39
40#endif /* !UTIL_H */

Archive Download the corresponding diff file

Branches:
master
tornado-v1



interactive