xref: /plan9/sys/src/ape/lib/fmt/fmtstr.c (revision 40ef9009116dd37656783aaadc8782c1d8bfb056)
1 /*
2  * The authors of this software are Rob Pike and Ken Thompson.
3  *              Copyright (c) 2002 by Lucent Technologies.
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose without fee is hereby granted, provided that this entire notice
6  * is included in all copies of any software which is or includes a copy
7  * or modification of this software and in all copies of the supporting
8  * documentation for such software.
9  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
11  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13  */
14 #include <stdarg.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include "utf.h"
18 #include "fmt.h"
19 #include "fmtdef.h"
20 
21 static int
fmtStrFlush(Fmt * f)22 fmtStrFlush(Fmt *f)
23 {
24 	char *s;
25 	int n;
26 
27 	n = (int)f->farg;
28 	n += 256;
29 	f->farg = (void*)n;
30 	s = (char*)f->start;
31 	f->start = realloc(s, n);
32 	if(f->start == nil){
33 		f->start = s;
34 		return 0;
35 	}
36 	f->to = (char*)f->start + ((char*)f->to - s);
37 	f->stop = (char*)f->start + n - 1;
38 	return 1;
39 }
40 
41 int
fmtstrinit(Fmt * f)42 fmtstrinit(Fmt *f)
43 {
44 	int n;
45 
46 	f->runes = 0;
47 	n = 32;
48 	f->start = malloc(n);
49 	if(f->start == nil)
50 		return -1;
51 	f->to = f->start;
52 	f->stop = (char*)f->start + n - 1;
53 	f->flush = fmtStrFlush;
54 	f->farg = (void*)n;
55 	f->nfmt = 0;
56 	return 0;
57 }
58 
59 char*
fmtstrflush(Fmt * f)60 fmtstrflush(Fmt *f)
61 {
62 	*(char*)f->to = '\0';
63 	f->to = f->start;
64 	return (char*)f->start;
65 }
66