From a2c29fe44720003df06f7f14d0bf02332298b87f Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 16 Oct 2017 18:30:18 -0400 Subject: - Re-named lux/world/fs to lux/world/file. --- stdlib/source/lux/world/file.lux | 120 +++++++++++++++++++++++++++++++++++++ stdlib/source/lux/world/fs.jvm.lux | 120 ------------------------------------- 2 files changed, 120 insertions(+), 120 deletions(-) create mode 100644 stdlib/source/lux/world/file.lux delete mode 100644 stdlib/source/lux/world/fs.jvm.lux (limited to 'stdlib/source') diff --git a/stdlib/source/lux/world/file.lux b/stdlib/source/lux/world/file.lux new file mode 100644 index 000000000..4c0881e04 --- /dev/null +++ b/stdlib/source/lux/world/file.lux @@ -0,0 +1,120 @@ +(;module: + lux + (lux (control [monad #+ do] + ["ex" exception #+ exception:]) + (concurrency ["P" promise] + ["T" task]) + (data ["R" result] + (coll [array])) + (time ["i" instant] + ["d" duration]) + (world [blob #+ Blob]) + [io] + [host])) + +(exception: Could-Not-Read-All-Data) + +(type: #export File Text) + +(host;import #long java.io.File + (new [String]) + (exists [] #io #try boolean) + (mkdir [] #io #try boolean) + (delete [] #io #try boolean) + (length [] #io #try long) + (listFiles [] #io #try (Array java.io.File)) + (getAbsolutePath [] #io #try String) + (renameTo [java.io.File] #io #try boolean) + (isFile [] #io #try boolean) + (isDirectory [] #io #try boolean) + (lastModified [] #io #try long) + (setLastModified [long] #io #try boolean) + (canRead [] #io #try boolean) + (canWrite [] #io #try boolean) + (canExecute [] #io #try boolean)) + +(host;import java.lang.AutoCloseable + (close [] #io #try void)) + +(host;import java.io.OutputStream + (write [(Array byte)] #io #try void) + (flush [] #io #try void)) + +(host;import java.io.FileOutputStream + (new [java.io.File boolean] #io #try)) + +(host;import java.io.InputStream + (read [(Array byte)] #io #try int)) + +(host;import java.io.FileInputStream + (new [java.io.File] #io #try)) + +(do-template [ ] + [(def: #export ( data file) + (-> Blob File (T;Task Unit)) + (P;future (do (R;ResultT io;Monad) + [stream (FileOutputStream.new [(java.io.File.new file) ]) + _ (OutputStream.write [data] stream) + _ (OutputStream.flush [] stream)] + (AutoCloseable.close [] stream))))] + + [append true] + [write false] + ) + +(def: #export (read file) + (-> File (T;Task Blob)) + (P;future (do (R;ResultT io;Monad) + [#let [file' (java.io.File.new file)] + size (java.io.File.length [] file') + #let [data (blob;create (int-to-nat size))] + stream (FileInputStream.new [file']) + bytes-read (InputStream.read [data] stream) + _ (AutoCloseable.close [] stream)] + (if (i.= size bytes-read) + (wrap data) + (io;io (ex;throw Could-Not-Read-All-Data file)))))) + +(def: #export (size file) + (-> File (T;Task Nat)) + (P;future (do (R;ResultT io;Monad) + [size (java.io.File.length [] (java.io.File.new file))] + (wrap (int-to-nat size))))) + +(def: #export (files dir) + (-> File (T;Task (List File))) + (P;future (do (R;ResultT io;Monad) + [files (java.io.File.listFiles [] (java.io.File.new dir))] + (monad;map @ (java.io.File.getAbsolutePath []) + (array;to-list files))))) + +(do-template [ ] + [(def: #export ( file) + (-> File (T;Task Bool)) + (P;future ( [] (java.io.File.new file))))] + + [exists? java.io.File.exists] + [make-dir java.io.File.mkdir] + [delete java.io.File.delete] + [file? java.io.File.isFile] + [directory? java.io.File.isDirectory] + [can-read? java.io.File.canRead] + [can-write? java.io.File.canWrite] + [can-execute? java.io.File.canExecute] + ) + +(def: #export (move target source) + (-> File File (T;Task Bool)) + (P;future (java.io.File.renameTo [(java.io.File.new target)] + (java.io.File.new source)))) + +(def: #export (get-last-modified file) + (-> File (T;Task i;Instant)) + (P;future (do (R;ResultT io;Monad) + [millis (java.io.File.lastModified [] (java.io.File.new file))] + (wrap (|> millis d;from-millis i;absolute))))) + +(def: #export (set-last-modified time file) + (-> i;Instant File (T;Task Bool)) + (P;future (java.io.File.setLastModified [(|> time i;relative d;to-millis)] + (java.io.File.new file)))) diff --git a/stdlib/source/lux/world/fs.jvm.lux b/stdlib/source/lux/world/fs.jvm.lux deleted file mode 100644 index 4c0881e04..000000000 --- a/stdlib/source/lux/world/fs.jvm.lux +++ /dev/null @@ -1,120 +0,0 @@ -(;module: - lux - (lux (control [monad #+ do] - ["ex" exception #+ exception:]) - (concurrency ["P" promise] - ["T" task]) - (data ["R" result] - (coll [array])) - (time ["i" instant] - ["d" duration]) - (world [blob #+ Blob]) - [io] - [host])) - -(exception: Could-Not-Read-All-Data) - -(type: #export File Text) - -(host;import #long java.io.File - (new [String]) - (exists [] #io #try boolean) - (mkdir [] #io #try boolean) - (delete [] #io #try boolean) - (length [] #io #try long) - (listFiles [] #io #try (Array java.io.File)) - (getAbsolutePath [] #io #try String) - (renameTo [java.io.File] #io #try boolean) - (isFile [] #io #try boolean) - (isDirectory [] #io #try boolean) - (lastModified [] #io #try long) - (setLastModified [long] #io #try boolean) - (canRead [] #io #try boolean) - (canWrite [] #io #try boolean) - (canExecute [] #io #try boolean)) - -(host;import java.lang.AutoCloseable - (close [] #io #try void)) - -(host;import java.io.OutputStream - (write [(Array byte)] #io #try void) - (flush [] #io #try void)) - -(host;import java.io.FileOutputStream - (new [java.io.File boolean] #io #try)) - -(host;import java.io.InputStream - (read [(Array byte)] #io #try int)) - -(host;import java.io.FileInputStream - (new [java.io.File] #io #try)) - -(do-template [ ] - [(def: #export ( data file) - (-> Blob File (T;Task Unit)) - (P;future (do (R;ResultT io;Monad) - [stream (FileOutputStream.new [(java.io.File.new file) ]) - _ (OutputStream.write [data] stream) - _ (OutputStream.flush [] stream)] - (AutoCloseable.close [] stream))))] - - [append true] - [write false] - ) - -(def: #export (read file) - (-> File (T;Task Blob)) - (P;future (do (R;ResultT io;Monad) - [#let [file' (java.io.File.new file)] - size (java.io.File.length [] file') - #let [data (blob;create (int-to-nat size))] - stream (FileInputStream.new [file']) - bytes-read (InputStream.read [data] stream) - _ (AutoCloseable.close [] stream)] - (if (i.= size bytes-read) - (wrap data) - (io;io (ex;throw Could-Not-Read-All-Data file)))))) - -(def: #export (size file) - (-> File (T;Task Nat)) - (P;future (do (R;ResultT io;Monad) - [size (java.io.File.length [] (java.io.File.new file))] - (wrap (int-to-nat size))))) - -(def: #export (files dir) - (-> File (T;Task (List File))) - (P;future (do (R;ResultT io;Monad) - [files (java.io.File.listFiles [] (java.io.File.new dir))] - (monad;map @ (java.io.File.getAbsolutePath []) - (array;to-list files))))) - -(do-template [ ] - [(def: #export ( file) - (-> File (T;Task Bool)) - (P;future ( [] (java.io.File.new file))))] - - [exists? java.io.File.exists] - [make-dir java.io.File.mkdir] - [delete java.io.File.delete] - [file? java.io.File.isFile] - [directory? java.io.File.isDirectory] - [can-read? java.io.File.canRead] - [can-write? java.io.File.canWrite] - [can-execute? java.io.File.canExecute] - ) - -(def: #export (move target source) - (-> File File (T;Task Bool)) - (P;future (java.io.File.renameTo [(java.io.File.new target)] - (java.io.File.new source)))) - -(def: #export (get-last-modified file) - (-> File (T;Task i;Instant)) - (P;future (do (R;ResultT io;Monad) - [millis (java.io.File.lastModified [] (java.io.File.new file))] - (wrap (|> millis d;from-millis i;absolute))))) - -(def: #export (set-last-modified time file) - (-> i;Instant File (T;Task Bool)) - (P;future (java.io.File.setLastModified [(|> time i;relative d;to-millis)] - (java.io.File.new file)))) -- cgit v1.2.3