summaryrefslogtreecommitdiff
path: root/libphobos/libdruntime/core/sys/linux/stdio.d
blob: 2d079fd71f7ffbabdfa7c76b9dc0b91ea680e543 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
  * D header file for GNU stdio.
  *
  * Copyright: Danny Milosavljevic 2014
  * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
  * Authors: Danny Milosavljevic
  */
module core.sys.linux.stdio;
version (CRuntime_Glibc):
public import core.sys.posix.stdio;
import core.sys.posix.sys.types : ssize_t, off64_t = off_t;
import core.sys.linux.config : __USE_FILE_OFFSET64;
import core.stdc.stdio : FILE;
import core.stdc.stddef : wchar_t;


extern(C) nothrow
{
    alias ssize_t function(void *cookie, char *buf, size_t size) cookie_read_function_t;
    alias ssize_t function(void *cookie, const(char) *buf, size_t size) cookie_write_function_t;
    alias int function(void *cookie, off64_t *offset, int whence) cookie_seek_function_t;
    alias int function(void *cookie) cookie_close_function_t;

    struct cookie_io_functions_t
    {
        cookie_read_function_t read;
        cookie_write_function_t write;
        cookie_seek_function_t seek;
        cookie_close_function_t close;
    }
    FILE* fopencookie(void* cookie, const(char)* mode, cookie_io_functions_t io_funcs);
    void setbuffer(FILE *stream, char *buf, size_t size); // note: _DEFAULT_SOURCE
}

unittest
{
    static int flag = 0;
    static int written = 0;
    static int closed = 0;
    // Note: this test needs to run on both a 32 and a 64 bit machine, both have to pass.
    import core.stdc.errno : errno, EBADF;
    //import core.sys.posix.sys.stat : off64_t;
    import core.stdc.stdio : FILE, fflush, fileno, fprintf, fgetc, EOF, fclose;
    import core.stdc.string : memcpy, memset;
    extern (C) ssize_t specialRead(void *cookie, char *buf, size_t size) nothrow
    {
        memset(buf, 'a', size);
        return size;
    }
    extern (C) int specialClose(void* cookie) nothrow
    {
        ++closed;
        return 0;
    }
    extern (C) ssize_t specialWrite(void *cookie, const(char) *buf, size_t size) nothrow
    {
        int* c = cast(int*) cookie;
        flag = *c;
        written += size;
        return size;
    }
    int dummy = 42;
    cookie_io_functions_t fns =
    {
        read: &specialRead,
        write: &specialWrite,
        close: &specialClose,
    };
    FILE* f = fopencookie(&dummy, "r+", fns);
    assert(f !is null);
    //File.open();
    //auto g = File(f);
    assert(fileno(f) == -1 && errno == EBADF);
    assert(fprintf(f, "hello") == "hello".length);
    //assert(errno == EBADF);
    assert(fflush(f) == 0);
    assert(written == "hello".length);
    // Note: do not swap reading and writing here.
    int c = 0;
    while ((c = fgetc(f)) != EOF)
    {
        assert(c == 'a');
        break; // endless otherwise
    }
    assert(c == 'a');
    assert(fclose(f) == 0);
    assert(closed == 1);
    assert(flag == dummy);
    //stdin.getFP();
    //assert(stdin.fileno() == 0);
}

unittest
{ /* setbuffer */
    char buf;
    int c;
    byte[10] dummy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    FILE* f = fmemopen(dummy.ptr, 10, "r");
    assert(f !is null);
    setbuffer(f, &buf, 1);
    assert(fgetc(f) == 1);
    assert(fgetc(f) == 2);
    assert(fgetc(f) == 3);
    assert(fgetc(f) == 4);
    assert(fclose(f) == 0);
}