]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/io.cpp
io.streams.256color: faster by caching styles
[factor.git] / vm / io.cpp
index bdb50ac3fbe39b73ff3c9517eca40b52d4e9b5a0..e299e9888607e392d5da89f5dd55a3c3134c48a3 100644 (file)
--- a/vm/io.cpp
+++ b/vm/io.cpp
@@ -2,27 +2,28 @@
 
 namespace factor {
 
-/* Simple wrappers for ANSI C I/O functions, used for bootstrapping.
+// Simple wrappers for ANSI C I/O functions, used for bootstrapping.
 
-Note the ugly loop logic in almost every function; we have to handle EINTR
-and restart the operation if the system call was interrupted. Naive
-applications don't do this, but then they quickly fail if one enables
-itimer()s or other signals.
+// Note the ugly loop logic in almost every function; we have to handle EINTR
+// and restart the operation if the system call was interrupted. Naive
+// applications don't do this, but then they quickly fail if one enables
+// itimer()s or other signals.
 
-The Factor library provides platform-specific code for Unix and Windows
-with many more capabilities so these words are not usually used in
-normal operation. */
+// The Factor library provides platform-specific code for Unix and Windows
+// with many more capabilities so these words are not usually used in
+// normal operation.
 
 size_t raw_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
+  FACTOR_ASSERT(nitems > 0);
   size_t items_read = 0;
-  size_t ret = 0;
 
   do {
-    ret = fread((void*)((int*)ptr + items_read * size), size,
-                nitems - items_read, stream);
+    size_t ret = fread((void*)((int*)ptr + items_read * size), size,
+                       nitems - items_read, stream);
     if (ret == 0) {
-      if (feof(stream))
+      if (feof(stream)) {
         break;
+      }
       else if (errno != EINTR) {
         return 0;
       }
@@ -40,14 +41,7 @@ int raw_fclose(FILE* stream) {
   return 0;
 }
 
-
-void factor_vm::init_c_io() {
-  special_objects[OBJ_STDIN] = allot_alien(false_object, (cell)stdin);
-  special_objects[OBJ_STDOUT] = allot_alien(false_object, (cell)stdout);
-  special_objects[OBJ_STDERR] = allot_alien(false_object, (cell)stderr);
-}
-
-/* Allocates memory */
+// Allocates memory
 void factor_vm::io_error_if_not_EINTR() {
   if (errno == EINTR)
     return;
@@ -55,7 +49,7 @@ void factor_vm::io_error_if_not_EINTR() {
   general_error(ERROR_IO, tag_fixnum(errno), false_object);
 }
 
-FILE* factor_vm::safe_fopen(char* filename, char* mode) {
+FILE* factor_vm::safe_fopen(char* filename, const char* mode) {
   FILE* file;
   for (;;) {
     file = fopen(filename, mode);
@@ -85,7 +79,7 @@ int factor_vm::safe_fgetc(FILE* stream) {
 size_t factor_vm::safe_fread(void* ptr, size_t size, size_t nitems,
                              FILE* stream) {
   size_t ret = raw_fread(ptr, size, nitems, stream);
-  if (!ret)
+  if (ret == 0 && !feof(stream))
     io_error_if_not_EINTR();
   return ret;
 }
@@ -115,7 +109,7 @@ size_t factor_vm::safe_fwrite(void* ptr, size_t size, size_t nitems,
   return items_written;
 }
 
-int factor_vm::safe_ftell(FILE* stream) {
+off_t factor_vm::safe_ftell(FILE* stream) {
   off_t offset;
   for (;;) {
     if ((offset = FTELL(stream)) == -1)
@@ -138,7 +132,7 @@ void factor_vm::safe_fseek(FILE* stream, off_t offset, int whence) {
       whence = SEEK_END;
       break;
     default:
-      critical_error("Bad value for whence", whence);
+      general_error(ERROR_IO, tag_fixnum(EINVAL), false_object);
   }
 
   for (;;) {
@@ -158,25 +152,21 @@ void factor_vm::safe_fflush(FILE* stream) {
   }
 }
 
-void factor_vm::safe_fclose(FILE* stream) {
-  if (raw_fclose(stream) == -1)
-    io_error_if_not_EINTR();
-}
-
 void factor_vm::primitive_fopen() {
-  data_root<byte_array> mode(ctx->pop(), this);
-  data_root<byte_array> path(ctx->pop(), this);
-  mode.untag_check(this);
-  path.untag_check(this);
+  byte_array *mode = untag_check<byte_array>(ctx->pop());
+  byte_array *path = untag_check<byte_array>(ctx->pop());
 
-  FILE* file;
-  file = safe_fopen((char*)(path.untagged() + 1), (char*)(mode.untagged() + 1));
-  ctx->push(allot_alien(file));
+  FILE* file = safe_fopen((char*)(path + 1), (char*)(mode + 1));
+  ctx->push(allot_alien((cell)file));
 }
 
-FILE* factor_vm::pop_file_handle() { return (FILE*)alien_offset(ctx->pop()); }
+FILE* factor_vm::pop_file_handle() {
+  return (FILE*)alien_offset(ctx->pop());
+}
 
-FILE* factor_vm::peek_file_handle() { return (FILE*)alien_offset(ctx->peek()); }
+FILE* factor_vm::peek_file_handle() {
+  return (FILE*)alien_offset(ctx->peek());
+}
 
 void factor_vm::primitive_fgetc() {
   FILE* file = peek_file_handle();
@@ -189,7 +179,7 @@ void factor_vm::primitive_fgetc() {
     ctx->replace(tag_fixnum(c));
 }
 
-/* Allocates memory (from_unsigned_cell())*/
+// Allocates memory (from_unsigned_cell())
 void factor_vm::primitive_fread() {
   FILE* file = pop_file_handle();
   void* buf = (void*)alien_offset(ctx->pop());
@@ -199,7 +189,6 @@ void factor_vm::primitive_fread() {
     ctx->push(from_unsigned_cell(0));
     return;
   }
-
   size_t c = safe_fread(buf, 1, size, file);
   if (c == 0 || feof(file))
     clearerr(file);
@@ -244,12 +233,13 @@ void factor_vm::primitive_fflush() {
 
 void factor_vm::primitive_fclose() {
   FILE* file = pop_file_handle();
-  safe_fclose(file);
+  if (raw_fclose(file) == -1)
+    io_error_if_not_EINTR();
 }
 
-/* This function is used by FFI I/O. Accessing the errno global directly is
-not portable, since on some libc's errno is not a global but a funky macro that
-reads thread-local storage. */
+// This function is used by FFI I/O. Accessing the errno global directly is
+// not portable, since on some libc's errno is not a global but a funky macro that
+// reads thread-local storage.
 VM_C_API int err_no() { return errno; }
 
 VM_C_API void set_err_no(int err) { errno = err; }