[chronojump] Safer EncoderCaptureStringR as List<string>



commit 9f6c497c56fbc7c7ca3baad0a612e37ce1471792
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Mar 5 14:40:29 2015 +0100

    Safer EncoderCaptureStringR as List<string>

 src/gui/chronojump.cs       |    2 +-
 src/gui/encoder.cs          |   43 +++++++++++----------------
 src/gui/encoderTreeviews.cs |   66 ++++++++++++++++++++++--------------------
 src/logB.cs                 |    5 ++-
 src/util.cs                 |   20 +++++++++++++
 src/utilEncoder.cs          |   10 ++++++
 6 files changed, 88 insertions(+), 58 deletions(-)
---
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index aeb3b42..e987890 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -6035,7 +6035,7 @@ LogB.Debug("X");
                //this avoids misbehaviour when bell is pressed and there's no data in treeview
                EncoderCurve curve = treeviewEncoderCaptureCurvesGetCurve(1, false);
                if(curve.N != null) {
-                       string contents = Util.ReadFile(UtilEncoder.GetEncoderCurvesTempFileName(), false);
+                       List<string> contents = 
Util.ReadFileAsStringList(UtilEncoder.GetEncoderCurvesTempFileName());
                        encoderUpdateTreeViewCapture(contents); //this updates encoderCaptureCurves
                        
                        findAndMarkSavedCurves();
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 9bc6776..da10061 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -25,6 +25,7 @@ using Gtk;
 using Gdk;
 using Glade;
 using System.Collections;
+using System.Collections.Generic; //List<T>
 using System.Threading;
 using Mono.Unix;
 using System.Linq;
@@ -682,9 +683,9 @@ public partial class ChronoJumpWindow
 
 
 
-       private void encoderUpdateTreeViewCapture(string contents)
+       private void encoderUpdateTreeViewCapture(List<string> contents)
        {
-               if (contents == null || contents == "") {
+               if (contents == null || contents.Count == 0) {
                        encoderButtonsSensitive(encoderSensEnum.DONENOSIGNAL);
                } else {
                        treeviewEncoderCaptureRemoveColumns();
@@ -3947,7 +3948,7 @@ public partial class ChronoJumpWindow
                encoderCapturePointsPainted = encoderCapturePointsCaptured;
        }
 
-       static string encoderCaptureStringR;
+       static List<string> encoderCaptureStringR;
        static ArrayList captureCurvesBarsData;
        static bool updatingEncoderCaptureGraphRCalc;
        
@@ -4228,14 +4229,14 @@ public partial class ChronoJumpWindow
                                                "meanPower: {4}\npeakPower: {5}\npeakPowerT: {6}", 
                                                height, meanSpeed, maxSpeed, speedT1, meanPower, peakPower, 
peakPowerT));
                        
-                       encoderCaptureStringR += 
string.Format("\n{0},2,a,3,4,{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},7",
+                       
encoderCaptureStringR.Add(string.Format("\n{0},2,a,3,4,{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},7",
                                        ecca.curvesAccepted +1,
                                        ecc.startFrame, ecc.endFrame-ecc.startFrame,
                                        Util.ConvertToPoint(height*10), //cm    
                                        Util.ConvertToPoint(meanSpeed), Util.ConvertToPoint(maxSpeed), 
speedT1,
                                        Util.ConvertToPoint(meanPower), Util.ConvertToPoint(peakPower), 
                                        Util.ConvertToPoint(peakPowerT*1000), Util.ConvertToPoint(peakPower / 
peakPowerT) 
-                                       );
+                                       ));
                
                        treeviewEncoderCaptureRemoveColumns();
                        ecca.curvesAccepted = createTreeViewEncoderCapture(encoderCaptureStringR);
@@ -4688,11 +4689,12 @@ public partial class ChronoJumpWindow
 
                                        //remove treeview columns
                                        treeviewEncoderCaptureRemoveColumns();
-                                       encoderCaptureStringR = 
+                                       encoderCaptureStringR = new List<string>();
+                                       encoderCaptureStringR.Add(
                                                ",series,exercise,mass,start,width,height," + 
                                                "meanSpeed,maxSpeed,maxSpeedT," +
                                                "meanPower,peakPower,peakPowerT,pp_ppt," +
-                                               "meanForce, maxForce, maxForceT";
+                                               "meanForce, maxForce, maxForceT");
 
                                        capturingCsharp = encoderCaptureProcess.CAPTURING;
 
@@ -5026,7 +5028,6 @@ LogB.Debug("D");
        {
                if (!String.IsNullOrEmpty(curveFromR.Data))
                {
-
                        LogB.Information("Without trim");
                        LogB.Information(curveFromR.Data);
 
@@ -5037,35 +5038,27 @@ LogB.Debug("D");
                        //fix if data couldn't be calculated from R
                        trimmed = trimmed.Replace("NA","0");
 
-                       /*      
-                       encoderCaptureStringR += 
string.Format("\n{0},2,a,3,4,{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},7",
-                                       ecca.curvesAccepted +1,
-                                       ecc.startFrame, ecc.endFrame-ecc.startFrame,
-                                       Util.ConvertToPoint(height*10), //cm    
-                                       Util.ConvertToPoint(meanSpeed), Util.ConvertToPoint(maxSpeed), 
speedT1,
-                                       Util.ConvertToPoint(meanPower), Util.ConvertToPoint(peakPower), 
-                                       Util.ConvertToPoint(peakPowerT*1000), Util.ConvertToPoint(peakPower / 
peakPowerT) 
-                                       */
-                       
                        string [] strs = trimmed.Split(new char[] {','});
 
-                       encoderCaptureStringR += 
string.Format("\n{0},2,a,3,4,{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}",
+                       
encoderCaptureStringR.Add(string.Format("\n{0},2,a,3,4,{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}",
                                        0,
                                        0, strs[0],                     //start, width
                                        strs[1],                        //height
                                        strs[2], strs[3], strs[4],      //speeds
                                        strs[5], strs[6], strs[7],      //powers
                                        strs[8],                        //pp/ppt
-                                       strs[9], strs[10], strs[11]);   //forces
+                                       strs[9], strs[10], strs[11]));  //forces
                        
-                       LogB.Debug("encoderCaptureStringR");
-                       LogB.Debug(encoderCaptureStringR);
+                       //LogB.Debug("encoderCaptureStringR");
+                       //LogB.Debug(encoderCaptureStringR);
 
                        double meanSpeed = Convert.ToDouble(Util.ChangeDecimalSeparator(strs[2]));
                        double maxSpeed = Convert.ToDouble(Util.ChangeDecimalSeparator(strs[3]));
                        double meanPower = Convert.ToDouble(Util.ChangeDecimalSeparator(strs[5]));
                        double peakPower = Convert.ToDouble(Util.ChangeDecimalSeparator(strs[6]));
                        captureCurvesBarsData.Add(new EncoderBarsData(meanSpeed, maxSpeed, meanPower, 
peakPower));
+                       
+                       LogB.Information("activating needToRefreshTreeviewCapture");
 
                        //executed on GTK thread pulse method
                        needToRefreshTreeviewCapture = true;
@@ -5138,8 +5131,8 @@ LogB.Debug("D");
        
                                if(needToRefreshTreeviewCapture) 
                                {
-                                       LogB.Error("HERE YES");
-                                       LogB.Error(encoderCaptureStringR);
+                                       //LogB.Error("HERE YES");
+                                       //LogB.Error(encoderCaptureStringR);
                                        
                                        treeviewEncoderCaptureRemoveColumns();
                                        ecca.curvesAccepted = 
createTreeViewEncoderCapture(encoderCaptureStringR);
@@ -5370,7 +5363,7 @@ LogB.Debug("D");
                                if(notebook_encoder_capture.CurrentPage == 0)
                                        notebook_encoder_capture.NextPage();
 
-                               string contents = Util.ReadFile(UtilEncoder.GetEncoderCurvesTempFileName(), 
false);
+                               List<string> contents = 
Util.ReadFileAsStringList(UtilEncoder.GetEncoderCurvesTempFileName());
                                
                                Pixbuf pixbuf = new Pixbuf (UtilEncoder.GetEncoderGraphTempFileName()); 
//from a file
                                image_encoder_capture.Pixbuf = pixbuf;
diff --git a/src/gui/encoderTreeviews.cs b/src/gui/encoderTreeviews.cs
index 571af01..1ebe588 100644
--- a/src/gui/encoderTreeviews.cs
+++ b/src/gui/encoderTreeviews.cs
@@ -25,6 +25,7 @@ using Gtk;
 using Gdk;
 using Glade;
 using System.Collections;
+using System.Collections.Generic; //List<T>
 using Mono.Unix;
 
 
@@ -35,7 +36,8 @@ public partial class ChronoJumpWindow
 
        //returns curves num
        //capture has single and multiple selection in order to save curves... Analyze only shows data.
-       private int createTreeViewEncoderCapture(string contents) {
+       private int createTreeViewEncoderCapture(List<string> contents) 
+       {
                bool showStartAndDuration = encoderCaptureOptionsWin.check_show_start_and_duration.Active;
 
                string [] columnsString = {
@@ -57,37 +59,39 @@ public partial class ChronoJumpWindow
 
                encoderCaptureCurves = new ArrayList ();
 
-               string line;
                int curvesCount = 0;
-               using (StringReader reader = new StringReader (contents)) {
-                       line = reader.ReadLine ();      //headers
-                       LogB.Information(line);
-                       do {
-                               line = reader.ReadLine ();
-                               LogB.Information(line);
-                               if (line == null)
-                                       break;
-
-                               curvesCount ++;
+               bool headers = true;
+               foreach(string line in contents)
+               {
+                       LogB.Debug(line);
+                       if(headers) {
+                               headers = false;
+                               continue;
+                       }
 
-                               string [] cells = line.Split(new char[] {','});
-                               cells = fixDecimals(cells, true); //useForce
-
-                               encoderCaptureCurves.Add (new EncoderCurve (
-                                                       false,                          //user need to mark 
to save them
-                                                       cells[0],       //id 
-                                                       //cells[1],     //seriesName
-                                                       //cells[2],     //exerciseName
-                                                       //cells[3],     //massBody
-                                                       //cells[4],     //massExtra
-                                                       cells[5], cells[6], cells[7],   //start, duration, 
height 
-                                                       cells[8], cells[9], cells[10],  //meanSpeed, 
maxSpeed, maxSpeedT
-                                                       cells[11], cells[12], cells[13],//meanPower, 
peakPower, peakPowerT
-                                                       cells[14],                      //peakPower / 
peakPowerT
-                                                       cells[15], cells[16], cells[17] //meanForce, 
maxSForce maxForceT
-                                                       ));
+                       if (line == null)
+                               break;
+
+                       curvesCount ++;
+
+                       string [] cells = line.Split(new char[] {','});
+                       cells = fixDecimals(cells, true); //useForce
+                       LogB.Error(Util.StringArrayToString(cells, ":"));
+
+                       encoderCaptureCurves.Add (new EncoderCurve (
+                                               false,                          //user need to mark to save 
them
+                                               cells[0],       //id 
+                                               //cells[1],     //seriesName
+                                               //cells[2],     //exerciseName
+                                               //cells[3],     //massBody
+                                               //cells[4],     //massExtra
+                                               cells[5], cells[6], cells[7],   //start, duration, height 
+                                               cells[8], cells[9], cells[10],  //meanSpeed, maxSpeed, 
maxSpeedT
+                                               cells[11], cells[12], cells[13],//meanPower, peakPower, 
peakPowerT
+                                               cells[14],                      //peakPower / peakPowerT
+                                               cells[15], cells[16], cells[17] //meanForce, maxSForce 
maxForceT
+                                               ));
 
-                       } while(true);
                }
 
                encoderCaptureListStore = new Gtk.ListStore (typeof (EncoderCurve));
diff --git a/src/logB.cs b/src/logB.cs
index 61709e2..184eeab 100644
--- a/src/logB.cs
+++ b/src/logB.cs
@@ -173,7 +173,10 @@ public static class LogB
                        if(details != null) {
                                Console.WriteLine(" {0} - {1}", message, details);
                        } else {
-                               Console.WriteLine(" {0}", message);
+                               if(type == LogEntryType.Debug)
+                                       Console.Write(" {0}", message);
+                               else
+                                       Console.WriteLine(" {0}", message);
                        }
                }
 
diff --git a/src/util.cs b/src/util.cs
index 1424a9a..be74b0c 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -22,6 +22,7 @@ using System;
 //using System.Data;
 using System.Text; //StringBuilder
 using System.Collections; //ArrayList
+using System.Collections.Generic; //List<T>
 using System.Diagnostics;      //for detect OS
 using System.IO;               //for detect OS
 
@@ -1004,6 +1005,7 @@ public class Util
                return File.Exists(fileName);
        }
 
+       //not recommended, better use below method. Better for bigger files
        public static string ReadFile(string fileName, bool removeEOL)
        {
                try {
@@ -1020,6 +1022,24 @@ public class Util
                        return null;
                }
        }
+       //recommended method
+       public static List<string> ReadFileAsStringList(string fileName)
+       {
+               try {
+                       List<string> lines = new List<string>();
+                       using (var sr = new StreamReader(fileName))
+                       {
+                               while (sr.Peek() >= 0)
+                               {
+                                       lines.Add(sr.ReadLine());
+                               }
+                       }
+                       return(lines);
+               } catch {
+                       return null;
+               }
+       }
+
 
        //returns int [] of encoder signal or curve
        //currently used on db conversion 1.05 -> 1.06
diff --git a/src/utilEncoder.cs b/src/utilEncoder.cs
index a38eded..5bf5bd1 100644
--- a/src/utilEncoder.cs
+++ b/src/utilEncoder.cs
@@ -746,6 +746,16 @@ public class UtilEncoder
                int digitPre = -10000; //just an impossible mark
                int rep = 0;
                int countNewLine = 0;
+               
+               /*
+               LogB.Information("Compressing curve");
+               string debugStr = "";
+               for(int i=0; i < curve.Length; i++) {
+                       debugStr += curve[i].ToString();
+               }
+               LogB.Debug(debugStr);
+               */
+
                for(int i=0; i < curve.Length; i++) 
                {
                        digit = Convert.ToInt32(curve[i]);


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