Skip to content

Commit

Permalink
Move some stuff from planck.io to planck.core
Browse files Browse the repository at this point in the history
Fixes #75


Former-commit-id: 5451819
  • Loading branch information
mfikes committed Aug 13, 2015
1 parent 5d4a7ae commit 923c8cb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 99 deletions.
6 changes: 6 additions & 0 deletions int-test/expected/PLANCK-ERR.txt
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
hello stderr
WARNING: planck.io/spit is deprecated. at line 1
WARNING: planck.io/spit is deprecated. at line 1
WARNING: planck.io/spit is deprecated. at line 1
WARNING: planck.io/spit is deprecated. at line 1
WARNING: planck.io/spit is deprecated. at line 1
WARNING: planck.io/spit is deprecated. at line 1
9 changes: 3 additions & 6 deletions int-test/expected/PLANCK-OUT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ nil
"abc"
reader and read-line
nil
nil
#'planck.io/test-read
#'cljs.user/test-read
nil
nil
nil
Expand All @@ -179,8 +178,7 @@ nil
nil
writer
nil
nil
#'planck.io/test-write
#'cljs.user/test-write
true
true
true
Expand All @@ -189,8 +187,7 @@ true
true
writer append
nil
nil
#'planck.io/test-write
#'cljs.user/test-write
true
true
true
Expand Down
13 changes: 5 additions & 8 deletions int-test/script/gen-actual
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ $PLANCK <<REPL_INPUT
(def test-file "/tmp/PLANCK_TEST.txt")
(require 'planck.io)
(defn test-spit-slurp [content]
(planck.io/spit test-file content)
(planck.core/spit test-file content)
(prn
(= content
(planck.io/slurp test-file))))
(planck.core/slurp test-file))))
(test-spit-slurp "")
(test-spit-slurp "a")
(test-spit-slurp "a\n")
Expand All @@ -167,8 +167,7 @@ REPL_INPUT

