lib: intro `Writer::write_byte` and implement it in `FileWriter`
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 19 May 2015 02:28:19 +0000 (22:28 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 19 May 2015 16:24:07 +0000 (12:24 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/standard/file.nit
lib/standard/stream.nit

index 1b81877..5324f01 100644 (file)
@@ -193,6 +193,26 @@ class FileWriter
                end
        end
 
+       redef fun write_byte(value)
+       do
+               if last_error != null then return
+               if not _is_writable then
+                       last_error = new IOError("Cannot write to non-writable stream")
+                       return
+               end
+               if _file.address_is_null then
+                       last_error = new IOError("Writing on a null stream")
+                       _is_writable = false
+                       return
+               end
+
+               var err = _file.write_byte(value)
+               if err != 1 then
+                       # Big problem
+                       last_error = new IOError("Problem writing a byte: {err}")
+               end
+       end
+
        redef fun close
        do
                super
@@ -1090,6 +1110,10 @@ end
 private extern class NativeFile `{ FILE* `}
        fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
        fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
+       fun write_byte(value: Int): Int `{
+               unsigned char b = (unsigned char)value;
+               return fwrite(&b, 1, 1, recv);
+       `}
        fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
        fun file_stat: NativeFileStat is extern "file_NativeFile_NativeFile_file_stat_0"
        fun fileno: Int `{ return fileno(recv); `}
index 6b6d3fe..7c4e606 100644 (file)
@@ -345,6 +345,9 @@ abstract class Writer
        # write a string
        fun write(s: Text) is abstract
 
+       # Write a single byte
+       fun write_byte(value: Int) is abstract
+
        # Can the stream be used to write
        fun is_writable: Bool is abstract
 end