KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_info.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) 2011 Jean-Pierre Charras, <[email protected]>
5 * Copyright (C) 2013-2016 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
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
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22/*
23 * Functions to read footprint libraries and fill m_footprints by available footprints names
24 * and their documentation (comments and keywords)
25 */
26
27#include <footprint_info.h>
29#include <string_utils.h>
30#include <lib_id.h>
31#include <wx/tokenzr.h>
32
33FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aLibNickname,
34 const wxString& aFootprintName )
35{
36 if( aFootprintName.IsEmpty() )
37 return nullptr;
38
39 for( std::unique_ptr<FOOTPRINT_INFO>& fp : m_list )
40 {
41 if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
42 return fp.get();
43 }
44
45 return nullptr;
46}
47
48
49FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aFootprintName )
50{
51 if( aFootprintName.IsEmpty() )
52 return nullptr;
53
54 LIB_ID fpid;
55
56 wxCHECK_MSG( fpid.Parse( aFootprintName ) < 0, nullptr,
57 wxString::Format( wxT( "'%s' is not a valid LIB_ID." ), aFootprintName ) );
58
59 return GetFootprintInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
60}
61
62
64{
65 const wxString keywords = GetKeywords();
66
67 m_searchTerms.clear();
68 m_searchTerms.reserve( 6 );
69
70 m_searchTerms.emplace_back( SEARCH_TERM( GetLibNickname(), 4 ) );
71 m_searchTerms.emplace_back( SEARCH_TERM( GetName(), 8, true ) );
72 m_searchTerms.emplace_back( SEARCH_TERM( GetLIB_ID().Format(), 16, true ) );
73
74 wxStringTokenizer keywordTokenizer( keywords, " \t\r\n", wxTOKEN_STRTOK );
75
76 while( keywordTokenizer.HasMoreTokens() )
77 m_searchTerms.emplace_back( SEARCH_TERM( keywordTokenizer.GetNextToken(), 4 ) );
78
79 // Also include keywords as one long string, just in case
80 m_searchTerms.emplace_back( SEARCH_TERM( keywords, 1 ) );
81 m_searchTerms.emplace_back( SEARCH_TERM( GetDesc(), 1 ) );
82}
83
84
85std::vector<SEARCH_TERM>& FOOTPRINT_INFO::GetSearchTerms()
86{
87 // Empty means not built yet, cacheSearchTerms() always adds the name terms
88 if( m_searchTerms.empty() )
90
91 return m_searchTerms;
92}
93
94
95bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
96{
97 return aLibrary == m_nickname;
98}
99
100
101bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs )
102{
103 int retv = StrNumCmp( lhs.m_nickname, rhs.m_nickname, false );
104
105 if( retv != 0 )
106 return retv < 0;
107
108 // Technically footprint names are not case sensitive because the file name is used
109 // as the footprint name. On windows this would be problematic because windows does
110 // not support case sensitive file names by default. This should not cause any issues
111 // and allow for a future change to use the name defined in the footprint file.
112 return StrNumCmp( lhs.m_fpname, rhs.m_fpname, false ) < 0;
113}
114
115
117{
118 wxString messages;
119
120 while( std::unique_ptr<IO_ERROR> error = PopError() )
121 {
122 if( !messages.IsEmpty() )
123 messages += wxS( "\n" );
124
125 messages += error->Problem();
126 }
127
128 return messages;
129}
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
LIB_ID GetLIB_ID() const override
wxString GetName() const override
wxString m_fpname
Module name.
wxString GetLibNickname() const override
void cacheSearchTerms()
Safe to build once and keep.
wxString GetKeywords()
wxString m_nickname
library as known in FP_LIB_TABLE
std::vector< SEARCH_TERM > & GetSearchTerms() override
Returns the cache itself, not a copy.
wxString GetDesc() override
std::vector< SEARCH_TERM > m_searchTerms
wxString GetErrorMessages()
Returns all accumulated errors as a newline-separated string for display in the status bar.
FOOTPRINT_INFO * GetFootprintInfo(const wxString &aFootprintName)
Get info for a footprint by id.
std::vector< std::unique_ptr< FOOTPRINT_INFO > > m_list
std::unique_ptr< IO_ERROR > PopError()
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
bool operator<(const FOOTPRINT_INFO &lhs, const FOOTPRINT_INFO &rhs)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition ptree.cpp:194
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
A structure for storing weighted search terms.