KiCad PCB EDA Suite
Loading...
Searching...
No Matches
odb_attribute.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 * Author: SYSUEric <[email protected]>.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "odb_attribute.h"
22#include <sstream>
23#include <iomanip>
24
25
26size_t ATTR_MANAGER::GetTextIndex( std::unordered_map<std::string, size_t>& aMap,
27 std::vector<std::pair<size_t, std::string>>& aVec,
28 const std::string& aText )
29{
30 if( aMap.count( aText ) )
31 {
32 return aMap.at( aText );
33 }
34 else
35 {
36 auto index = aMap.size();
37 aMap.emplace( aText, index );
38 aVec.emplace_back( index, aText );
39
40 return index;
41 }
42}
43
44
45size_t ATTR_MANAGER::GetAttrNameNumber( const wxString& aName )
46{
47 return GetTextIndex( m_attrNames, m_attrNameVec, aName.Lower().ToStdString() );
48}
49
50
51size_t ATTR_MANAGER::GetAttrTextNumber( const wxString& aText )
52{
53 return GetTextIndex( m_attrTexts, m_attrTextVec, aText.Upper().ToStdString() );
54}
55
56
57void ATTR_RECORD_WRITER::WriteAttributes( std::ostream& ost ) const
58{
59 ODB::CHECK_ONCE once;
60
61 // Some readers need the attribute and ID sections even when both are empty
62 if( m_ODBattributes.empty() )
63 {
64 ost << " ;;";
65 return;
66 }
67
68 ost << " ;";
69
70 for( const auto& attr : m_ODBattributes )
71 {
72 if( !once() )
73 ost << ",";
74
75 ost << attr.first;
76
77 if( attr.second.size() )
78 ost << "=" << attr.second;
79 }
80}
81
82
83void ATTR_MANAGER::WriteAttributesName( std::ostream& ost, const std::string& prefix ) const
84{
85 for( const auto& [n, name] : m_attrNameVec )
86 {
87 ost << prefix << "@" << n << " " << name << std::endl;
88 }
89}
90
91
92void ATTR_MANAGER::WriteAttributesText( std::ostream& ost, const std::string& prefix ) const
93{
94 for( const auto& [n, name] : m_attrTextVec )
95 {
96 ost << prefix << "&" << n << " " << name << std::endl;
97 }
98}
99
100
101void ATTR_MANAGER::WriteAttributes( std::ostream& ost, const std::string& prefix ) const
102{
103 ost << std::endl << "#\n#Feature attribute names\n#" << std::endl;
104 WriteAttributesName( ost );
105
106 ost << std::endl << "#\n#Feature attribute text strings\n#" << std::endl;
107 WriteAttributesText( ost );
108}
int index
const char * name
void WriteAttributesText(std::ostream &ost, const std::string &prefix="") const
std::vector< std::pair< size_t, std::string > > m_attrTextVec
std::vector< std::pair< size_t, std::string > > m_attrNameVec
size_t GetTextIndex(std::unordered_map< std::string, size_t > &aMap, std::vector< std::pair< size_t, std::string > > &aVec, const std::string &aText)
void WriteAttributes(std::ostream &ost, const std::string &prefix="") const
size_t GetAttrTextNumber(const wxString &aName)
std::unordered_map< std::string, size_t > m_attrNames
size_t GetAttrNameNumber(const wxString &name)
void WriteAttributesName(std::ostream &ost, const std::string &prefix="") const
std::unordered_map< std::string, size_t > m_attrTexts
void WriteAttributes(std::ostream &ost) const
std::map< unsigned int, std::string > m_ODBattributes