writer.odin 3.0 KB

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