Skip to content

Commit a9f9437

Browse files
committed
---
yaml --- r: 555 b: refs/heads/master c: 1db0cb2 h: refs/heads/master i: 553: c79b977 551: 2043d42 v: v3
1 parent b21bf0e commit a9f9437

File tree

5 files changed

+103
-96
lines changed

5 files changed

+103
-96
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 34e5e98cf7dc12b0c900031639c708ade084ccc0
2+
refs/heads/master: 1db0cb208de058ea2d1d5b2d2982a482d55de3b8

trunk/src/lib/_io.rs

Lines changed: 99 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,150 +5,154 @@ import std._vec;
55

66
type stdio_reader = unsafe obj {
77
fn getc() -> int;
8+
fn ungetc(int i);
89
};
910

1011
fn new_stdio_reader(str path) -> stdio_reader {
11-
unsafe obj stdio_FILE_reader(os.libc.FILE f) {
12-
fn getc() -> int {
13-
ret os.libc.fgetc(f);
14-
}
15-
drop {
16-
os.libc.fclose(f);
12+
unsafe obj stdio_FILE_reader(os.libc.FILE f) {
13+
fn getc() -> int {
14+
ret os.libc.fgetc(f);
15+
}
16+
fn ungetc(int i) {
17+
os.libc.ungetc(i, f);
18+
}
19+
drop {
20+
os.libc.fclose(f);
21+
}
1722
}
18-
}
19-
ret stdio_FILE_reader(os.libc.fopen(_str.buf(path),
20-
_str.buf("r")));
23+
ret stdio_FILE_reader(os.libc.fopen(_str.buf(path),
24+
_str.buf("r")));
2125
}
2226

2327

2428
type buf_reader = unsafe obj {
25-
fn read() -> vec[u8];
29+
fn read() -> vec[u8];
2630
};
2731

2832
type buf_writer = unsafe obj {
29-
fn write(vec[u8] v);
33+
fn write(vec[u8] v);
3034
};
3135

3236
fn default_bufsz() -> uint {
33-
ret 4096u;
37+
ret 4096u;
3438
}
3539

3640
fn new_buf() -> vec[u8] {
37-
ret _vec.alloc[u8](default_bufsz());
41+
ret _vec.alloc[u8](default_bufsz());
3842
}
3943

4044
fn new_buf_reader(str path) -> buf_reader {
4145

42-
unsafe obj fd_buf_reader(int fd, mutable vec[u8] buf) {
46+
unsafe obj fd_buf_reader(int fd, mutable vec[u8] buf) {
4347

44-
fn read() -> vec[u8] {
48+
fn read() -> vec[u8] {
4549

46-
// Ensure our buf is singly-referenced.
47-
if (_vec.rustrt.refcount[u8](buf) != 1u) {
48-
buf = new_buf();
49-
}
50+
// Ensure our buf is singly-referenced.
51+
if (_vec.rustrt.refcount[u8](buf) != 1u) {
52+
buf = new_buf();
53+
}
5054

51-
auto len = default_bufsz();
52-
auto vbuf = _vec.buf[u8](buf);
53-
auto count = os.libc.read(fd, vbuf, len);
55+
auto len = default_bufsz();
56+
auto vbuf = _vec.buf[u8](buf);
57+
auto count = os.libc.read(fd, vbuf, len);
5458

55-
if (count < 0) {
56-
log "error filling buffer";
57-
log sys.rustrt.last_os_error();
58-
fail;
59-
}
59+
if (count < 0) {
60+
log "error filling buffer";
61+
log sys.rustrt.last_os_error();
62+
fail;
63+
}
64+
65+
_vec.len_set[u8](buf, count as uint);
66+
ret buf;
67+
}
6068

61-
_vec.len_set[u8](buf, count as uint);
62-
ret buf;
69+
drop {
70+
os.libc.close(fd);
71+
}
6372
}
6473

65-
drop {
66-
os.libc.close(fd);
74+
auto fd = os.libc.open(_str.buf(path),
75+
os.libc_constants.O_RDONLY() |
76+
os.libc_constants.O_BINARY(),
77+
0u);
78+
79+
if (fd < 0) {
80+
log "error opening file for reading";
81+
log sys.rustrt.last_os_error();
82+
fail;
6783
}
68-
}
69-
70-
auto fd = os.libc.open(_str.buf(path),
71-
os.libc_constants.O_RDONLY() |
72-
os.libc_constants.O_BINARY(),
73-
0u);
74-
75-
if (fd < 0) {
76-
log "error opening file for reading";
77-
log sys.rustrt.last_os_error();
78-
fail;
79-
}
80-
ret fd_buf_reader(fd, new_buf());
84+
ret fd_buf_reader(fd, new_buf());
8185
}
8286

8387
type fileflag = tag(append(), create(), truncate());
8488

8589
fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
8690

87-
unsafe obj fd_buf_writer(int fd) {
88-
89-
fn write(vec[u8] v) {
90-
auto len = _vec.len[u8](v);
91-
auto count = 0u;
92-
auto vbuf;
93-
while (count < len) {
94-
vbuf = _vec.buf_off[u8](v, count);
95-
auto nout = os.libc.write(fd, vbuf, len);
96-
if (nout < 0) {
97-
log "error dumping buffer";
98-
log sys.rustrt.last_os_error();
99-
fail;
91+
unsafe obj fd_buf_writer(int fd) {
92+
93+
fn write(vec[u8] v) {
94+
auto len = _vec.len[u8](v);
95+
auto count = 0u;
96+
auto vbuf;
97+
while (count < len) {
98+
vbuf = _vec.buf_off[u8](v, count);
99+
auto nout = os.libc.write(fd, vbuf, len);
100+
if (nout < 0) {
101+
log "error dumping buffer";
102+
log sys.rustrt.last_os_error();
103+
fail;
104+
}
105+
count += nout as uint;
106+
}
107+
}
108+
109+
drop {
110+
os.libc.close(fd);
100111
}
101-
count += nout as uint;
102-
}
103112
}
104113

105-
drop {
106-
os.libc.close(fd);
114+
let int fflags =
115+
os.libc_constants.O_WRONLY() |
116+
os.libc_constants.O_BINARY();
117+
118+
for (fileflag f in flags) {
119+
alt (f) {
120+
case (append()) { fflags |= os.libc_constants.O_APPEND(); }
121+
case (create()) { fflags |= os.libc_constants.O_CREAT(); }
122+
case (truncate()) { fflags |= os.libc_constants.O_TRUNC(); }
123+
}
107124
}
108-
}
109125

110-
let int fflags =
111-
os.libc_constants.O_WRONLY() |
112-
os.libc_constants.O_BINARY();
126+
auto fd = os.libc.open(_str.buf(path),
127+
fflags,
128+
os.libc_constants.S_IRUSR() |
129+
os.libc_constants.S_IWUSR());
113130

114-
for (fileflag f in flags) {
115-
alt (f) {
116-
case (append()) { fflags |= os.libc_constants.O_APPEND(); }
117-
case (create()) { fflags |= os.libc_constants.O_CREAT(); }
118-
case (truncate()) { fflags |= os.libc_constants.O_TRUNC(); }
131+
if (fd < 0) {
132+
log "error opening file for writing";
133+
log sys.rustrt.last_os_error();
134+
fail;
119135
}
120-
}
121-
122-
auto fd = os.libc.open(_str.buf(path),
123-
fflags,
124-
os.libc_constants.S_IRUSR() |
125-
os.libc_constants.S_IWUSR());
126-
127-
if (fd < 0) {
128-
log "error opening file for writing";
129-
log sys.rustrt.last_os_error();
130-
fail;
131-
}
132-
ret fd_buf_writer(fd);
136+
ret fd_buf_writer(fd);
133137
}
134138

135139
type writer =
136-
unsafe obj {
137-
fn write_str(str s);
138-
fn write_int(int n);
139-
fn write_uint(uint n);
140-
};
140+
unsafe obj {
141+
fn write_str(str s);
142+
fn write_int(int n);
143+
fn write_uint(uint n);
144+
};
141145

142146
fn file_writer(str path,
143147
vec[fileflag] flags)
144-
-> writer
148+
-> writer
145149
{
146-
unsafe obj fw(buf_writer out) {
147-
fn write_str(str s) { out.write(_str.bytes(s)); }
148-
fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
149-
fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
150-
}
151-
ret fw(new_buf_writer(path, flags));
150+
unsafe obj fw(buf_writer out) {
151+
fn write_str(str s) { out.write(_str.bytes(s)); }
152+
fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
153+
fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
154+
}
155+
ret fw(new_buf_writer(path, flags));
152156
}
153157

154158
//

trunk/src/lib/linux_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ native mod libc = "libc.so.6" {
1212
fn fopen(sbuf path, sbuf mode) -> FILE;
1313
fn fclose(FILE f);
1414
fn fgetc(FILE f) -> int;
15+
fn ungetc(int c, FILE f);
1516

1617
type dir;
1718
// readdir is a mess; handle via wrapper function in rustrt.

trunk/src/lib/macos_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ native mod libc = "libc.dylib" {
1212
fn fopen(sbuf path, sbuf mode) -> FILE;
1313
fn fclose(FILE f);
1414
fn fgetc(FILE f) -> int;
15+
fn ungetc(int c, FILE f);
1516

1617
type dir;
1718
// readdir is a mess; handle via wrapper function in rustrt.

trunk/src/lib/win32_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ native mod libc = "msvcrt.dll" {
1111
fn fopen(sbuf path, sbuf mode) -> FILE;
1212
fn fclose(FILE f);
1313
fn fgetc(FILE f) -> int;
14+
fn ungetc(int c, FILE f);
1415
}
1516

1617
mod libc_constants {

0 commit comments

Comments
 (0)