From 471bfb1131389b4fd1535e7e1a7738357e16bb8c Mon Sep 17 00:00:00 2001 From: Alex Shvartzkop Date: Thu, 5 Oct 2023 10:52:23 +0300 Subject: [PATCH] Add EDA_UNIT_UTILS::ParseInternalUnits --- common/eda_units.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- include/eda_units.h | 28 +++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/common/eda_units.cpp b/common/eda_units.cpp index 4d4af43755..b6dcb0dfa7 100644 --- a/common/eda_units.cpp +++ b/common/eda_units.cpp @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -25,6 +25,7 @@ #include #include // for KiROUND #include +#include bool EDA_UNIT_UTILS::IsImperialUnit( EDA_UNITS aUnit ) { @@ -180,6 +181,44 @@ std::string EDA_UNIT_UTILS::FormatInternalUnits( const EDA_IU_SCALE& aIuScale, } +bool EDA_UNIT_UTILS::ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale, + int& aOut ) +{ + double value; + + if( std::from_chars( aInput.data(), aInput.data() + aInput.size(), value ).ec != std::errc() ) + return false; + + aOut = value * aIuScale.IU_PER_MM; + return true; +} + + +bool EDA_UNIT_UTILS::ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale, + VECTOR2I& aOut ) +{ + size_t pos = aInput.find( ' ' ); + + if( pos == std::string::npos ) + return false; + + std::string first = aInput.substr( 0, pos ); + std::string second = aInput.substr( pos + 1 ); + + VECTOR2I vec; + + if( !ParseInternalUnits( first, aIuScale, vec.x ) ) + return false; + + if( !ParseInternalUnits( second, aIuScale, vec.y ) ) + return false; + + aOut = vec; + + return true; +} + + #define IU_TO_MM( x, scale ) ( x / scale.IU_PER_MM ) #define IU_TO_IN( x, scale ) ( x / scale.IU_PER_MILS / 1000 ) #define IU_TO_MILS( x, scale ) ( x / scale.IU_PER_MILS ) diff --git a/include/eda_units.h b/include/eda_units.h index 0473d7fdaa..abced634ae 100644 --- a/include/eda_units.h +++ b/include/eda_units.h @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -122,6 +122,32 @@ namespace EDA_UNIT_UTILS KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, const VECTOR2I& aPoint ); + /** + * Converts \a aInput string to internal units when reading from a file. + * + * This should only be used for reading from files as it ignores locale + * + * @param aInput is std::string to parse. + * @param aIuScale is the scale to use. + * @param aOut is the output reference. + * @return true if the parsing was successful. + */ + KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale, + int& aOut ); + + /** + * Converts \a aInput string to internal units vector when reading from a file. + * + * This should only be used for reading from files as it ignores locale + * + * @param aInput is std::string to parse. + * @param aIuScale is the scale to use. + * @param aOut is the output reference vector. + * @return true if the parsing was successful. + */ + KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale, + VECTOR2I& aOut ); + constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils ) { double x = mils * aIuScale.IU_PER_MILS;