You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.6 KiB

  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. #include <pcb_calculator_utils.h>
  20. #include <locale.h>
  21. bool findMatch( wxArrayString& aList, const wxString& aValue, int& aIdx )
  22. {
  23. bool success = false;
  24. // Find the previous choice index:
  25. aIdx = 0;
  26. // Some countries use comma instead of point as separator.
  27. // The value can be enter with pint or comma
  28. // use point for string comparisons:
  29. wxString cvalue = aValue;
  30. cvalue.Replace( ',', '.' );
  31. // First compare strings:
  32. for( wxString& text : aList )
  33. {
  34. if( text.IsEmpty() ) // No match found: select the empty line choice
  35. break;
  36. wxString val_str = text.BeforeFirst( ' ' );
  37. val_str.Replace( ',', '.' );
  38. // compare string values
  39. if( val_str == cvalue )
  40. {
  41. success = true;
  42. break;
  43. }
  44. aIdx++;
  45. }
  46. // Due to multiple ways to write a double, if string values
  47. // do not match, compare double values
  48. if( !success )
  49. {
  50. struct lconv* lc = localeconv();
  51. char localeDecimalSeparator = *lc->decimal_point;
  52. if( localeDecimalSeparator == ',' )
  53. cvalue.Replace( '.', ',' );
  54. double curr_value;
  55. cvalue.ToDouble( &curr_value );
  56. aIdx = 0;
  57. for( wxString& text : aList )
  58. {
  59. if( text.IsEmpty() ) // No match found: select the empty line choice
  60. break;
  61. double val;
  62. wxString val_str = text.BeforeFirst( ' ' );
  63. if( localeDecimalSeparator == ',' )
  64. val_str.Replace( '.', ',' );
  65. val_str.ToDouble( &val );;
  66. if( curr_value == val )
  67. {
  68. success = true;
  69. break;
  70. }
  71. aIdx++;
  72. }
  73. }
  74. return success;
  75. }