KiCad PCB EDA Suite
Loading...
Searching...
No Matches
csv.cpp
Go to the documentation of this file.
1
2
3#include "csv.h"
4
5#include <wx/txtstrm.h>
6
7#include <rapidcsv/rapidcsv.h>
8
9
10CSV_WRITER::CSV_WRITER( wxOutputStream& aStream ) :
11 m_stream( aStream ), m_delimiter( wxT( "," ) ),
12 m_quote( wxT( "\"" ) ), m_lineTerminator( wxT( "\n" ) ),
13 m_escape( wxEmptyString )
14{
15}
16
17
18void CSV_WRITER::WriteLines( const std::vector<std::vector<wxString>>& aRows )
19{
20 for( const auto& row : aRows )
21 {
22 WriteLine( row );
23 }
24}
25
26
27void CSV_WRITER::WriteLine( const std::vector<wxString>& cols )
28{
29 wxString line;
30
31 for( size_t i = 0; i < cols.size(); ++i )
32 {
33 wxString colVal = cols[i];
34
35 if( i > 0 )
36 line += m_delimiter;
37
38
39 const bool useEscape = m_escape.size();
40
41 if( useEscape )
42 {
43 colVal.Replace( m_quote, m_escape + m_quote, true );
44 }
45 else
46 {
47 colVal.Replace( m_quote, m_quote + m_quote, true );
48 }
49
50 // Always quote - if that's a problem, we can only quote if the
51 // delimiter (or newlines?) are present in the string.
52 colVal = m_quote + colVal + m_quote;
53
54
55 line += colVal;
56 }
57
58 line += m_lineTerminator;
59
60 // Always write text on file using UTF8 encoding
61 std::string utf8str( line.utf8_string() );
62 m_stream.Write( utf8str.data(), utf8str.length() );
63}
64
65
66bool AutoDecodeCSV( const wxString& aInput, std::vector<std::vector<wxString>>& aData )
67{
68 // Read the first line to determine the delimiter
69 bool trimCells = true;
70 bool skipCommentLines = true;
71 bool skipEmptyLines = true;
72 char commentPrefix = '#';
73 char delimiter = ',';
74
75 // Assume if we find a tab, we are dealing with a TSV file
76 if( aInput.find( '\t' ) != std::string::npos )
77 {
78 delimiter = '\t';
79 }
80
81 std::istringstream inputStream( aInput.ToStdString() );
82
83 rapidcsv::Document doc( inputStream, rapidcsv::LabelParams( -1, -1 ),
84 rapidcsv::SeparatorParams( delimiter, trimCells ), rapidcsv::ConverterParams(),
85 rapidcsv::LineReaderParams( skipCommentLines, commentPrefix, skipEmptyLines ) );
86
87 // Read the data into aData
88 aData.clear();
89
90 for( size_t i = 0; i < doc.GetRowCount(); ++i )
91 {
92 std::vector<wxString>& row = aData.emplace_back();
93 for( size_t j = 0; j < doc.GetColumnCount(); ++j )
94 {
95 std::string cell = doc.GetCell<std::string>( j, i );
96 row.emplace_back( cell );
97 }
98 }
99
100 // Anything in the first row?
101 return aData[0].size() > 0;
102}
void WriteLines(const std::vector< std::vector< wxString > > &aRows)
Write a vector of rows to the stream.
Definition: csv.cpp:18
wxString m_quote
Definition: csv.h:65
void WriteLine(const std::vector< wxString > &aCols)
Write a single row to the stream.
Definition: csv.cpp:27
wxString m_delimiter
Definition: csv.h:64
wxOutputStream & m_stream
Definition: csv.h:63
CSV_WRITER(wxOutputStream &aStream)
Definition: csv.cpp:10
wxString m_lineTerminator
Definition: csv.h:66
wxString m_escape
Definition: csv.h:67
bool AutoDecodeCSV(const wxString &aInput, std::vector< std::vector< wxString > > &aData)
Try to guess the format of a T/CSV file and decode it into aData.
Definition: csv.cpp:66