Skip to content

Commit b537b34

Browse files
committed
---
yaml --- r: 1868 b: refs/heads/master c: 739c4ae h: refs/heads/master v: v3
1 parent d07a130 commit b537b34

File tree

6 files changed

+86
-26
lines changed

6 files changed

+86
-26
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: 91c2b82b0947eece64df03f9e03d65a27d4ef0bf
2+
refs/heads/master: 739c4aedf1efd9df5abcad097a3137ed1025c556

trunk/src/lib/io.rs

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,25 @@ native "rust" mod rustrt {
1111

1212
tag seek_style {seek_set; seek_end; seek_cur;}
1313

14+
// The raw underlying reader class. All readers must implement this.
15+
type buf_reader =
16+
state obj {
17+
impure fn read(uint len) -> vec[u8];
18+
impure fn unread_byte(int byte);
19+
impure fn eof() -> bool;
20+
21+
// FIXME: Seekable really should be orthogonal. We will need
22+
// inheritance.
23+
impure fn seek(int offset, seek_style whence);
24+
impure fn tell() -> uint;
25+
};
26+
27+
// Convenience methods for reading.
1428
type reader =
1529
state obj {
30+
// FIXME: This should inherit from buf_reader.
31+
impure fn get_buf_reader() -> buf_reader;
32+
1633
impure fn read_byte() -> int;
1734
impure fn unread_byte(int byte);
1835
impure fn read_bytes(uint len) -> vec[u8];
@@ -35,21 +52,58 @@ fn convert_whence(seek_style whence) -> int {
3552
}
3653
}
3754

38-
state obj FILE_reader(os.libc.FILE f, bool must_close) {
39-
impure fn read_byte() -> int {
40-
ret os.libc.fgetc(f);
55+
state obj FILE_buf_reader(os.libc.FILE f, bool must_close) {
56+
impure fn read(uint len) -> vec[u8] {
57+
auto buf = _vec.alloc[u8](len);
58+
auto read = os.libc.fread(_vec.buf[u8](buf), 1u, len, f);
59+
_vec.len_set[u8](buf, read);
60+
ret buf;
4161
}
4262
impure fn unread_byte(int byte) {
4363
os.libc.ungetc(byte, f);
4464
}
65+
impure fn eof() -> bool {
66+
ret os.libc.feof(f) != 0;
67+
}
68+
impure fn seek(int offset, seek_style whence) {
69+
check (os.libc.fseek(f, offset, convert_whence(whence)) == 0);
70+
}
71+
impure fn tell() -> uint {
72+
ret os.libc.ftell(f) as uint;
73+
}
74+
drop {
75+
if (must_close) { os.libc.fclose(f); }
76+
}
77+
}
78+
79+
// FIXME: When we have a "self" keyword, move this into read_byte(). This is
80+
// only here so that multiple method implementations below can use it.
81+
//
82+
// FIXME: Return value should be option[u8], not int.
83+
impure fn read_byte_from_buf_reader(buf_reader rdr) -> int {
84+
auto buf = rdr.read(1u);
85+
if (_vec.len[u8](buf) == 0u) {
86+
ret -1;
87+
}
88+
ret buf.(0) as int;
89+
}
90+
91+
// FIXME: Convert this into pseudomethods on buf_reader.
92+
state obj new_reader(buf_reader rdr) {
93+
impure fn get_buf_reader() -> buf_reader {
94+
ret rdr;
95+
}
96+
impure fn read_byte() -> int {
97+
ret read_byte_from_buf_reader(rdr);
98+
}
99+
impure fn unread_byte(int byte) {
100+
ret rdr.unread_byte(byte);
101+
}
45102
impure fn read_bytes(uint len) -> vec[u8] {
46-
auto buf = _vec.alloc[u8](len);
47-
auto read = os.libc.fread(_vec.buf[u8](buf), 1u, len, f);
48-
_vec.len_set[u8](buf, read);
49-
ret buf;
103+
ret rdr.read(len);
50104
}
51105
impure fn read_char() -> char {
52-
auto c0 = os.libc.fgetc(f);
106+
auto c0 = read_byte_from_buf_reader(rdr);
53107
if (c0 == -1) {ret -1 as char;} // FIXME will this stay valid?
54108
auto b0 = c0 as u8;
55109
auto w = _str.utf8_char_width(b0);
@@ -58,7 +112,7 @@ state obj FILE_reader(os.libc.FILE f, bool must_close) {
58112
auto val = 0u;
59113
while (w > 1u) {
60114
w -= 1u;
61-
auto next = os.libc.fgetc(f);
115+
auto next = read_byte_from_buf_reader(rdr);
62116
check(next > -1);
63117
check(next & 0xc0 == 0x80);
64118
val <<= 6u;
@@ -69,17 +123,14 @@ state obj FILE_reader(os.libc.FILE f, bool must_close) {
69123
ret val as char;
70124
}
71125
impure fn eof() -> bool {
72-
auto ch = os.libc.fgetc(f);
73-
if (ch == -1) {ret true;}
74-
os.libc.ungetc(ch, f);
75-
ret false;
126+
ret rdr.eof();
76127
}
77128
impure fn read_line() -> str {
78129
let vec[u8] buf = vec();
79130
// No break yet in rustc
80131
auto go_on = true;
81132
while (go_on) {
82-
auto ch = os.libc.fgetc(f);
133+
auto ch = read_byte_from_buf_reader(rdr);
83134
if (ch == -1 || ch == 10) {go_on = false;}
84135
else {_vec.push[u8](buf, ch as u8);}
85136
}
@@ -89,7 +140,7 @@ state obj FILE_reader(os.libc.FILE f, bool must_close) {
89140
let vec[u8] buf = vec();
90141
auto go_on = true;
91142
while (go_on) {
92-
auto ch = os.libc.fgetc(f);
143+
auto ch = read_byte_from_buf_reader(rdr);
93144
if (ch < 1) {go_on = false;}
94145
else {_vec.push[u8](buf, ch as u8);}
95146
}
@@ -100,7 +151,7 @@ state obj FILE_reader(os.libc.FILE f, bool must_close) {
100151
auto val = 0u;
101152
auto pos = 0u;
102153
while (size > 0u) {
103-
val += (os.libc.fgetc(f) as uint) << pos;
154+
val += (read_byte_from_buf_reader(rdr) as uint) << pos;
104155
pos += 8u;
105156
size -= 1u;
106157
}
@@ -110,25 +161,22 @@ state obj FILE_reader(os.libc.FILE f, bool must_close) {
110161
auto val = 0u;
111162
auto pos = 0u;
112163
while (size > 0u) {
113-
val += (os.libc.fgetc(f) as uint) << pos;
164+
val += (read_byte_from_buf_reader(rdr) as uint) << pos;
114165
pos += 8u;
115166
size -= 1u;
116167
}
117168
ret val as int;
118169
}
119170
impure fn seek(int offset, seek_style whence) {
120-
check(os.libc.fseek(f, offset, convert_whence(whence)) == 0);
171+
ret rdr.seek(offset, whence);
121172
}
122173
impure fn tell() -> uint {
123-
ret os.libc.ftell(f) as uint;
124-
}
125-
drop {
126-
if (must_close) {os.libc.fclose(f);}
174+
ret rdr.tell();
127175
}
128176
}
129177

130178
fn stdin() -> reader {
131-
ret FILE_reader(rustrt.rust_get_stdin(), false);
179+
ret new_reader(FILE_buf_reader(rustrt.rust_get_stdin(), false));
132180
}
133181

134182
fn file_reader(str path) -> reader {
@@ -137,9 +185,17 @@ fn file_reader(str path) -> reader {
137185
log "error opening " + path;
138186
fail;
139187
}
140-
ret FILE_reader(f, true);
188+
ret new_reader(FILE_buf_reader(f, true));
141189
}
142190

191+
192+
// Byte buffer readers
193+
194+
//state obj byte_buf_reader(vec[mutable? u8] buf) {
195+
// fn read(
196+
//}
197+
198+
143199
// Writing
144200

145201
tag fileflag {
@@ -152,6 +208,7 @@ tag fileflag {
152208
type buf_writer = state obj {
153209
fn write(vec[u8] v);
154210

211+
// FIXME: Seekable really should be orthogonal. We will need inheritance.
155212
fn seek(int offset, seek_style whence);
156213
fn tell() -> uint; // FIXME: eventually u64
157214
};

trunk/src/lib/linux_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ native mod libc = "libc.so.6" {
1717
fn fclose(FILE f);
1818
fn fgetc(FILE f) -> int;
1919
fn ungetc(int c, FILE f);
20+
fn feof(FILE f) -> int;
2021
fn fread(vbuf buf, uint size, uint n, FILE f) -> uint;
2122
fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint;
2223
fn fseek(FILE f, int offset, int whence) -> int;

trunk/src/lib/macos_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ native mod libc = "libc.dylib" {
1414
fn fclose(FILE f);
1515
fn fgetc(FILE f) -> int;
1616
fn ungetc(int c, FILE f);
17+
fn feof(FILE f) -> int;
1718
fn fread(vbuf buf, uint size, uint n, FILE f) -> uint;
1819
fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint;
1920
fn fseek(FILE f, int offset, int whence) -> int;

trunk/src/lib/run_program.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impure fn start_program(str prog, vec[str] args) -> @program {
4848
ret io.new_writer(io.fd_buf_writer(in_fd, false));
4949
}
5050
fn output() -> io.reader {
51-
ret io.FILE_reader(out_file, false);
51+
ret io.new_reader(io.FILE_buf_reader(out_file, false));
5252
}
5353
impure fn close_input() {
5454
os.libc.close(in_fd);

trunk/src/lib/win32_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ native mod libc = "msvcrt.dll" {
1313
fn fclose(FILE f);
1414
fn fgetc(FILE f) -> int;
1515
fn ungetc(int c, FILE f);
16+
fn feof(FILE f) -> int;
1617
fn fread(vbuf buf, uint size, uint n, FILE f) -> uint;
1718
fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint;
1819
fn fseek(FILE f, int offset, int whence) -> int;

0 commit comments

Comments
 (0)