KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_file_utils.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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26// For PCB parsing
29#include <richio.h>
30#include <board.h>
31#include <footprint.h>
32
34
35namespace KI_TEST
36{
37
38#ifndef QA_PCBNEW_DATA_LOCATION
39 #define QA_PCBNEW_DATA_LOCATION "???"
40#endif
41
43{
44 const char* env = std::getenv( "KICAD_TEST_PCBNEW_DATA_DIR" );
45 std::string fn;
46
47 if( !env )
48 {
49 // Use the compiled-in location of the data dir (i.e. where the files were at build time)
51 }
52 else
53 {
54 // Use whatever was given in the env var
55 fn = env;
56 }
57
58 // Ensure the string ends in / to force a directory interpretation
59 fn += "/";
60
61 return fn;
62}
63
64
65void DumpBoardToFile( BOARD& board, const std::string& aFilename )
66{
68 io.SaveBoard( aFilename, &board );
69}
70
71
72std::unique_ptr<BOARD_ITEM> ReadBoardItemFromStream( std::istream& aStream )
73{
74 // Take input from stdin
76 reader.SetStream( aStream );
77
78 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
79 std::unique_ptr<BOARD_ITEM> board;
80
81 try
82 {
83 board.reset( parser.Parse() );
84 }
85 catch( const IO_ERROR& e )
86 {
87 throw e;
88 }
89
90 return board;
91}
92
93
94std::unique_ptr<BOARD> ReadBoardFromFileOrStream( const std::string& aFilename,
95 std::istream& aFallback )
96{
97 std::istream* in_stream = nullptr;
98 std::ifstream file_stream;
99
100 if( aFilename.empty() )
101 {
102 // no file, read stdin
103 in_stream = &aFallback;
104 }
105 else
106 {
107 file_stream.open( aFilename );
108
109 if( !file_stream.is_open() )
110 {
111 return nullptr;
112 }
113
114 in_stream = &file_stream;
115 }
116
117 return ReadItemFromStream<BOARD>( *in_stream );
118}
119
120
121std::unique_ptr<FOOTPRINT> ReadFootprintFromFileOrStream( const std::string& aFilename,
122 std::istream& aFallback )
123{
124 std::istream* in_stream = nullptr;
125 std::ifstream file_stream;
126
127 if( aFilename.empty() )
128 {
129 // no file, read stdin
130 in_stream = &aFallback;
131 }
132 else
133 {
134 file_stream.open( aFilename );
135
136 if( !file_stream.is_open() )
137 {
138 return nullptr;
139 }
140
141 in_stream = &file_stream;
142 }
143
144 return ReadItemFromStream<FOOTPRINT>( *in_stream );
145}
146
147
148void DumpFootprintToFile( const FOOTPRINT& aFootprint, const std::string& aLibraryPath )
149{
151 io.FootprintSave( aLibraryPath, &aFootprint, nullptr );
152}
153
154
155} // namespace KI_TEST
#define QA_PCBNEW_DATA_LOCATION
General utilities for PCB file IO for QA programs.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aFootprint to an existing library located at aLibraryPath.
void SaveBoard(const wxString &aFileName, BOARD *aBoard, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aBoard to a storage file in a format that this PCB_IO implementation knows about or it can be u...
LINE_READER that wraps a given std::istream instance.
void SetStream(std::istream &aStream)
Set the stream for this line reader.
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.
std::unique_ptr< FOOTPRINT > ReadFootprintFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
void DumpBoardToFile(BOARD &board, const std::string &aFilename)
Utility function to simply write a Board out to a file.
void DumpFootprintToFile(const FOOTPRINT &aFootprint, const std::string &aLibraryPath)
Same as DumpBoardToFile, but for footprints.
std::unique_ptr< BOARD_ITEM > ReadBoardItemFromStream(std::istream &aStream)
Utility function to read a BOARD_ITEM (probably a FOOTPRINT or a BOARD) from a file.
Pcbnew s-expression file format parser definition.