Exemplo n.º 1
0
        public string LoadFromFile(string fileName, out string header)
        {
            string filePath  = null;
            int    numPoints = 0;

            header = "";
            try {
                String wrkDir = Shell.GetWorkingDir(Shell.CurrentShellId);
                if (String.IsNullOrWhiteSpace(wrkDir))
                {
                    wrkDir = Project.GetProjectDir();
                }
                filePath = Path.Combine(wrkDir, fileName);
            } catch (Exception) {
                return("Error: bad file name:" + filePath);
            }
            System.IO.StreamReader file = null;
            try {
                file = new System.IO.StreamReader(filePath);
            } catch (Exception) {
                return("Error: bad file name:" + filePath);
            }
            try {
                lock (PointList)
                {
                    for (; ;)
                    {
                        try {
                            string s = file.ReadLine();
                            if (s == null)
                            {
                                break;
                            }
                            if (s.StartsWith("#") && (s.Length > 1))
                            {
                                header = s.Substring(1);
                                continue;
                            }
                            string[] ary = s.Split(DataFileParameterSeparators);
                            if (ary.Length > 0)
                            {
                                List <double> a = new List <double>();
                                for (int k = 0; k < ary.Length; k++)
                                {
                                    double d = 0.0;
                                    if (String.IsNullOrWhiteSpace(ary[k]))
                                    {
                                        d = Double.NaN;
                                    }
                                    else
                                    {
                                        Double.TryParse(ary[k], out d);
                                    }
                                    a.Add(d);
                                }
                                ++numPoints;
                                if ((numPoints + 10) > CacheSize)
                                {
                                    CacheSize = numPoints + 10;
                                }
                                AddPoint(a);
                            }
                        } catch (Exception) {
                            break;
                        }
                    }
                }
            } catch (Exception) {
            }
            try {
                if (file != null)
                {
                    file.Close();
                }
            } catch (Exception) {
            }
            return(filePath);
        }
Exemplo n.º 2
0
        // Dump in text csv format.
        public string DumpToFile(string fileNamePath, bool zeroTimeBase, string header)
        {
            string filePath = null; double timeBase = 0.0; bool isStart = true;

            // fileNamePath may be a path.
            try {
                String wrkDir = Shell.GetWorkingDir(Shell.CurrentShellId);
                if (String.IsNullOrWhiteSpace(wrkDir))
                {
                    wrkDir = Project.GetProjectDir();
                }
                // This will take care of combinging a mixture of absolute & relative paths.
                filePath = Path.Combine(wrkDir, fileNamePath);
            } catch (Exception) {
                filePath = "tmpdc" + TmpCounter++;
            }
            System.IO.StreamWriter file = null;
            try {
                file = new System.IO.StreamWriter(filePath);
            } catch (Exception) {
                return("Error: bad file name:" + filePath);
            }
            try {
                lock (PointList)
                {
                    if (!String.IsNullOrWhiteSpace(header))
                    {
                        file.WriteLine('#' + header);
                    }
                    foreach (List <double> p in PointList)
                    {
                        String s       = "";
                        bool   isFirst = true;
                        if (isStart)
                        {
                            isStart  = false;
                            timeBase = p[0];
                        }
                        foreach (double r in p)
                        {
                            double v = r;
                            if (isFirst)
                            {
                                isFirst = false;
                                if (zeroTimeBase)
                                {
                                    v -= timeBase;
                                }
                            }
                            else
                            {
                                s += ",";
                            }
                            if (!Double.IsNaN(v))
                            {
                                s += v.ToString();
                            }
                        }
                        file.WriteLine(s);
                    }
                }
            } catch (Exception) {
            }
            try {
                if (file != null)
                {
                    file.Close();
                }
            } catch (Exception) {
            }
            return(filePath);
        }