KiCad PCB EDA Suite
Loading...
Searching...
No Matches
import_design_rules.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 "import_design_rules.h"
21
22#include <reporter.h>
24#include <wx/file.h>
25#include <wx/filefn.h>
26#include <wx/log.h>
27
28
29bool SaveImportedDesignRules( const wxString& aRules, const wxFileName& aBoardPath, REPORTER& aReporter )
30{
31 if( aRules.IsEmpty() )
32 return false;
33
34 // a nameless board would put the rules in a hidden file
35 if( aBoardPath.GetName().IsEmpty() )
36 {
37 aReporter.Report( _( "Imported design rules were not saved, the board has no name.\n" ), RPT_SEVERITY_WARNING );
38 return false;
39 }
40
41 wxFileName rulesPath( aBoardPath );
42 rulesPath.SetExt( FILEEXT::DesignRulesFileExtension );
43
44 // Creating exclusively both refuses an existing file and closes the race a FileExists test would leave open
45 wxFile rulesFile;
46
47 {
48 // Refusal is expected and reported below, so keep wx from raising its own error
49 wxLogNull noLog;
50 rulesFile.Open( rulesPath.GetFullPath(), wxFile::write_excl );
51 }
52
53 if( !rulesFile.IsOpened() )
54 {
55 aReporter.Report( wxString::Format( rulesPath.FileExists()
56 ? _( "Imported design rules were not saved, '%s' already exists.\n" )
57 : _( "Could not write imported design rules to '%s'\n" ),
58 rulesPath.GetFullPath() ),
60 return false;
61 }
62
63 bool written = rulesFile.Write( aRules );
64
65 if( !rulesFile.Close() )
66 written = false;
67
68 // A partial file would not parse, and because it is created exclusively it would block the next import
69 if( !written )
70 {
71 wxRemoveFile( rulesPath.GetFullPath() );
72
73 aReporter.Report( wxString::Format( _( "Could not write imported design rules to '%s'\n" ),
74 rulesPath.GetFullPath() ),
76 return false;
77 }
78
79 return true;
80}
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
#define _(s)
static const std::string DesignRulesFileExtension
bool SaveImportedDesignRules(const wxString &aRules, const wxFileName &aBoardPath, REPORTER &aReporter)
Save the custom rules an importer derived beside the board it produced.
@ RPT_SEVERITY_WARNING
Definition of file extensions used in Kicad.