@@ -91,6 +91,7 @@ tag fileflag {
91
91
truncate;
92
92
}
93
93
94
+ // FIXME move into fd_buf_writer
94
95
fn writefd ( int fd, vec[ u8] v ) {
95
96
auto len = _vec. len [ u8] ( v) ;
96
97
auto count = 0 u;
@@ -107,19 +108,17 @@ fn writefd(int fd, vec[u8] v) {
107
108
}
108
109
}
109
110
110
- fn new_buf_writer ( str path , vec[ fileflag] flags ) -> buf_writer {
111
-
112
- state obj fd_buf_writer ( int fd) {
113
-
114
- fn write ( vec[ u8] v ) {
115
- writefd ( fd, v) ;
116
- }
111
+ state obj fd_buf_writer ( int fd, bool must_close) {
112
+ fn write ( vec[ u8] v ) {
113
+ writefd ( fd, v) ;
114
+ }
117
115
118
- drop {
119
- os. libc . close ( fd) ;
120
- }
116
+ drop {
117
+ if ( must_close) { os. libc . close ( fd) ; }
121
118
}
119
+ }
122
120
121
+ fn file_buf_writer( str path , vec[ fileflag] flags ) -> buf_writer {
123
122
let int fflags =
124
123
os. libc_constants . O_WRONLY ( ) |
125
124
os. libc_constants . O_BINARY ( ) ;
@@ -142,26 +141,52 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
142
141
log sys. rustrt . last_os_error ( ) ;
143
142
fail;
144
143
}
145
- ret fd_buf_writer ( fd) ;
144
+ ret fd_buf_writer ( fd, true ) ;
146
145
}
147
146
148
147
type writer =
149
148
state obj {
150
- fn write_str( str s) ;
151
- fn write_int ( int n) ;
152
- fn write_uint ( uint n) ;
149
+ impure fn write_str( str s) ;
150
+ impure fn write_int ( int n) ;
151
+ impure fn write_uint ( uint n) ;
153
152
} ;
154
153
155
- fn file_writer ( str path ,
156
- vec[ fileflag] flags )
157
- -> writer
158
- {
159
- state obj fw( buf_writer out) {
160
- fn write_str ( str s) { out. write ( _str. bytes ( s) ) ; }
161
- fn write_int ( int n) { out. write ( _str. bytes ( _int. to_str ( n, 10 u) ) ) ; }
162
- fn write_uint ( uint n) { out. write ( _str. bytes ( _uint. to_str ( n, 10 u) ) ) ; }
154
+ state obj new_writer ( buf_writer out) {
155
+ impure fn write_str ( str s) { out. write ( _str. bytes ( s) ) ; }
156
+ impure fn write_int ( int n) { out. write ( _str. bytes ( _int. to_str ( n, 10 u) ) ) ; }
157
+ impure fn write_uint ( uint n) { out. write ( _str. bytes ( _uint. to_str ( n, 10 u) ) ) ; }
158
+ }
159
+
160
+ fn file_writer ( str path , vec[ fileflag] flags ) -> writer {
161
+ ret new_writer ( file_buf_writer ( path, flags) ) ;
162
+ }
163
+
164
+ // FIXME it would be great if this could be a const named stdout
165
+ fn stdout_writer ( ) -> writer {
166
+ ret new_writer ( fd_buf_writer ( 1 , false ) ) ;
167
+ }
168
+
169
+ type str_writer =
170
+ state obj {
171
+ fn get_writer( ) -> writer;
172
+ fn get_str ( ) -> str ;
173
+ } ;
174
+
175
+ type str_buf = @rec( mutable str buf) ;
176
+
177
+ // TODO awkward! it's not possible to implement a writer with an extra method
178
+ fn string_writer( ) -> str_writer {
179
+ auto buf = @rec( mutable buf = "") ;
180
+ state obj str_writer_writer( str_buf buf) {
181
+ impure fn write_str( str s) { buf. buf += s; }
182
+ impure fn write_int ( int n) { buf. buf += _int. to_str ( n, 10 u) ; }
183
+ impure fn write_uint ( uint n) { buf. buf += _uint. to_str ( n, 10 u) ; }
184
+ }
185
+ state obj str_writer_wrap ( writer wr, str_buf buf) {
186
+ fn get_writer ( ) -> writer { ret wr; }
187
+ fn get_str ( ) -> str { ret buf. buf ; }
163
188
}
164
- ret fw ( new_buf_writer ( path , flags ) ) ;
189
+ ret str_writer_wrap ( str_writer_writer ( buf ) , buf ) ;
165
190
}
166
191
167
192
//
0 commit comments