/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. (mac drainf (expr f (o eof nil))
  3. (w/uniq (gdone gres)
  4. `(let ,gdone nil
  5. (while (no ,gdone)
  6. (let ,gres ,expr
  7. (if (is ,gres ,eof)
  8. (= ,gdone t)
  9. (,f ,gres)))))))
  10. (def pump (in out (o binary nil))
  11. (with (err-flag t
  12. inport (if (isa in 'string) (instring in) in)
  13. outport (if (isa out 'string) (outstring out) out))
  14. (after
  15. (do
  16. (if binary
  17. (drainf (readb inport) [writeb _ outport])
  18. (drainf (readc inport) [writec _ outport]))
  19. (= err-flag nil)
  20. (if (isa out 'string)
  21. (inside outport)
  22. out))
  23. (if (isa in 'string) (close inport))
  24. (if (isa out 'string) (close outport)))))
  25. (def read-text-file (path)
  26. (w/infile i path (pump i "")))
  27. (def read-text-list (path)
  28. (tokens (read-text-file path) #\newline))
  29. (def write-text-list (lst path)
  30. (w/outfile outf path
  31. (each elem (butlast lst)
  32. (disp (+ elem "\n") outf))
  33. (if lst (disp (last lst) outf))))