vswscanf.c 741 字节
Newer Older
1
#include "stdio_impl.h"
I
Isaac Dunham 已提交
2
#include "libc.h"
R
Rich Felker 已提交
3
#include <wchar.h>
R
Rich Felker 已提交
4

5
static size_t wstring_read(FILE *f, unsigned char *buf, size_t len)
R
Rich Felker 已提交
6
{
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
	const wchar_t *src = f->cookie;
	size_t k;

	if (!src) return 0;

	k = wcsrtombs((void *)f->buf, &src, f->buf_size, 0);
	if (k==(size_t)-1) {
		f->rpos = f->rend = 0;
		return 0;
	}

	f->rpos = f->buf;
	f->rend = f->buf + k;
	f->cookie = (void *)src;

22
	if (!len || !k) return 0;
23 24 25

	*buf = *f->rpos++;
	return 1;
R
Rich Felker 已提交
26 27
}

28
int vswscanf(const wchar_t *restrict s, const wchar_t *restrict fmt, va_list ap)
R
Rich Felker 已提交
29
{
30 31 32 33 34 35 36
	unsigned char buf[256];
	FILE f = {
		.buf = buf, .buf_size = sizeof buf,
		.cookie = (void *)s,
		.read = wstring_read, .lock = -1
	};
	return vfwscanf(&f, fmt, ap);
R
Rich Felker 已提交
37
}
I
Isaac Dunham 已提交
38 39

weak_alias(vswscanf,__isoc99_vswscanf);