writer.odin 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package csv
  2. import "core:io"
  3. import "core:strings"
  4. import "core:unicode/utf8"
  5. // Writer is a data structure used for writing records using a CSV-encoding.
  6. Writer :: struct {
  7. // Field delimiter (set to ',' with writer_init)
  8. comma: rune,
  9. // if set to true, \r\n will be used as the line terminator
  10. use_crlf: bool,
  11. w: io.Writer,
  12. }
  13. // writer_init initializes a Writer that writes to w
  14. writer_init :: proc(writer: ^Writer, w: io.Writer) {
  15. writer.comma = ','
  16. writer.w = w
  17. }
  18. // write writes a single CSV records to w with any of the necessarily quoting.
  19. // A record is a slice of strings, where each string is a single field.
  20. //
  21. // If the underlying io.Writer requires flushing, make sure to call io.flush
  22. write :: proc(w: ^Writer, record: []string) -> io.Error {
  23. CHAR_SET :: "\n\r\""
  24. field_needs_quoting :: proc(w: ^Writer, field: string) -> bool {
  25. switch {
  26. case field == "": // No need to quote empty strings
  27. return false
  28. case field == `\.`: // Postgres is weird
  29. return true
  30. case w.comma < utf8.RUNE_SELF: // ASCII optimization
  31. for i in 0..<len(field) {
  32. switch field[i] {
  33. case '\n', '\r', '"', byte(w.comma):
  34. return true
  35. }
  36. }
  37. case:
  38. if strings.contains_rune(field, w.comma) {
  39. return true
  40. }
  41. if strings.contains_any(field, CHAR_SET) {
  42. return true
  43. }
  44. }
  45. // Leading spaces need quoting
  46. r, _ := utf8.decode_rune_in_string(field)
  47. return strings.is_space(r)
  48. }
  49. if !is_valid_delim(w.comma) {
  50. return .No_Progress // TODO(bill): Is this a good error?
  51. }
  52. for _, field_idx in record {
  53. // NOTE(bill): declared like this so that the field can be modified later if necessary
  54. field := record[field_idx]
  55. if field_idx > 0 {
  56. io.write_rune(w.w, w.comma) or_return
  57. }
  58. if !field_needs_quoting(w, field) {
  59. io.write_string(w.w, field) or_return
  60. continue
  61. }
  62. io.write_byte(w.w, '"') or_return
  63. for len(field) > 0 {
  64. i := strings.index_any(field, CHAR_SET)
  65. if i < 0 {
  66. i = len(field)
  67. }
  68. io.write_string(w.w, field[:i]) or_return
  69. field = field[i:]
  70. if len(field) > 0 {
  71. switch field[0] {
  72. case '\r':
  73. if !w.use_crlf {
  74. io.write_byte(w.w, '\r') or_return
  75. }
  76. case '\n':
  77. if w.use_crlf {
  78. io.write_string(w.w, "\r\n") or_return
  79. } else {
  80. io.write_byte(w.w, '\n') or_return
  81. }
  82. case '"':
  83. io.write_string(w.w, `""`) or_return
  84. }
  85. field = field[1:]
  86. }
  87. }
  88. io.write_byte(w.w, '"') or_return
  89. }
  90. if w.use_crlf {
  91. _, err := io.write_string(w.w, "\r\n")
  92. return err
  93. }
  94. return io.write_byte(w.w, '\n')
  95. }
  96. // write_all writes multiple CSV records to w using write, and then flushes (if necessary).
  97. write_all :: proc(w: ^Writer, records: [][]string) -> io.Error {
  98. for record in records {
  99. write(w, record) or_return
  100. }
  101. return writer_flush(w)
  102. }
  103. // writer_flush flushes the underlying io.Writer.
  104. // If the underlying io.Writer does not support flush, nil is returned.
  105. writer_flush :: proc(w: ^Writer) -> io.Error {
  106. return io.flush(auto_cast w.w)
  107. }