From 433e148e0801d975c1d4fcefad6acdea18d4b1d8 Mon Sep 17 00:00:00 2001
From: Jon Evans <jon@craftyjon.com>
Date: Wed, 14 Apr 2021 23:20:36 -0400
Subject: [PATCH] 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)
---
 common/base_units.cpp                         | 49 -------------------
 common/drawing_sheet/ds_data_model_io.cpp     |  3 +-
 common/libeval/numeric_evaluator.cpp          |  2 +-
 common/string.cpp                             | 42 ++++++++++++++++
 gerbview/export_to_pcbnew.cpp                 |  1 +
 include/base_units.h                          | 17 -------
 include/drawing_sheet/ds_draw_item.h          |  2 +-
 include/kicad_string.h                        | 16 ++++++
 .../class_board_stackup.cpp                   |  1 +
 pcbnew/dialogs/panel_edit_options.cpp         |  2 +-
 pcbnew/plugins/kicad/kicad_plugin.cpp         |  1 +
 11 files changed, 65 insertions(+), 71 deletions(-)

diff --git a/common/base_units.cpp b/common/base_units.cpp
index 91ca6a3f6c..9ef529d06f 100644
--- a/common/base_units.cpp
+++ b/common/base_units.cpp
@@ -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 )
diff --git a/common/drawing_sheet/ds_data_model_io.cpp b/common/drawing_sheet/ds_data_model_io.cpp
index 0ce3da6674..acda9e9fce 100644
--- a/common/drawing_sheet/ds_data_model_io.cpp
+++ b/common/drawing_sheet/ds_data_model_io.cpp
@@ -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>
 
diff --git a/common/libeval/numeric_evaluator.cpp b/common/libeval/numeric_evaluator.cpp
index 6e44399651..c0d586f8bc 100644
--- a/common/libeval/numeric_evaluator.cpp
+++ b/common/libeval/numeric_evaluator.cpp
@@ -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.
diff --git a/common/string.cpp b/common/string.cpp
index 7b7a75249c..d61f60310b 100644
--- a/common/string.cpp
+++ b/common/string.cpp
@@ -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;
+}
diff --git a/gerbview/export_to_pcbnew.cpp b/gerbview/export_to_pcbnew.cpp
index 7fcaccaebd..71514ef7bc 100644
--- a/gerbview/export_to_pcbnew.cpp
+++ b/gerbview/export_to_pcbnew.cpp
@@ -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>
diff --git a/include/base_units.h b/include/base_units.h
index b325594693..97672df4c6 100644
--- a/include/base_units.h
+++ b/include/base_units.h
@@ -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,
diff --git a/include/drawing_sheet/ds_draw_item.h b/include/drawing_sheet/ds_draw_item.h
index 6d8133800e..88b26f8a7f 100644
--- a/include/drawing_sheet/ds_draw_item.h
+++ b/include/drawing_sheet/ds_draw_item.h
@@ -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>
diff --git a/include/kicad_string.h b/include/kicad_string.h
index a16b288c9f..e9cc9cac3e 100644
--- a/include/kicad_string.h
+++ b/include/kicad_string.h
@@ -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_
diff --git a/pcbnew/board_stackup_manager/class_board_stackup.cpp b/pcbnew/board_stackup_manager/class_board_stackup.cpp
index b586cb12c0..66aed3ed59 100644
--- a/pcbnew/board_stackup_manager/class_board_stackup.cpp
+++ b/pcbnew/board_stackup_manager/class_board_stackup.cpp
@@ -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>
diff --git a/pcbnew/dialogs/panel_edit_options.cpp b/pcbnew/dialogs/panel_edit_options.cpp
index 184f2d75f9..8e8850813f 100644
--- a/pcbnew/dialogs/panel_edit_options.cpp
+++ b/pcbnew/dialogs/panel_edit_options.cpp
@@ -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>
diff --git a/pcbnew/plugins/kicad/kicad_plugin.cpp b/pcbnew/plugins/kicad/kicad_plugin.cpp
index dbc6b4f0de..5467c6c2ea 100644
--- a/pcbnew/plugins/kicad/kicad_plugin.cpp
+++ b/pcbnew/plugins/kicad/kicad_plugin.cpp
@@ -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>