summaryrefslogtreecommitdiff
path: root/libc/src/yscanf.c
blob: 3d2a488e3b5bf56a35f577188b1894102b47dd40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "stdio.h"

struct opaque_t {
    union {
        const char * p;
        int fd;
    };
    uint8_t bsize;
    char backbuffer[3];
};

static int str_getc(void * _opaque) {
    struct opaque_t * opaque = (struct opaque_t *) _opaque;
    int r;

    if (opaque->bsize)
        return opaque->backbuffer[--(opaque->bsize)];
    
    r = *(opaque->p++);
    
    return r ? r : EOF;
}

static int file_getc(void * _opaque) {
    struct opaque_t * opaque = (struct opaque_t *) _opaque;
    uint8_t v;

    if (opaque->bsize)
        return opaque->backbuffer[--(opaque->bsize)];
    
    if (read(opaque->fd, &v, 1) != 1)
        return EOF;
    return v;
}

static void scanf_ungetc(void * _opaque, int c) {
    struct opaque_t * opaque = (struct opaque_t *) _opaque;
    
    if (opaque->bsize < sizeof(opaque->backbuffer))
        opaque->backbuffer[opaque->bsize++] = c;
}

int vsscanf(const char * str, const char * format, va_list ap) {
    struct opaque_t opaque;
    opaque.p = str;
    opaque.bsize = 0;
    return vxscanf(str_getc, scanf_ungetc, &opaque, format, ap);
}

int vdscanf(int fd, const char * format, va_list ap) {
    struct opaque_t opaque;
    opaque.fd = fd;
    opaque.bsize = 0;
    return vxscanf(file_getc, scanf_ungetc, &opaque, format, ap);
}