1 /*
2 Copyright 2021 Northern.tech AS
3
4 This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 3.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
19 To the extent this program is licensed as part of the Enterprise
20 versions of CFEngine, the applicable Commercial Open Source License
21 (COSL) may apply to this file if you as a licensee so wish it. See
22 included file COSL.txt.
23 */
24
25 #include <csv_writer.h>
26
27 #include <alloc.h>
28
29 struct CsvWriter_
30 {
31 Writer *w;
32 bool beginning_of_line;
33 bool terminate_last_line;
34 };
35
36 /*****************************************************************************/
37
38 static void WriteCsvEscapedString(Writer *w, const char *str);
39 static void CsvWriterFieldVF(CsvWriter * csvw, const char *fmt, va_list ap) FUNC_ATTR_PRINTF(2, 0);
40
41 /*****************************************************************************/
42
CsvWriterOpenSpecifyTerminate(Writer * w,bool terminate_last_line)43 CsvWriter *CsvWriterOpenSpecifyTerminate(Writer *w, bool terminate_last_line)
44 {
45 CsvWriter *csvw = xmalloc(sizeof(CsvWriter));
46
47 csvw->w = w;
48 csvw->beginning_of_line = true;
49 csvw->terminate_last_line = terminate_last_line;
50 return csvw;
51 }
52
CsvWriterOpen(Writer * w)53 CsvWriter *CsvWriterOpen(Writer *w)
54 {
55 return CsvWriterOpenSpecifyTerminate(w, true);
56 }
57
58 /*****************************************************************************/
59
CsvWriterField(CsvWriter * csvw,const char * str)60 void CsvWriterField(CsvWriter * csvw, const char *str)
61 {
62 assert(csvw != NULL);
63
64 if (csvw->beginning_of_line)
65 {
66 csvw->beginning_of_line = false;
67 }
68 else
69 {
70 WriterWriteChar(csvw->w, ',');
71 }
72
73 if (strpbrk(str, "\",\r\n"))
74 {
75 WriteCsvEscapedString(csvw->w, str);
76 }
77 else
78 {
79 WriterWrite(csvw->w, str);
80 }
81 }
82
83 /*****************************************************************************/
84
CsvWriterFieldF(CsvWriter * csvw,const char * fmt,...)85 void CsvWriterFieldF(CsvWriter * csvw, const char *fmt, ...)
86 {
87 va_list ap;
88
89 va_start(ap, fmt);
90 CsvWriterFieldVF(csvw, fmt, ap);
91 va_end(ap);
92 }
93
94 /*****************************************************************************/
95
CsvWriterNewRecord(CsvWriter * csvw)96 void CsvWriterNewRecord(CsvWriter * csvw)
97 {
98 assert(csvw != NULL);
99
100 WriterWrite(csvw->w, "\r\n");
101 csvw->beginning_of_line = true;
102 }
103
104 /*****************************************************************************/
105
CsvWriterClose(CsvWriter * csvw)106 void CsvWriterClose(CsvWriter * csvw)
107 {
108 assert(csvw != NULL);
109
110 if (!csvw->beginning_of_line && csvw->terminate_last_line)
111 {
112 WriterWrite(csvw->w, "\r\n");
113 }
114 free(csvw);
115 }
116
117 /*****************************************************************************/
118
CsvWriterFieldVF(CsvWriter * csvw,const char * fmt,va_list ap)119 static void CsvWriterFieldVF(CsvWriter * csvw, const char *fmt, va_list ap)
120 {
121 /*
122 * We are unable to avoid allocating memory here, as we need full string
123 * contents before deciding whether there is a " in string, and hence whether
124 * the field needs to be escaped in CSV
125 */
126 char *str;
127
128 xvasprintf(&str, fmt, ap);
129 CsvWriterField(csvw, str);
130 free(str);
131 }
132
133 /*****************************************************************************/
134
WriteCsvEscapedString(Writer * w,const char * s)135 static void WriteCsvEscapedString(Writer *w, const char *s)
136 {
137 WriterWriteChar(w, '"');
138 while (*s)
139 {
140 if (*s == '"')
141 {
142 WriterWriteChar(w, '"');
143 }
144 WriterWriteChar(w, *s);
145 s++;
146 }
147 WriterWriteChar(w, '"');
148 }
149
CsvWriterGetWriter(CsvWriter * csvw)150 Writer *CsvWriterGetWriter(CsvWriter *csvw)
151 {
152 assert(csvw != NULL);
153 return csvw->w;
154 }
155