KiCad PCB EDA Suite
pcb_calculator_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 (C) 1992-2022 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 along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21#include <locale.h>
22
23bool findMatch( wxArrayString& aList, const wxString& aValue, int& aIdx )
24{
25 bool success = false;
26 // Find the previous choice index:
27 aIdx = 0;
28
29 // Some countries use comma instead of point as separator.
30 // The value can be enter with pint or comma
31 // use point for string comparisons:
32 wxString cvalue = aValue;
33 cvalue.Replace( ',', '.' );
34
35 // First compare strings:
36 for( wxString& text : aList )
37 {
38 if( text.IsEmpty() ) // No match found: select the empty line choice
39 break;
40
41 wxString val_str = text.BeforeFirst( ' ' );
42 val_str.Replace( ',', '.' );
43
44 // compare string values
45 if( val_str == cvalue )
46 {
47 success = true;
48 break;
49 }
50
51 aIdx++;
52 }
53
54 // Due to multiple ways to write a double, if string values
55 // do not match, compare double values
56 if( !success )
57 {
58 struct lconv* lc = localeconv();
59 char localeDecimalSeparator = *lc->decimal_point;
60
61 if( localeDecimalSeparator == ',' )
62 cvalue.Replace( '.', ',' );
63
64 double curr_value;
65 cvalue.ToDouble( &curr_value );
66
67 aIdx = 0;
68
69 for( wxString& text : aList )
70 {
71 if( text.IsEmpty() ) // No match found: select the empty line choice
72 break;
73
74 double val;
75 wxString val_str = text.BeforeFirst( ' ' );
76
77 if( localeDecimalSeparator == ',' )
78 val_str.Replace( '.', ',' );
79
80 val_str.ToDouble( &val );;
81
82 if( curr_value == val )
83 {
84 success = true;
85 break;
86 }
87
88 aIdx++;
89 }
90 }
91
92 return success;
93}
bool findMatch(wxArrayString &aList, const wxString &aValue, int &aIdx)