KiCad PCB EDA Suite
Loading...
Searching...
No Matches
allegro_db_utils.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 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "allegro_db_utils.h"
21
22#include <wx/arrstr.h>
23
24
25using namespace ALLEGRO;
26
27
28uint32_t ALLEGRO::GetPrimaryNext( const BLOCK_BASE& aBlock )
29{
30 return aBlock.GetNext();
31}
32
33
34std::optional<FIELD_VALUE> ALLEGRO::GetFirstFieldOfType( const BRD_DB& aDb, uint32_t aFieldsPtr, uint32_t aEndKey,
35 uint16_t aFieldCode )
36{
37 LL_WALKER fieldWalker{ aFieldsPtr, aEndKey, aDb };
38
39 for( const BLOCK_BASE* block : fieldWalker )
40 {
41 if( block->GetBlockType() != 0x03 )
42 continue;
43
44 const auto& field = BlockDataAs<BLK_0x03_FIELD>( *block );
45
46 if( field.m_Hdr1 != aFieldCode )
47 continue;
48
49 switch( field.m_SubType )
50 {
51 case 0x68:
52 {
53 const std::string* str = std::get_if<std::string>( &field.m_Substruct );
54
55 if( str )
56 return wxString( *str );
57
58 break;
59 }
60 case 0x66:
61 {
62 const uint32_t* val = std::get_if<uint32_t>( &field.m_Substruct );
63
64 if( val )
65 return *val;
66
67 break;
68 }
69 }
70 }
71
72 return std::nullopt;
73}
74
75
76std::optional<int> ALLEGRO::GetFirstFieldOfTypeInt( const BRD_DB& aDb, uint32_t aFieldsPtr, uint32_t aEndKey,
77 uint16_t aFieldCode )
78{
79 std::optional<FIELD_VALUE> result = GetFirstFieldOfType( aDb, aFieldsPtr, aEndKey, aFieldCode );
80
81 if( !result.has_value() )
82 return std::nullopt;
83
84 if( uint32_t* val = std::get_if<uint32_t>( &result.value() ) )
85 return static_cast<int>( *val );
86
87 return std::nullopt;
88}
89
90
91wxString ALLEGRO::DecodeEscapedIdentifiers( const wxString& aName )
92{
93 wxArrayString components = wxSplit( aName, ':', '\0' );
94
95 for( wxString& component : components )
96 {
97 if( component.length() > 2 && component.StartsWith( wxS( "\\" ) ) && component.EndsWith( wxS( "\\" ) )
98 && component.find( '\\', 1 ) == component.length() - 1 )
99 {
100 component = component.Mid( 1, component.length() - 2 );
101 }
102 }
103
104 return wxJoin( components, ':', '\0' );
105}
Utility functions that operate over BRD_DBs and BLOCKs.
The base class for all blocks in the main body of an Allegro file.
uint32_t GetNext() const
An Allegro board database representing the contents of a .brd (and presumably .dra) file.
Definition allegro_db.h:43
Range-for-compatible walker over a linked list of BLOCK_BASE objects in a BRD_DB.
std::optional< FIELD_VALUE > GetFirstFieldOfType(const BRD_DB &aDb, uint32_t aFieldsPtr, uint32_t aEndKey, uint16_t aFieldCode)
Look up the first 0x03 FIELD value of a given type in a linked field chain.
std::optional< int > GetFirstFieldOfTypeInt(const BRD_DB &aDb, uint32_t aFieldsPtr, uint32_t aEndKey, uint16_t aFieldCode)
Convenience wrapper around GetFirstFieldOfType() for integer-valued fields.
uint32_t GetPrimaryNext(const BLOCK_BASE &aBlock)
Get the next block key in the linked list for a given block.
wxString DecodeEscapedIdentifiers(const wxString &aName)
Remove the backslash wrapper Cadence puts around an identifier that holds special characters.
const BLK_T & BlockDataAs(const BLOCK_BASE &aBlock)
Cast a BLOCK_BASE to a typed BLOCK<T> and return the data.
wxString result
Test unit parsing edge cases and error handling.