1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2019 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 2
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/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 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 <array_axis.h>
25 
26 
27 /**
28  * @return False for schemes like 0,1...9,10
29  *         True for schemes like A,B..Z,AA (where the tens column starts with char 0)
30  */
schemeNonUnitColsStartAt0(ARRAY_AXIS::NUMBERING_TYPE type)31 static bool schemeNonUnitColsStartAt0( ARRAY_AXIS::NUMBERING_TYPE type )
32 {
33     return type == ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_FULL
34            || type == ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_NO_IOSQXZ;
35 }
36 
37 
ARRAY_AXIS()38 ARRAY_AXIS::ARRAY_AXIS() : m_type( NUMBERING_TYPE::NUMBERING_NUMERIC ), m_offset( 0 ), m_step( 1 )
39 {
40 }
41 
42 
GetAlphabet() const43 const wxString& ARRAY_AXIS::GetAlphabet() const
44 {
45     static const wxString alphaNumeric = "0123456789";
46     static const wxString alphaHex = "0123456789ABCDEF";
47     static const wxString alphaFull = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
48     static const wxString alphaNoIOSQXZ = "ABCDEFGHJKLMNPRTUVWY";
49 
50     switch( m_type )
51     {
52     default:
53     case NUMBERING_NUMERIC:
54         return alphaNumeric;
55     case NUMBERING_HEX:
56         return alphaHex;
57     case NUMBERING_ALPHA_NO_IOSQXZ:
58         return alphaNoIOSQXZ;
59     case NUMBERING_ALPHA_FULL:
60         return alphaFull;
61     }
62 }
63 
64 
getNumberingOffset(const wxString & str) const65 OPT<int> ARRAY_AXIS::getNumberingOffset( const wxString& str ) const
66 {
67     if( str.length() == 0 )
68         return OPT<int>{};
69 
70     const wxString& alphabet = GetAlphabet();
71 
72     int       offset = 0;
73     const int radix = alphabet.length();
74 
75     for( unsigned i = 0; i < str.length(); i++ )
76     {
77         int chIndex = alphabet.Find( str[i], false );
78 
79         if( chIndex == wxNOT_FOUND )
80             return OPT<int>{};
81 
82         const bool start0 = schemeNonUnitColsStartAt0( m_type );
83 
84         // eg "AA" is actually index 27, not 26
85         if( start0 && i < str.length() - 1 )
86             chIndex++;
87 
88         offset *= radix;
89         offset += chIndex;
90     }
91 
92     return OPT<int>{ offset };
93 }
94 
95 
SetAxisType(NUMBERING_TYPE aType)96 void ARRAY_AXIS::SetAxisType( NUMBERING_TYPE aType )
97 {
98     m_type = aType;
99 }
100 
101 
SetOffset(const wxString & aOffsetName)102 bool ARRAY_AXIS::SetOffset( const wxString& aOffsetName )
103 {
104     OPT<int> offset = getNumberingOffset( aOffsetName );
105 
106     // The string does not decode to a valid offset
107     if( !offset )
108         return false;
109 
110     SetOffset( *offset );
111     return true;
112 }
113 
114 
SetOffset(int aOffset)115 void ARRAY_AXIS::SetOffset( int aOffset )
116 {
117     m_offset = aOffset;
118 }
119 
120 
GetOffset() const121 int ARRAY_AXIS::GetOffset() const
122 {
123     return m_offset;
124 }
125 
126 
SetStep(int aStep)127 void ARRAY_AXIS::SetStep( int aStep )
128 {
129     m_step = aStep;
130 }
131 
132 
GetItemNumber(int n) const133 wxString ARRAY_AXIS::GetItemNumber( int n ) const
134 {
135     wxString        itemNum;
136     const wxString& alphabet = GetAlphabet();
137 
138     const bool nonUnitColsStartAt0 = schemeNonUnitColsStartAt0( m_type );
139 
140     bool firstRound = true;
141     int  radix = alphabet.Length();
142 
143     n = m_offset + m_step * n;
144 
145     do
146     {
147         int modN = n % radix;
148 
149         if( nonUnitColsStartAt0 && !firstRound )
150             modN--; // Start the "tens/hundreds/etc column" at "Ax", not "Bx"
151 
152         itemNum.insert( 0, 1, alphabet[modN] );
153 
154         n /= radix;
155         firstRound = false;
156     } while( n );
157 
158     return itemNum;
159 }