echo "reader and read-line"
$PLANCK <<REPL_INPUT
(require 'planck.io)
(in-ns 'planck.io)
(require '[planck.core :refer [*in* -close spit read-line]] '[planck.io :refer [reader]])
(defn test-read [content]
(spit "/tmp/PLANCK_TEST.txt" content)
(let [rdr (reader "/tmp/PLANCK_TEST.txt")]
Expand All @@ -189,8 +188,7 @@ REPL_INPUT

echo "writer"
$PLANCK <<REPL_INPUT
(require 'planck.io)
(in-ns 'planck.io)
(require '[planck.core :refer [-close slurp spit]] '[planck.io :refer [reader writer]])
(defn test-write [content]
(spit "/tmp/PLANCK_TEST.txt" content)
(let [wtr (writer "/tmp/PLANCK_TEST.txt")]
Expand All @@ -209,8 +207,7 @@ REPL_INPUT

echo "writer append"
$PLANCK <<REPL_INPUT
(require 'planck.io)
(in-ns 'planck.io)
(require '[planck.core :refer [-close slurp spit]] '[planck.io :refer [reader writer]])
(defn test-write [previous-content content]
(spit "/tmp/PLANCK_TEST.txt" previous-content)
(let [wtr (writer "/tmp/PLANCK_TEST.txt" :append true)]
Expand Down
83 changes: 83 additions & 0 deletions planck-cljs/src/planck/core.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,89 @@
(ns planck.core)

(defprotocol IClosable
(-close [this]))

(defprotocol IReader
(-read [this] "Returns available characters as a string or nil of EOF."))

(defrecord Reader [raw-read raw-close]
IReader
(-read [_]
(raw-read))
IClosable
(-close [_]
(raw-close)))

(defrecord Writer [raw-write raw-flush raw-close]
IWriter
(-write [_ s]
(raw-write s))
(-flush [_]
(raw-flush))
IClosable
(-close [_]
(raw-close)))

(defonce
^{:doc "A planck.io/IReader representing standard input for read operations."
:dynamic true}
*in*
(Reader. js/PLANCK_RAW_READ_STDIN nil))

(set! cljs.core/*out* (Writer. js/PLANCK_RAW_WRITE_STDOUT js/PLANCK_RAW_FLUSH_STDOUT nil))

(defonce
^{:doc "A cljs.core/IWriter representing standard error for print operations."
:dynamic true}
*err*
(Writer. js/PLANCK_RAW_WRITE_STDERR js/PLANCK_RAW_FLUSH_STDERR nil))

(defn file-seq
"A tree seq on PLKFiles"
[dir]
(js/PLANCK_IO_FILESEQ dir))

(defn- fission!
"Breaks an atom's value into two parts. The supplied function should
return a pair. The first element will be set to be the atom's new
value and the second element will be returned."
[atom f & args]
(loop []
(let [old @atom
[new-in new-out] (apply f old args)]
(if (compare-and-set! atom old new-in)
new-out
(recur)))))

(defonce ^:private buffer (atom nil))

(defn read-line
"Reads the next line from the current value of planck.io/*in*"
[]
(if-let [buffered @buffer]
(let [n (.indexOf buffered "\n")]
(if (neg? n)
(if-let [next-characters (-read *in*)]
(do
(swap! buffer (fn [s] (str s next-characters)))
(recur))
(fission! buffer (fn [s] [nil s])))
(fission! buffer (fn [s] [(let [residual (subs s (inc n))]
(if (= "" residual)
nil
residual))
(subs s 0 n)]))))
(when (reset! buffer (-read *in*))
(recur))))

(defn slurp
"Slurps a file"
[filename]
(or (js/PLANCK_READ_FILE filename)
(throw (js/Error. filename))))

(defn spit
"Spits a file"
[filename content]
(js/PLANCK_WRITE_FILE filename content)
nil)
93 changes: 8 additions & 85 deletions planck-cljs/src/planck/io.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns planck.io
(:require planck.core)
#_(:import goog.Uri))

(defrecord File [path])
Expand Down Expand Up @@ -38,44 +39,6 @@
(as-file (.getPath u))
(throw (js/Error. (str "Not a file: " u))))))

(defprotocol IClosable
(-close [this]))

(defprotocol IReader
(-read [this] "Returns available characters as a string or nil of EOF."))

(defrecord Reader [raw-read raw-close]
IReader
(-read [_]
(raw-read))
IClosable
(-close [_]
(raw-close)))

(defrecord Writer [raw-write raw-flush raw-close]
IWriter
(-write [_ s]
(raw-write s))
(-flush [_]
(raw-flush))
IClosable
(-close [_]
(raw-close)))

(defonce
^{:doc "A planck.io/IReader representing standard input for read operations."
:dynamic true}
*in*
(Reader. js/PLANCK_RAW_READ_STDIN nil))

(set! cljs.core/*out* (Writer. js/PLANCK_RAW_WRITE_STDOUT js/PLANCK_RAW_FLUSH_STDOUT nil))

(defonce
^{:doc "A cljs.core/IWriter representing standard error for print operations."
:dynamic true}
*err*
(Writer. js/PLANCK_RAW_WRITE_STDERR js/PLANCK_RAW_FLUSH_STDERR nil))

(defprotocol IOFactory
"Factory functions that create ready-to-use versions of
the various stream types, on top of anything that can
Expand Down Expand Up @@ -108,13 +71,13 @@
(make-reader [file opts]
(let [file-reader (js/PLKFileReader.open (:path file))]
(check-utf-8-encoding (:encoding opts))
(Reader.
(planck.core/Reader.
(fn [] (.read file-reader))
(fn [] (.close file-reader)))))
(make-writer [file opts]
(let [file-writer (js/PLKFileWriter.openAppend (:path file) (:append opts))]
(check-utf-8-encoding (:encoding opts))
(Writer.
(planck.core/Writer.
(fn [s] (.write file-writer s))
(fn [] (.flush file-writer))
(fn [] (.close file-writer))))))
Expand All @@ -129,51 +92,6 @@
[x & opts]
(make-writer x (when opts (apply hash-map opts))))

(defn- fission!
"Breaks an atom's value into two parts. The supplied function should
return a pair. The first element will be set to be the atom's new
value and the second element will be returned."
[atom f & args]
(loop []
(let [old @atom
[new-in new-out] (apply f old args)]
(if (compare-and-set! atom old new-in)
new-out
(recur)))))

(defonce ^:private buffer (atom nil))

(defn read-line
"Reads the next line from the current value of planck.io/*in*"
[]
(if-let [buffered @buffer]
(let [n (.indexOf buffered "\n")]
(if (neg? n)
(if-let [next-characters (-read *in*)]
(do
(swap! buffer (fn [s] (str s next-characters)))
(recur))
(fission! buffer (fn [s] [nil s])))
(fission! buffer (fn [s] [(let [residual (subs s (inc n))]
(if (= "" residual)
nil
residual))
(subs s 0 n)]))))
(when (reset! buffer (-read *in*))
(recur))))

(defn slurp
"Slurps a file"
[filename]
(or (js/PLANCK_READ_FILE filename)
(throw (js/Error. filename))))

(defn spit
"Spits a file"
[filename content]
(js/PLANCK_WRITE_FILE filename content)
nil)

(defn file
"Returns a PLKFile, passing each arg to as-file. Multiple-arg
versions treat the first argument as parent and subsequent args as
Expand All @@ -188,3 +106,8 @@
[f]
(.deleteFile f))

;; These have been moved
(def ^:deprecated read-line planck.core/read-line)
(def ^:deprecated slurp planck.core/slurp)
(def ^:deprecated spit planck.core/spit)

0 comments on commit 923c8cb

Please sign in to comment.