Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <u.h>
2
#include <libc.h>
3
#include "fmtdef.h"
4
 
5
static int
6
fmtStrFlush(Fmt *f)
7
{
8
	char *s;
9
	int n;
10
 
11
	if(f->start == nil)
12
		return 0;
13
	n = (uintptr)f->farg;
14
	n *= 2;
15
	s = (char*)f->start;
16
	f->start = realloc(s, n);
17
	if(f->start == nil){
18
		f->farg = nil;
19
		f->to = nil;
20
		f->stop = nil;
21
		free(s);
22
		return 0;
23
	}
24
	f->farg = (void*)(uintptr)n;
25
	f->to = (char*)f->start + ((char*)f->to - s);
26
	f->stop = (char*)f->start + n - 1;
27
	return 1;
28
}
29
 
30
int
31
fmtstrinit(Fmt *f)
32
{
33
	int n;
34
 
35
	memset(f, 0, sizeof *f);
36
	f->runes = 0;
37
	n = 32;
38
	f->start = malloc(n);
39
	if(f->start == nil)
40
		return -1;
41
	f->to = f->start;
42
	f->stop = (char*)f->start + n - 1;
43
	f->flush = fmtStrFlush;
44
	f->farg = (void*)(uintptr)n;
45
	f->nfmt = 0;
46
	return 0;
47
}
48
 
49
/*
50
 * print into an allocated string buffer
51
 */
52
char*
53
vsmprint(char *fmt, va_list args)
54
{
55
	Fmt f;
56
	int n;
57
 
58
	if(fmtstrinit(&f) < 0)
59
		return nil;
60
	VA_COPY(f.args,args);
61
	n = dofmt(&f, fmt);
62
	VA_END(f.args);
63
	if(n < 0){
64
		free(f.start);
65
		return nil;
66
	}
67
	return fmtstrflush(&f);
68
}