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 The 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 <common.h>
25#include <lib_symbol.h>
26#include <wx_filename.h>
27
28
29SCH_IO_LIB_CACHE::SCH_IO_LIB_CACHE( const wxString& aFullPathAndFileName ) :
30 m_modHash( 1 ),
31 m_fileName( aFullPathAndFileName ),
32 m_libFileName( aFullPathAndFileName ),
33 m_fileModTime( 0 ),
34 m_isWritable( true ),
35 m_isModified( false )
36{
38}
39
40
42{
43 // When the cache is destroyed, all of the alias objects on the heap should be deleted.
44 for( auto& symbol : m_symbols )
45 delete symbol.second;
46
47 m_symbols.clear();
48}
49
50
51void SCH_IO_LIB_CACHE::Save( const std::optional<bool>& aOpt )
52{
53 wxCHECK( false, /* void */ );
54}
55
56
58{
59 wxFileName fn( m_libFileName );
60
61 // If m_libFileName is a symlink follow it to the real source file
63 return fn;
64}
65
66
68{
69 wxFileName fn = GetRealFile();
70 wxString wildcard = fn.GetFullName();
71
72 // Update the writable flag while we have a wxFileName, in a network this is possibly quite dynamic anyway.
73 if( !fn.IsDir() )
74 {
75 m_isWritable = fn.IsFileWritable();
76 return fn.GetModificationTime().GetValue().GetValue();
77 }
78 else
79 {
80 m_isWritable = fn.IsDirWritable();
81 wildcard = wxS( "*." ) + wxString( FILEEXT::KiCadSymbolLibFileExtension );
82 return TimestampDir( fn.GetPath(), wildcard );
83 }
84}
85
86
87bool SCH_IO_LIB_CACHE::IsFile( const wxString& aFullPathAndFileName ) const
88{
89 return m_fileName == aFullPathAndFileName;
90}
91
92
94{
95 wxFileName fn = GetRealFile();
96
97 if( !fn.IsOk() )
98 return false;
99
100 if( !fn.IsDir() && fn.IsFileReadable() )
101 return fn.GetModificationTime().GetValue().GetValue() != m_fileModTime;
102
103 if( fn.IsDir() && fn.IsDirReadable() )
104 return TimestampDir( fn.GetPath(),
105 wxS( "*." ) + wxString( FILEEXT::KiCadSymbolLibFileExtension ) ) != m_fileModTime;
106
107 return false;
108}
109
110
112{
113 wxCHECK_MSG( aSymbol != nullptr, nullptr, "NULL pointer cannot be removed from library." );
114
115 LIB_SYMBOL* firstChild = nullptr;
116 LIB_SYMBOL_MAP::iterator it = m_symbols.find( aSymbol->GetName() );
117
118 if( it == m_symbols.end() )
119 return nullptr;
120
121 // If the entry pointer doesn't match the name it is mapped to in the library, we
122 // have done something terribly wrong.
123 wxCHECK_MSG( &*it->second == aSymbol, nullptr,
124 "Pointer mismatch while attempting to remove alias entry <" + aSymbol->GetName() +
125 "> from library cache <" + m_libFileName.GetName() + ">." );
126
127 // If the symbol is a root symbol used by other symbols find the first derived symbol that uses
128 // the root symbol and make it the new root.
129 if( aSymbol->IsRoot() )
130 {
131 for( const std::pair<const wxString, LIB_SYMBOL*>& entry : m_symbols )
132 {
133 if( entry.second->IsDerived()
134 && entry.second->GetParent().lock() == aSymbol->SharedPtr() )
135 {
136 firstChild = entry.second;
137 break;
138 }
139 }
140
141 if( firstChild )
142 {
143 for( SCH_ITEM& drawItem : aSymbol->GetDrawItems() )
144 {
145 if( drawItem.Type() == SCH_FIELD_T )
146 {
147 SCH_FIELD& field = static_cast<SCH_FIELD&>( drawItem );
148
149 if( firstChild->GetField( field.GetCanonicalName() ) )
150 continue;
151 }
152
153 SCH_ITEM* newItem = (SCH_ITEM*) drawItem.Clone();
154 drawItem.SetParent( firstChild );
155 firstChild->AddDrawItem( newItem );
156 }
157
158 // Reparent the remaining derived symbols.
159 for( const std::pair<const wxString, LIB_SYMBOL*>& entry : m_symbols )
160 {
161 if( entry.second->IsDerived()
162 && entry.second->GetParent().lock() == aSymbol->SharedPtr() )
163 {
164 entry.second->SetParent( firstChild );
165 }
166 }
167 }
168 }
169
170 m_symbols.erase( it );
171 delete aSymbol;
172 m_isModified = true;
174 return firstChild;
175}
176
177
179{
180 // aSymbol is cloned in SYMBOL_LIB::AddSymbol(). The cache takes ownership of aSymbol.
181 wxString name = aSymbol->GetName();
182 LIB_SYMBOL_MAP::iterator it = m_symbols.find( name );
183
184 if( it != m_symbols.end() )
185 {
186 removeSymbol( it->second );
187 }
188
189 m_symbols[ name ] = const_cast< LIB_SYMBOL* >( aSymbol );
190 m_isModified = true;
192}
193
194
196{
197 LIB_SYMBOL_MAP::iterator it = m_symbols.find( aName );
198
199 if( it != m_symbols.end() )
200 {
201 return it->second;
202 }
203
204 return nullptr;
205}
const char * name
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.h:113
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition eda_item.cpp:118
Define a library symbol object.
Definition lib_symbol.h:85
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition lib_symbol.h:205
SCH_FIELD * GetField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName; return nullptr if not found.
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition lib_symbol.h:688
wxString GetName() const override
Definition lib_symbol.h:148
std::shared_ptr< LIB_SYMBOL > SharedPtr() const
http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared.
Definition lib_symbol.h:95
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
bool IsFile(const wxString &aFullPathAndFileName) const
virtual LIB_SYMBOL * GetSymbol(const wxString &aName)
virtual void AddSymbol(const LIB_SYMBOL *aSymbol)
SCH_LIB_TYPE m_libType
LIB_SYMBOL_MAP m_symbols
wxFileName GetRealFile() const
bool IsFileChanged() const
long long GetLibModificationTime()
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)
LIB_SYMBOL * removeSymbol(LIB_SYMBOL *aAlias)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
static void ResolvePossibleSymlinks(wxFileName &aFilename)
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition common.cpp:614
The common library.
static const std::string KiCadSymbolLibFileExtension
@ SCH_FIELD_T
Definition typeinfo.h:154