]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
Added function to load file to string
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 7 Oct 2017 00:31:53 +0000 (20:31 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 7 Oct 2017 00:31:53 +0000 (20:31 -0400)
Makefile
edit.ml
lib/misc.ml [new file with mode: 0644]
lib/misc_prims.c [new file with mode: 0644]

index eeb53a8f616d39c5093e56b02bc17ef6bf4e812b..68289b893d81d245b49c0306b1a756ccc39c522b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -28,9 +28,11 @@ LIBOBJS = \
     lib/tide.$(OBJEXT) \
     lib/x11.$(OBJEXT) \
     lib/cfg.$(OBJEXT) \
-    lib/x11_prims.o \
     lib/rope.$(OBJEXT) \
     lib/buf.$(OBJEXT) \
+    lib/misc.$(OBJEXT) \
+    lib/x11_prims.o \
+    lib/misc_prims.o \
     lib/utf8.o
 
 .PHONY: all clean
diff --git a/edit.ml b/edit.ml
index d2136aed423718fe782c289010afca2f00d57784..848d7e20614dd9739176e5e29e35b11f55ba3db8 100644 (file)
--- a/edit.ml
+++ b/edit.ml
@@ -8,13 +8,12 @@ let font = font_load "Monospace:size=10"
 type drawpos = { x: int; y: int }
 
 let draw_bkg color width height pos =
-  let clr = Cfg.Color.palette.(color) in
-  draw_rect { x = pos.x; y = pos.y; w = width; h = height; c = clr }
+  draw_rect { x = pos.x; y = pos.y; w = width; h = height; c = color }
 
 (* curried helpers *)
-let draw_dark_bkg = draw_bkg 0
-let draw_light_bkg = draw_bkg 1
-let draw_gray_bkg = draw_bkg 3
+let draw_dark_bkg  = draw_bkg Cfg.Color.palette.(0)
+let draw_light_bkg = draw_bkg Cfg.Color.palette.(1)
+let draw_gray_bkg  = draw_bkg Cfg.Color.palette.(3)
 
 let draw_text text pos =
   draw_string font Cfg.Color.palette.(5) text (pos.x + 2, pos.y + 2);
@@ -69,7 +68,7 @@ let onupdate width height =
   let pos = draw_status pos width "UNSI> *scratch*" in
   let pos = draw_tags pos width (height / font.height / 4) "Sample tags data" in
   let pos = draw_scroll pos height in
-  let _ = draw_edit pos width height in
+  let _   = draw_edit pos width height in
   flip ()
 
 let onshutdown () =
diff --git a/lib/misc.ml b/lib/misc.ml
new file mode 100644 (file)
index 0000000..18d6320
--- /dev/null
@@ -0,0 +1,2 @@
+external load_file : string -> string
+                   = "load_file"
diff --git a/lib/misc_prims.c b/lib/misc_prims.c
new file mode 100644 (file)
index 0000000..2bc6268
--- /dev/null
@@ -0,0 +1,34 @@
+#include "internals.h"
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+CAMLprim value load_file(value path) {
+    CAMLparam1(path);
+    CAMLlocal1(str);
+    int fd, nread;
+    struct stat sb;
+    if (((fd = open(String_val(path), O_RDONLY, 0)) < 0) ||
+        (fstat(fd, &sb) < 0) ||
+        (sb.st_size == 0)) {
+        caml_failwith("could not open file");
+    } else {
+        str = caml_alloc_string(sb.st_size);
+        while ((nread = read(fd, String_val(str), sb.st_size)) > 0);
+        if (nread < 0)
+            caml_failwith("read() failed");
+    }
+    close(fd);
+    CAMLreturn(str);
+}
+
+CAMLprim value env_set(value name, value val) {
+    CAMLparam2(name,val);
+    CAMLreturn(Val_unit);
+}
+
+CAMLprim value env_get(value name) {
+    CAMLparam1(name);
+    CAMLreturn(Val_unit);
+}