aboutsummaryrefslogtreecommitdiff
path: root/include/libfmt.h
blob: 7ba40a023ccda4ee195558003fe47cad5933bce8 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once

typedef struct fmt·State fmt·State;

struct fmt·State
{
    struct{
        char *beg;
        char *cur;
        char *end;
    } buffer;
    int n;

    va_list args;
    rune  verb;
    ulong flag;
    int  width;
    int  prec;
    char *thousands, *groups, *decimal;

    void *file;
    int (*flush)(fmt·State *);
    struct {
        void *heap;
        mem·Reallocator mem;
    };
};

#define iota(x) (1 << (x))
enum
{
    fmt·Width    = iota(0),
    fmt·Left     = iota(1),
    fmt·Prec     = iota(2),
    fmt·Sharp    = iota(3),
    fmt·Space    = iota(4),
    fmt·Sign     = iota(5),
    fmt·Apost    = iota(6),
    fmt·Zero     = iota(7),
    fmt·Unsigned = iota(8),
    fmt·Short    = iota(9),
    fmt·Long     = iota(10),
    fmt·Vlong    = iota(11),
    fmt·Comma    = iota(12),
    fmt·Byte     = iota(13),
    fmt·Ldouble  = iota(14),
    fmt·Flag     = iota(15),
};
#undef iota

/* normal printing interface */
int   fmt·print(char *fmt, ...);
int   fmt·fprint(int fd, char *fmt, ...);

int   fmt·sprint(char *buf, char *fmt, ...);
int   fmt·nsprint(int len, char *buf, char *fmt, ...);
char *fmt·esprint(char *buf, char *end, char *fmt, ...);

int   fmt·vprint(char *fmt, va_list args);
int   fmt·vfprint(int fd, char *fmt, va_list args);
int   fmt·vnsprint(int len, char *buf, char *fmt, va_list args);
char *fmt·vesprint(char *buf, char *end, char *fmt, va_list args);

/* low-level interface: used for custom printing verbs */
int   fmt·open(int fd, int len, char *buf, fmt·State *);    // creates a buffer on a file
int   fmt·make(mem·Reallocator mem, void *heap, fmt·State *); // creates an in-memory buffer
void  fmt·free(fmt·State *); // releases an in-memory buffer

int   fmt·do(fmt·State *, char *fmt);
int   fmt·write(fmt·State *, char *fmt, ...);
int   fmt·vwrite(fmt·State *, char *fmt, va_list args);
int   fmt·install(int c, int (*format)(fmt·State *));