1#include "csv.h"
2
3#include <base/system.h>
4
5void CsvWrite(IOHANDLE File, int NumColumns, const char *const *ppColumns)
6{
7 for(int i = 0; i < NumColumns; i++)
8 {
9 if(i != 0)
10 {
11 io_write(io: File, buffer: ",", size: 1);
12 }
13 const char *pColumn = ppColumns[i];
14 int ColumnLength = str_length(str: pColumn);
15 if(!str_find(haystack: pColumn, needle: "\"") && !str_find(haystack: pColumn, needle: ","))
16 {
17 io_write(io: File, buffer: pColumn, size: ColumnLength);
18 continue;
19 }
20
21 int Start = 0;
22 io_write(io: File, buffer: "\"", size: 1);
23 for(int j = 0; j < ColumnLength; j++)
24 {
25 if(pColumn[j] == '"')
26 {
27 if(Start != j)
28 {
29 io_write(io: File, buffer: pColumn + Start, size: j - Start);
30 }
31 Start = j + 1;
32 io_write(io: File, buffer: "\"\"", size: 2);
33 }
34 }
35 if(Start != ColumnLength)
36 {
37 io_write(io: File, buffer: pColumn + Start, size: ColumnLength - Start);
38 }
39 io_write(io: File, buffer: "\"", size: 1);
40 }
41 io_write_newline(io: File);
42}
43