KiCad PCB EDA Suite
Loading...
Searching...
No Matches
table_io.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <table_io.h>
21
22#include <clipboard.h>
23#include <eda_base_frame.h>
24#include <io/csv.h>
25#include <kiplatform/ui.h>
26#include <richio.h>
28
29#include <wx/filedlg.h>
30#include <wx/intl.h>
31#include <wx/wfstream.h>
32
33
34std::optional<std::vector<std::vector<wxString>>> ReadTableFromFileOrClipboard( EDA_BASE_FRAME& aFrame, bool aFromFile )
35{
36 wxString path;
37
38 if( aFromFile )
39 {
40 wxFileDialog dlg( &aFrame, _( "Select data file" ), "", "", FILEEXT::CsvTsvFileWildcard(),
41 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
42
44
45 if( dlg.ShowModal() == wxID_CANCEL )
46 return std::nullopt;
47
48 path = dlg.GetPath();
49 }
50
51 std::vector<std::vector<wxString>> table;
52 bool ok = false;
53
54 if( !path.IsEmpty() )
55 ok = AutoDecodeCSV( SafeReadFile( path, "r" ), table );
56 else
58
59 if( !ok )
60 return std::nullopt;
61
62 return table;
63}
64
65
66void WriteTableToFileOrClipboard( const wxString& aToFile, const std::vector<std::vector<wxString>>& aTable )
67{
68 if( !aToFile.IsEmpty() )
69 {
70 wxFileOutputStream os( aToFile );
71 CSV_WRITER writer( os );
72 writer.WriteLines( aTable );
73 }
74 else
75 {
77 }
78}
void WriteLines(const std::vector< std::vector< wxString > > &aRows)
Write a vector of rows to the stream.
Definition csv.cpp:18
The base frame for deriving all KiCad main window classes.
bool SaveTabularDataToClipboard(const std::vector< std::vector< wxString > > &aData)
Store tabular data to the system clipboard.
bool GetTabularDataFromClipboard(std::vector< std::vector< wxString > > &aData)
Attempt to get tabular data from the clipboard.
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
#define _(s)
Base window classes and related definitions.
static wxString CsvTsvFileWildcard()
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:521
wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition richio.cpp:51
void WriteTableToFileOrClipboard(const wxString &aToFile, const std::vector< std::vector< wxString > > &aTable)
Write aTable as CSV to aToFile (if non-empty) or to the system clipboard.
Definition table_io.cpp:66
std::optional< std::vector< std::vector< wxString > > > ReadTableFromFileOrClipboard(EDA_BASE_FRAME &aFrame, bool aFromFile)
Read a CSV/TSV table from a file selected via a dialog (aFromFile) or from the system clipboard.
Definition table_io.cpp:34
std::string path
Definition of file extensions used in Kicad.