KiCad PCB EDA Suite
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 (C) 2019 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
27 #include <kicad_plugin.h>
28 #include <pcb_parser.h>
29 #include <richio.h>
30 
31 #include <class_board.h>
32 
34 
35 namespace KI_TEST
36 {
37 
38 void DumpBoardToFile( BOARD& board, const std::string& aFilename )
39 {
40  PCB_IO io;
41  io.Save( aFilename, &board );
42 }
43 
44 
45 std::unique_ptr<BOARD_ITEM> ReadBoardItemFromFile( const std::string& aFilename )
46 {
47  FILE_LINE_READER reader( aFilename );
48 
49  PCB_PARSER parser;
50  parser.SetLineReader( &reader );
51 
52  return std::unique_ptr<BOARD_ITEM>( parser.Parse() );
53 }
54 
55 
56 std::unique_ptr<BOARD_ITEM> ReadBoardItemFromStream( std::istream& aStream )
57 {
58  // Take input from stdin
60  reader.SetStream( aStream );
61 
62  PCB_PARSER parser;
63 
64  parser.SetLineReader( &reader );
65 
66  std::unique_ptr<BOARD_ITEM> board;
67 
68  try
69  {
70  board.reset( parser.Parse() );
71  }
72  catch( const IO_ERROR& parse_error )
73  {
74  std::cerr << parse_error.Problem() << std::endl;
75  std::cerr << parse_error.Where() << std::endl;
76  }
77 
78  return board;
79 }
80 
81 std::unique_ptr<BOARD> ReadBoardFromFileOrStream(
82  const std::string& aFilename, std::istream& aFallback )
83 {
84  std::istream* in_stream = nullptr;
85  std::ifstream file_stream;
86 
87  if( aFilename.empty() )
88  {
89  // no file, read stdin
90  in_stream = &aFallback;
91  }
92  else
93  {
94  file_stream.open( aFilename );
95  in_stream = &file_stream;
96  }
97 
98  return ReadItemFromStream<BOARD>( *in_stream );
99 }
100 
101 } // namespace KI_TEST
PCB_IO is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
PCB_PARSER reads a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD...
Definition: pcb_parser.h:69
std::unique_ptr< BOARD_ITEM > ReadBoardItemFromStream(std::istream &aStream)
Utility function to read a BOARD_ITEM (probably a MODULE or a BOARD) from a file.
std::unique_ptr< BOARD_ITEM > ReadBoardItemFromFile(const std::string &aFilename)
virtual const wxString Problem() const
what was the problem?
Definition: exceptions.cpp:49
virtual const wxString Where() const
where did the Problem() occur?
Definition: exceptions.cpp:43
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
void SetStream(std::istream &aStream)
Set the stream for this line reader.
LINE_READER that wraps a given std::istream instance.
void DumpBoardToFile(BOARD &board, const std::string &aFilename)
Utility function to simply write a Board out to a file.
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:467
LINE_READER * SetLineReader(LINE_READER *aReader)
Function SetLineReader sets aLineReader into the parser, and returns the previous one,...
Definition: pcb_parser.h:335
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
Pcbnew s-expression file format parser definition.
General utilities for PCB file IO for QA programs.
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
virtual void Save(const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=NULL) override
Function Save will write aBoard to a storage file in a format that this PLUGIN implementation knows a...