-
Notifications
You must be signed in to change notification settings - Fork 17
/
file.go
166 lines (139 loc) · 3.41 KB
/
file.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2016 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cgo
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
// on mingw, stderr and stdout are defined as &_iob[FILENO]
// on netbsd, they are defined as &__sF[FILENO]
// and cgo doesn't recognize them, so write a function to get them,
// instead of depending on internals of libc implementation.
static FILE *cgo_get_stdin(void) { return stdin; }
static FILE *cgo_get_stdout(void) { return stdout; }
static FILE *cgo_get_stderr(void) { return stderr; }
*/
import "C"
import (
"errors"
"io"
"unsafe"
)
type File C.FILE
var (
_ io.Seeker = (*File)(nil)
_ io.Reader = (*File)(nil)
_ io.Writer = (*File)(nil)
_ io.Closer = (*File)(nil)
_ io.ByteReader = (*File)(nil)
_ io.ByteWriter = (*File)(nil)
)
const (
EOF = C.EOF
SEEK_CUR = C.SEEK_CUR
SEEK_SET = C.SEEK_SET
SEEK_END = C.SEEK_END
)
var (
Stdin = (*File)(C.cgo_get_stdin())
Stdout = (*File)(C.cgo_get_stdout())
Stderr = (*File)(C.cgo_get_stderr())
)
func OpenFile(filename, mode *Char) *File {
return (*File)(C.fopen((*C.char)(filename), (*C.char)(mode)))
}
func TempFile() *File {
return (*File)(C.tmpfile())
}
func (f *File) Close() error {
n := int(C.fclose((*C.FILE)(f)))
if n != 0 {
return io.EOF
}
return nil
}
func (f *File) Flush() {
C.fflush((*C.FILE)(f))
}
func (f *File) Read(buf []byte) (int, error) {
n := int(C.fread(unsafe.Pointer(&buf[0]), C.size_t(1), C.size_t(len(buf)), (*C.FILE)(f)))
if n == len(buf) {
return n, nil
}
if f.Eof() != 0 {
return 0, io.EOF
}
err := errors.New(C.GoString(C.strerror(C.int(f.Error()))))
return 0, err
}
func (f *File) Write(buf []byte) (int, error) {
n := int(C.fwrite(unsafe.Pointer(&buf[0]), C.size_t(1), C.size_t(len(buf)), (*C.FILE)(f)))
if n == len(buf) {
return n, nil
}
if f.Eof() != 0 {
return 0, io.EOF
}
err := errors.New(C.GoString(C.strerror(C.int(f.Error()))))
return 0, err
}
func (f *File) ReadByte() (c byte, err error) {
if ch := f.Getc(); ch >= 0 {
return byte(ch), nil
}
if f.Eof() != 0 {
return 0, io.EOF
}
err = errors.New(C.GoString(C.strerror(C.int(f.Error()))))
return 0, err
}
func (f *File) WriteByte(c byte) error {
if n := f.Putc(int(c)); n != int(C.EOF) {
return nil
}
if f.Eof() != 0 {
return io.EOF
}
err := errors.New(C.GoString(C.strerror(C.int(f.Error()))))
return err
}
func (f *File) Puts(s *Char) {
C.fputs((*C.char)(s), (*C.FILE)(f))
}
func (f *File) Getc() int {
return int(C.fgetc((*C.FILE)(f)))
}
func (f *File) Gets(buf []byte) *Char {
return (*Char)(C.fgets((*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)), (*C.FILE)(f)))
}
func (f *File) Putc(ch int) int {
return int(C.fputc(C.int(ch), (*C.FILE)(f)))
}
func (f *File) Ungetc(ch int) int {
return int(C.ungetc(C.int(ch), (*C.FILE)(f)))
}
func (f *File) Tell() int64 {
return int64(C.ftell((*C.FILE)(f)))
}
func (f *File) Seek(off int64, origin int) (int64, error) {
n := int(C.fseek((*C.FILE)(f), C.long(off), C.int(origin)))
if n == 0 {
return f.Tell(), nil
}
err := errors.New(C.GoString(C.strerror(C.int(f.Error()))))
return 0, err
}
func (f *File) Rewind() {
C.rewind((*C.FILE)(f))
}
func (f *File) Eof() int {
return int(C.feof((*C.FILE)(f)))
}
func (f *File) Error() int {
return int(C.ferror((*C.FILE)(f)))
}
func (f *File) ClearErr() {
C.clearerr((*C.FILE)(f))
}