[chronojump] Safest RenderN, RenderNAnalyze string to int conversion



commit 9e617745750253ef5ff97e1ba8230476b85f120a
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Dec 16 17:06:01 2015 +0100

    Safest RenderN, RenderNAnalyze string to int conversion

 src/encoder.cs              |   14 ++++++++++++++
 src/gui/encoderTreeviews.cs |   16 ++++++++++++++++
 src/util.cs                 |    2 ++
 3 files changed, 32 insertions(+), 0 deletions(-)
---
diff --git a/src/encoder.cs b/src/encoder.cs
index 959a504..0ac8ad2 100644
--- a/src/encoder.cs
+++ b/src/encoder.cs
@@ -295,6 +295,20 @@ public class EncoderCurve
                this.MaxForceT = maxForceT;
        }
 
+       //http://stackoverflow.com/questions/894263/how-to-identify-if-a-string-is-a-number
+       //this does not check if decimal point is a different character (eg '.' or ',')
+       public bool IsNumberN() {
+               int num;
+               return int.TryParse(N, out num);
+       }
+       //at least for RenderNAnalyze
+       public bool IsValidN() {
+               if (N == "AVG" || N == "SD" || IsNumberN())
+                       return true;
+               return false;
+       }
+
+
        public string ToCSV(string decimalSeparator) {
                //latin:        2,3 ; 2,5
                //non-latin:    2.3 , 2.5
diff --git a/src/gui/encoderTreeviews.cs b/src/gui/encoderTreeviews.cs
index 361ad3d..e5c48f5 100644
--- a/src/gui/encoderTreeviews.cs
+++ b/src/gui/encoderTreeviews.cs
@@ -863,6 +863,15 @@ public partial class ChronoJumpWindow
                        ecconLast = findEccon(false);
 
                EncoderCurve curve = (EncoderCurve) model.GetValue (iter, 0);
+       
+               //Check if it's number
+               if(! curve.IsNumberN()) {
+                       (cell as Gtk.CellRendererText).Text = "";
+                       LogB.Error("Curve is not number at RenderN:" + curve.ToCSV("COMMA"));
+                       return;
+               }
+               
+
                if(ecconLast == "c")
                        (cell as Gtk.CellRendererText).Text = 
                                String.Format(UtilGtk.TVNumPrint(curve.N,1,0),Convert.ToInt32(curve.N));
@@ -895,6 +904,13 @@ public partial class ChronoJumpWindow
        private void RenderNAnalyze (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, 
Gtk.TreeIter iter)
        {
                EncoderCurve curve = (EncoderCurve) model.GetValue (iter, 0);
+
+               //Check if it's valid
+               if(! curve.IsValidN()) {
+                       (cell as Gtk.CellRendererText).Text = "";
+                       LogB.Error("Curve is not valid at RenderNAnalyze:" + curve.ToCSV("COMMA"));
+                       return;
+               }
                        
                if(curve.N == "AVG" || curve.N == "SD") {
                        (cell as Gtk.CellRendererText).Markup = "<b>" + Catalog.GetString(curve.N) + "</b>";
diff --git a/src/util.cs b/src/util.cs
index 33365d9..a7becef 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -728,6 +728,8 @@ public class Util
        //ATTENTTION ONLY WORKS FOR POSITIVES
        //before changing this method, better create another method for all numbers, 
        //and call that method on possible negative numbers
+       //
+       //On EncoderCurve class there's the method IsNumber that's better, but does not check if the decimal 
point is a different character
        public static bool IsNumber(string myString, bool canBeDecimal) {
                System.Globalization.NumberFormatInfo localeInfo = new 
System.Globalization.NumberFormatInfo();
                localeInfo = System.Globalization.NumberFormatInfo.CurrentInfo;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]