example.scm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. (use-modules (dsv))
  2. (use-modules ((rnrs)
  3. :version (6)
  4. #:prefix rnrs:))
  5. ;; The important procedures and other items are (copied from the docs of guile-dsv):
  6. ;; dsv->scm [port [delimiter]] [#:format='unix] [#:comment-prefix='default]
  7. ;; dsv-string->scm string [delimiter] [#:format='unix] [#:comment-symbol='default]
  8. ;; scm->dsv list [port [delimiter]] [#:format='unix]
  9. ;; scm->dsv-string list [delimiter] [#:format='unix]
  10. ;; guess-delimiter string [known-delimiters]
  11. ;; set-debug! enabled?
  12. ;; dsv-parser-error
  13. ;; =======
  14. ;; HELPERS
  15. ;; =======
  16. (define displayln
  17. (lambda (sth)
  18. (display (simple-format #f "~a\n" sth))))
  19. (define (map* proc lst)
  20. (cond [(null? lst) '()]
  21. [(pair? (car lst))
  22. (cons (map* proc (car lst))
  23. (map* proc (cdr lst)))]
  24. [else
  25. (cons (proc (car lst))
  26. (map* proc (cdr lst)))]))
  27. (define (stringify* lst)
  28. (map* (lambda (val)
  29. (cond
  30. [(number? val) (number->string val)]
  31. [(string? val) val]
  32. [else (simple-format #f "~s" val)]))
  33. lst))
  34. ;; =============================
  35. ;; LIBRARY INTERFACE ABSTRACTION
  36. ;; =============================
  37. (define* (read-dsv-from-file
  38. file-path
  39. #:optional (delimiter #\,)
  40. #:key
  41. (format 'unix)
  42. (comment-prefix 'default)
  43. (encoding "UTF-8"))
  44. (call-with-input-file file-path
  45. (lambda (port)
  46. (set-port-encoding! port encoding)
  47. (dsv->scm port
  48. delimiter
  49. #:format format
  50. #:comment-prefix comment-prefix))))
  51. (define* (read-dsv-from-string input
  52. #:optional (delimiter #\,)
  53. #:key
  54. (format 'unix)
  55. (comment-prefix 'default))
  56. (dsv-string->scm input
  57. delimiter
  58. #:format format
  59. #:comment-prefix comment-prefix))
  60. (define* (write-scm-dsv-to-file scm-output file-path
  61. #:optional (delimiter #\,)
  62. #:key
  63. (format 'unix)
  64. (comment-prefix 'default)
  65. (encoding "UTF-8"))
  66. ;; For some unknown reason scm->dsv expects everything inside the
  67. ;; list to be strings already. This means we need to convert to
  68. ;; strings before giving the data to scm->dsv.
  69. ;; WARNING: There is no representation for symbols in DSV files, so
  70. ;; symbols and strings are not distinguished within such a
  71. ;; file. That means the conversion is lossy for some types of data.
  72. (call-with-output-file file-path
  73. (lambda (port)
  74. (rnrs:assert (list? scm-output))
  75. (set-port-encoding! port encoding)
  76. (scm->dsv (stringify* scm-output)
  77. port
  78. delimiter
  79. #:format format))))
  80. (define* (write-scm-dsv-to-string scm-output
  81. #:optional (delimiter #\,)
  82. #:key
  83. (format 'unix)
  84. (comment-prefix 'default))
  85. (scm->dsv-string (stringify* scm-output)
  86. delimiter
  87. #:format format))
  88. ;; =============
  89. ;; USAGE EXAMPLE
  90. ;; =============
  91. (define (main)
  92. (displayln "read from file")
  93. (displayln
  94. (read-dsv-from-file "example-in.csv"))
  95. (displayln "read from string")
  96. (displayln
  97. (read-dsv-from-string
  98. (string-join '("1,2,3,4" "5,6,7,8" "9,10,\"a\",\"b\"")
  99. "\n")))
  100. (displayln "write to file")
  101. (displayln
  102. (write-scm-dsv-to-file
  103. '((1 2 3 4) (5 6 7 8) (9 10 "a" "b" c))
  104. "example-out.csv"))
  105. (displayln "write to string")
  106. (displayln
  107. (write-scm-dsv-to-string '((1 2 3 4) (5 6 7 8) (9 10 "a" "b")))))
  108. (main)