Move some string formatting functions out of base_units

Keeping them in base_units means that we can't introduce
any dependence on these functions to anything that needs to
compile without one of the unit defines (EESCHEMA, PCBNEW, etc)
This commit is contained in:
Jon Evans 2021-04-14 23:20:36 -04:00
parent 268d570941
commit 433e148e08
11 changed files with 65 additions and 71 deletions

View File

@ -52,41 +52,6 @@
#error "Cannot resolve internal units due to no definition of EESCHEMA, CVPCB or PCBNEW."
#endif
// Helper function to print a float number without using scientific notation
// and no trailing 0
// So we cannot always just use the %g or the %f format to print a fp number
// this helper function uses the %f format when needed, or %g when %f is
// not well working and then removes trailing 0
std::string Double2Str( double aValue )
{
char buf[50];
int len;
if( aValue != 0.0 && fabs( aValue ) <= 0.0001 )
{
// For these small values, %f works fine,
// and %g gives an exponent
len = sprintf( buf, "%.16f", aValue );
while( --len > 0 && buf[len] == '0' )
buf[len] = '\0';
if( buf[len] == '.' )
buf[len] = '\0';
else
++len;
}
else
{
// For these values, %g works fine, and sometimes %f
// gives a bad value (try aValue = 1.222222222222, with %.16f format!)
len = sprintf( buf, "%.10g", aValue );
}
return std::string( buf, len );
}
double To_User_Unit( EDA_UNITS aUnit, double aValue )
{
@ -442,20 +407,6 @@ long long int ValueFromString( EDA_UNITS aUnits, const wxString& aTextValue, EDA
}
/**
* A helper to convert \a aAngle in deci-degrees to a string in degrees.
*/
wxString AngleToStringDegrees( double aAngle )
{
wxString text;
text.Printf( wxT( "%.3f" ), aAngle / 10.0 );
StripTrailingZeros( text, 1 );
return text;
}
wxString GetAbbreviatedUnitsLabel( EDA_UNITS aUnit, EDA_DATA_TYPE aType )
{
switch( aUnit )

View File

@ -24,7 +24,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <eda_item.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <macros.h>
#include <drawing_sheet/ds_painter.h>
@ -33,7 +33,6 @@
#include <drawing_sheet/ds_data_model.h>
#include <math/vector2d.h>
#include <drawing_sheet/drawing_sheet_reader_lexer.h>
#include <convert_to_biu.h>
#include <wx/msgdlg.h>

View File

@ -17,7 +17,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <kicad_string.h>
#include <libeval/numeric_evaluator.h>
/* The (generated) lemon parser is written in C.

View File

@ -27,6 +27,7 @@
*/
#include <clocale>
#include <cmath>
#include <macros.h>
#include <richio.h> // StrPrintf
#include <kicad_string.h>
@ -866,3 +867,44 @@ void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
}
}
}
std::string Double2Str( double aValue )
{
char buf[50];
int len;
if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
{
// For these small values, %f works fine,
// and %g gives an exponent
len = sprintf( buf, "%.16f", aValue );
while( --len > 0 && buf[len] == '0' )
buf[len] = '\0';
if( buf[len] == '.' )
buf[len] = '\0';
else
++len;
}
else
{
// For these values, %g works fine, and sometimes %f
// gives a bad value (try aValue = 1.222222222222, with %.16f format!)
len = sprintf( buf, "%.10g", aValue );
}
return std::string( buf, len );
}
wxString AngleToStringDegrees( double aAngle )
{
wxString text;
text.Printf( wxT( "%.3f" ), aAngle / 10.0 );
StripTrailingZeros( text, 1 );
return text;
}

View File

@ -27,6 +27,7 @@
#include <export_to_pcbnew.h>
#include <confirm.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <macros.h>
#include <trigo.h>

View File

@ -64,16 +64,6 @@ inline int Mm2mils( double x ) { return KiROUND( x * 1000./25.4 ); }
/// Convert mils to mm.
inline int Mils2mm( double x ) { return KiROUND( x * 25.4 / 1000. ); }
/** Helper function Double2Str to print a float number without
* using scientific notation and no trailing 0
* We want to avoid scientific notation in S-expr files (not easy to read)
* for floating numbers.
* So we cannot always just use the %g or the %f format to print a fp number
* this helper function uses the %f format when needed, or %g when %f is
* not well working and then removes trailing 0
*/
std::string Double2Str( double aValue );
/**
* Function To_User_Unit
* convert \a aValue in internal units to the appropriate user units defined by \a aUnit.
@ -84,13 +74,6 @@ std::string Double2Str( double aValue );
*/
double To_User_Unit( EDA_UNITS aUnit, double aValue );
/**
* Function AngleToStringDegrees
* is a helper to convert the \a double \a aAngle (in internal unit)
* to a string in degrees
*/
wxString AngleToStringDegrees( double aAngle );
/**
* Function MessageTextFromValue
* is a helper to convert the \a double length \a aValue to a string in inches,

View File

@ -28,9 +28,9 @@
#include <core/typeinfo.h>
#include <math/vector2d.h>
#include <eda_text.h>
#include <bitmap_base.h>
#include "widgets/msgpanel.h"
#include <geometry/shape_poly_set.h>
#include <eda_item.h>
#include <eda_units.h>
#include <algorithm>

View File

@ -327,4 +327,20 @@ void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSpli
*/
void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
/**
* Prints a float number without using scientific notation and no trailing 0
* We want to avoid scientific notation in S-expr files (not easy to read)
* for floating numbers.
* So we cannot always just use the %g or the %f format to print a fp number
* this helper function uses the %f format when needed, or %g when %f is
* not well working and then removes trailing 0
*/
std::string Double2Str( double aValue );
/**
* A helper to convert the \a double \a aAngle (in internal unit)
* to a string in degrees
*/
wxString AngleToStringDegrees( double aAngle );
#endif // KICAD_STRING_H_

View File

@ -22,6 +22,7 @@
#include "class_board_stackup.h"
#include <convert_to_biu.h>
#include <base_units.h>
#include <kicad_string.h>
#include <layers_id_colors_and_visibility.h>
#include <board_design_settings.h>
#include <board.h>

View File

@ -22,7 +22,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <board_design_settings.h>
#include <kicad_string.h>
#include <panel_edit_options.h>
#include <pcb_edit_frame.h>
#include <pcb_painter.h>

View File

@ -32,6 +32,7 @@
#include <dimension.h>
#include <footprint.h>
#include <fp_shape.h>
#include <kicad_string.h>
#include <kiface_i.h>
#include <locale_io.h>
#include <macros.h>