PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/text.arc

http://github.com/alimoeeny/arc
Unknown | 38 lines | 33 code | 5 blank | 0 comment | 0 complexity | d30d7396768e84deb9d056c99a7cc4e3 MD5 | raw file
 1; drainf and pump taken from cchooper's post on http://arclanguage.org/item?id=4220
 2
 3(mac drainf (expr f (o eof nil))
 4  (w/uniq (gdone gres)
 5    `(let ,gdone nil
 6       (while (no ,gdone)
 7	      (let ,gres ,expr
 8		(if (is ,gres ,eof)
 9		    (= ,gdone t)
10		    (,f ,gres)))))))
11
12(def pump (in out (o binary nil))
13  (with (err-flag t
14		  inport  (if (isa in  'string) (instring  in)  in)
15		  outport (if (isa out 'string) (outstring out) out))
16    (after
17      (do
18	(if binary
19	    (drainf (readb inport) [writeb _ outport])
20	    (drainf (readc inport) [writec _ outport]))
21	(= err-flag nil)
22	(if (isa out 'string)
23	    (inside outport)
24	    out))
25      (if (isa in  'string) (close inport))
26      (if (isa out 'string) (close outport)))))
27
28(def read-text-file (path)
29  (w/infile i path (pump i "")))
30
31(def read-text-list (path)
32  (tokens (read-text-file path) #\newline))
33
34(def write-text-list (lst path)
35  (w/outfile outf path
36	     (each elem (butlast lst)
37	       (disp (+ elem "\n") outf))
38	     (if lst (disp (last lst) outf))))