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
23using namespace ALLEGRO;
24
25
26uint32_t ALLEGRO::GetPrimaryNext( const BLOCK_BASE& aBlock )
27{
28 return aBlock.GetNext();
29}
30
31
32std::optional<FIELD_VALUE> ALLEGRO::GetFirstFieldOfType( const BRD_DB& aDb, uint32_t aFieldsPtr, uint32_t aEndKey,
33 uint16_t aFieldCode )
34{
35 LL_WALKER fieldWalker{ aFieldsPtr, aEndKey, aDb };
36
37 for( const BLOCK_BASE* block : fieldWalker )
38 {
39 if( block->GetBlockType() != 0x03 )
40 continue;
41
42 const auto& field = BlockDataAs<BLK_0x03_FIELD>( *block );
43
44 if( field.m_Hdr1 != aFieldCode )
45 continue;
46
47 switch( field.m_SubType )
48 {
49 case 0x68:
50 {
51 const std::string* str = std::get_if<std::string>( &field.m_Substruct );
52
53 if( str )
54 return wxString( *str );
55
56 break;
57 }
58 case 0x66:
59 {
60 const uint32_t* val = std::get_if<uint32_t>( &field.m_Substruct );
61
62 if( val )
63 return *val;
64
65 break;
66 }
67 }
68 }
69
70 return std::nullopt;
71}
72
73
74std::optional<int> ALLEGRO::GetFirstFieldOfTypeInt( const BRD_DB& aDb, uint32_t aFieldsPtr, uint32_t aEndKey,
75 uint16_t aFieldCode )
76{
77 std::optional<FIELD_VALUE> result = GetFirstFieldOfType( aDb, aFieldsPtr, aEndKey, aFieldCode );
78
79 if( !result.has_value() )
80 return std::nullopt;
81
82 if( uint32_t* val = std::get_if<uint32_t>( &result.value() ) )
83 return static_cast<int>( *val );
84
85 return std::nullopt;
86}
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.
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.