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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "allegro_db_utils.h"
25
26
27using namespace ALLEGRO;
28
29
30uint32_t ALLEGRO::GetPrimaryNext( const BLOCK_BASE& aBlock )
31{
32 return aBlock.GetNext();
33}
34
35
36std::optional<FIELD_VALUE> ALLEGRO::GetFirstFieldOfType( const BRD_DB& aDb, uint32_t aFieldsPtr, uint32_t aEndKey,
37 uint16_t aFieldCode )
38{
39 LL_WALKER fieldWalker{ aFieldsPtr, aEndKey, aDb };
40
41 for( const BLOCK_BASE* block : fieldWalker )
42 {
43 if( block->GetBlockType() != 0x03 )
44 continue;
45
46 const auto& field = BlockDataAs<BLK_0x03_FIELD>( *block );
47
48 if( field.m_Hdr1 != aFieldCode )
49 continue;
50
51 switch( field.m_SubType )
52 {
53 case 0x68:
54 {
55 const std::string* str = std::get_if<std::string>( &field.m_Substruct );
56
57 if( str )
58 return wxString( *str );
59
60 break;
61 }
62 case 0x66:
63 {
64 const uint32_t* val = std::get_if<uint32_t>( &field.m_Substruct );
65
66 if( val )
67 return *val;
68
69 break;
70 }
71 }
72 }
73
74 return std::nullopt;
75}
76
77
78std::optional<int> ALLEGRO::GetFirstFieldOfTypeInt( const BRD_DB& aDb, uint32_t aFieldsPtr, uint32_t aEndKey,
79 uint16_t aFieldCode )
80{
81 std::optional<FIELD_VALUE> result = GetFirstFieldOfType( aDb, aFieldsPtr, aEndKey, aFieldCode );
82
83 if( !result.has_value() )
84 return std::nullopt;
85
86 if( uint32_t* val = std::get_if<uint32_t>( &result.value() ) )
87 return static_cast<int>( *val );
88
89 return std::nullopt;
90}
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:47
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.
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.