KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_lib_cache.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) 2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * @author Wayne Stambaugh <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "sch_io_lib_cache.h"
23
24#include <lib_symbol.h>
25#include <wx_filename.h>
26
27
28SCH_IO_LIB_CACHE::SCH_IO_LIB_CACHE( const wxString& aFullPathAndFileName ) :
29 m_modHash( 1 ),
30 m_fileName( aFullPathAndFileName ),
31 m_libFileName( aFullPathAndFileName ),
32 m_isWritable( true ),
33 m_isModified( false )
34{
35 m_libType = SCH_LIB_TYPE::LT_EESCHEMA;
36}
37
38
40{
41 // When the cache is destroyed, all of the alias objects on the heap should be deleted.
42 for( auto& symbol : m_symbols )
43 delete symbol.second;
44
45 m_symbols.clear();
46}
47
48
49void SCH_IO_LIB_CACHE::Save( const std::optional<bool>& aOpt )
50{
51 wxCHECK( false, /* void */ );
52}
53
54
56{
57 wxFileName fn( m_libFileName );
58
59 // If m_libFileName is a symlink follow it to the real source file
61 return fn;
62}
63
64
66{
67 wxFileName fn = GetRealFile();
68
69 // update the writable flag while we have a wxFileName, in a network this
70 // is possibly quite dynamic anyway.
71 m_isWritable = fn.IsFileWritable();
72
73 return fn.GetModificationTime();
74}
75
76
77bool SCH_IO_LIB_CACHE::IsFile( const wxString& aFullPathAndFileName ) const
78{
79 return m_fileName == aFullPathAndFileName;
80}
81
82
84{
85 wxFileName fn = GetRealFile();
86
87 if( m_fileModTime.IsValid() && fn.IsOk() && fn.FileExists() )
88 return fn.GetModificationTime() != m_fileModTime;
89
90 return false;
91}
92
93
95{
96 wxCHECK_MSG( aSymbol != nullptr, nullptr, "NULL pointer cannot be removed from library." );
97
98 LIB_SYMBOL* firstChild = nullptr;
99 LIB_SYMBOL_MAP::iterator it = m_symbols.find( aSymbol->GetName() );
100
101 if( it == m_symbols.end() )
102 return nullptr;
103
104 // If the entry pointer doesn't match the name it is mapped to in the library, we
105 // have done something terribly wrong.
106 wxCHECK_MSG( &*it->second == aSymbol, nullptr,
107 "Pointer mismatch while attempting to remove alias entry <" + aSymbol->GetName() +
108 "> from library cache <" + m_libFileName.GetName() + ">." );
109
110 // If the symbol is a root symbol used by other symbols find the first alias that uses
111 // the root symbol and make it the new root.
112 if( aSymbol->IsRoot() )
113 {
114 for( const std::pair<const wxString, LIB_SYMBOL*>& entry : m_symbols )
115 {
116 if( entry.second->IsAlias()
117 && entry.second->GetParent().lock() == aSymbol->SharedPtr() )
118 {
119 firstChild = entry.second;
120 break;
121 }
122 }
123
124 if( firstChild )
125 {
126 for( SCH_ITEM& drawItem : aSymbol->GetDrawItems() )
127 {
128 if( drawItem.Type() == SCH_FIELD_T )
129 {
130 SCH_FIELD& field = static_cast<SCH_FIELD&>( drawItem );
131
132 if( firstChild->FindField( field.GetCanonicalName() ) )
133 continue;
134 }
135
136 SCH_ITEM* newItem = (SCH_ITEM*) drawItem.Clone();
137 drawItem.SetParent( firstChild );
138 firstChild->AddDrawItem( newItem );
139 }
140
141 // Reparent the remaining aliases.
142 for( const std::pair<const wxString, LIB_SYMBOL*>& entry : m_symbols )
143 {
144 if( entry.second->IsAlias()
145 && entry.second->GetParent().lock() == aSymbol->SharedPtr() )
146 {
147 entry.second->SetParent( firstChild );
148 }
149 }
150 }
151 }
152
153 m_symbols.erase( it );
154 delete aSymbol;
155 m_isModified = true;
157 return firstChild;
158}
159
160
162{
163 // aSymbol is cloned in SYMBOL_LIB::AddSymbol(). The cache takes ownership of aSymbol.
164 wxString name = aSymbol->GetName();
165 LIB_SYMBOL_MAP::iterator it = m_symbols.find( name );
166
167 if( it != m_symbols.end() )
168 {
169 removeSymbol( it->second );
170 }
171
172 m_symbols[ name ] = const_cast< LIB_SYMBOL* >( aSymbol );
173 m_isModified = true;
175}
176
177
179{
180 LIB_SYMBOL_MAP::iterator it = m_symbols.find( aName );
181
182 if( it != m_symbols.end() )
183 {
184 return it->second;
185 }
186
187 return nullptr;
188}
const char * name
Definition: DXF_plotter.cpp:57
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:104
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:85
Define a library symbol object.
Definition: lib_symbol.h:78
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition: lib_symbol.h:194
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition: lib_symbol.h:510
wxString GetName() const override
Definition: lib_symbol.h:137
LIB_SYMBOL_SPTR SharedPtr() const
Definition: lib_symbol.h:89
SCH_FIELD * FindField(const wxString &aFieldName, bool aCaseInsensitive=false)
Find a field within this symbol matching aFieldName and returns it or NULL if not found.
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
Definition: lib_symbol.cpp:808
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: sch_field.cpp:1253
bool IsFile(const wxString &aFullPathAndFileName) const
virtual LIB_SYMBOL * GetSymbol(const wxString &aName)
wxDateTime GetLibModificationTime()
virtual void AddSymbol(const LIB_SYMBOL *aSymbol)
SCH_LIB_TYPE m_libType
LIB_SYMBOL_MAP m_symbols
wxFileName GetRealFile() const
bool IsFileChanged() const
wxDateTime m_fileModTime
wxFileName m_libFileName
virtual void Save(const std::optional< bool > &aOpt=std::nullopt)
Save the entire library to file m_libFileName;.
SCH_IO_LIB_CACHE(const wxString &aLibraryPath)
virtual ~SCH_IO_LIB_CACHE()
LIB_SYMBOL * removeSymbol(LIB_SYMBOL *aAlias)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:168
static void ResolvePossibleSymlinks(wxFileName &aFilename)
Definition: wx_filename.cpp:92
@ SCH_FIELD_T
Definition: typeinfo.h:150