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 (C) 2024 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 along
18 * with this program. If not, see <http://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 for( const auto& attr : attributes )
62 {
63 if( once() )
64 ost << ";";
65 else
66 ost << ",";
67 ost << attr.first;
68 if( attr.second.size() )
69 ost << "=" << attr.second;
70 }
71
72 ost << ";";
73}
74
75
76void ATTR_MANAGER::WriteAttributesName( std::ostream& ost, const std::string& prefix ) const
77{
78 for( const auto& [n, name] : m_attrNameVec )
79 {
80 ost << prefix << "@" << n << " " << name << std::endl;
81 }
82}
83
84
85void ATTR_MANAGER::WriteAttributesText( std::ostream& ost, const std::string& prefix ) const
86{
87 for( const auto& [n, name] : m_attrTextVec )
88 {
89 ost << prefix << "&" << n << " " << name << std::endl;
90 }
91}
92
93
94void ATTR_MANAGER::WriteAttributes( std::ostream& ost, const std::string& prefix ) const
95{
96 ost << std::endl << "#\n#Feature attribute names\n#" << std::endl;
98
99 ost << std::endl << "#\n#Feature attribute text strings\n#" << std::endl;
100 WriteAttributesText( ost );
101}
const char * name
Definition: DXF_plotter.cpp:57
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
std::map< unsigned int, std::string > attributes
void WriteAttributes(std::ostream &ost) const