Exemple #1
0
        // columns by name
        public Boolean ReadXY <T>(String[] filenames, ref T[] dataX, ref T[] dataY, ref String fileheader, String colXname, String colYname)
        {
            if (filenames == null || (filenames.Length == 0))
            {
                filenames = ValidManyFiles(null);
            }
            else if (filenames.Length == 1)
            {
                filenames = ValidManyFiles(filenames[0]);
            }

            if (filenames == null || (filenames.Length == 0))
            {
                return(false);
            }

            ProgressForm sa_pf = new ProgressForm(filenames);

            if (saShowProgress)
            {
                sa_pf.Show();
                sa_pf.Count = 0;
            }
            T[][,] tmpData;
            String[] strData;

            Int32 i = 0;

            try
            {
                TryParseMethod <T> TTryParse = FindTryParse <T>();
                ParseMethod <T>    TParse    = FindParse <T>();

                Int32 firstData; Int32 lastData;

                strData = File.ReadAllLines(filenames[0]);
                tmpData = new T[filenames.Length][, ];

                fileheader = ReadHeaderAndFooter(strData, 1, out firstData, out lastData);
                String[] tmpStr = fileheader.Split(saDelimeters, StringSplitOptions.RemoveEmptyEntries);
                Int32    colX   = Array.IndexOf <String>(tmpStr, colXname) + 1;
                if (colX == 0)
                {
                    throw new ArgumentException("Column \"" + colXname + "\" not found");
                }
                Int32 colY = Array.IndexOf <String>(tmpStr, colYname) + 1;
                if (colY == 0)
                {
                    throw new ArgumentException("Column \"" + colYname + "\" not found");
                }

                tmpData[0] = ReadDataMany <T>(ref strData, new Int32[] { colX, colY }, TParse, firstData, lastData);

                Int32[] recordlengths = new Int32[filenames.Length];
                recordlengths[0] = lastData - firstData + 1;
                Int32 totallength = lastData - firstData + 1;

                for (i = 1; i < filenames.Length; i++)
                {
                    sa_pf.Count = i;
                    strData     = File.ReadAllLines(filenames[i]);
                    ReadHeaderAndFooter(strData, colY, out firstData, out lastData);
                    tmpData[i]       = ReadDataMany <T>(ref strData, new Int32[] { colX, colY }, TParse, firstData, lastData);
                    recordlengths[i] = lastData - firstData + 1;
                    totallength     += lastData - firstData + 1;
                }

                dataX = new T[totallength];
                dataY = new T[totallength];
                Int32 j = 0;

                for (i = 0; i < filenames.Length; i++)
                {
                    for (Int32 k = 0; k < tmpData[i].GetLength(0); k++)
                    {
                        dataX[k + j] = tmpData[i][k, 0];
                    }
                    for (Int32 k = 0; k < tmpData[i].GetLength(0); k++)
                    {
                        dataY[k + j] = tmpData[i][k, 1];
                    }
                    j += recordlengths[i];
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error in " + this.ToString() + ":\n" + e.Message + "\nIn file: " + filenames[i],
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            finally
            {
                if (saShowProgress)
                {
                    sa_pf.Close();
                }
                sa_pf.Dispose();
            }
            return(true);
        }
Exemple #2
0
        // column by number
        public Boolean ReadY <T>(String[] filenames, ref T[] dataY, ref String fileheader, Int32 colY)
        {
            if (filenames == null || (filenames.Length == 0))
            {
                filenames = ValidManyFiles(null);
            }
            else if (filenames.Length == 1)
            {
                filenames = ValidManyFiles(filenames[0]);
            }

            if (filenames == null || (filenames.Length == 0))
            {
                return(false);
            }

            ProgressForm sa_pf = new ProgressForm(filenames);

            if (saShowProgress)
            {
                sa_pf.Show();
                sa_pf.Count = 0;
            }
            T[][]    tmpData;
            String[] strData;

            Int32 i = 0;

            try
            {
                TryParseMethod <T> TTryParse = FindTryParse <T>();
                ParseMethod <T>    TParse    = FindParse <T>();

                Int32 firstData; Int32 lastData;

                strData = File.ReadAllLines(filenames[0]);
                tmpData = new T[filenames.Length][];

                fileheader = ReadHeaderAndFooter(strData, colY, out firstData, out lastData);
                tmpData[0] = ReadDataOne <T>(ref strData, colY, TParse, firstData, lastData);

                Int32[] recordlengths = new Int32[filenames.Length];
                recordlengths[0] = lastData - firstData + 1;
                Int32 totallength = lastData - firstData + 1;

                for (i = 1; i < filenames.Length; i++)
                {
                    sa_pf.Count = i;
                    strData     = File.ReadAllLines(filenames[i]);
                    ReadHeaderAndFooter(strData, colY, out firstData, out lastData);
                    tmpData[i]       = ReadDataOne <T>(ref strData, colY, TParse, firstData, lastData);
                    recordlengths[i] = lastData - firstData + 1;
                    totallength     += lastData - firstData + 1;
                }

                dataY = new T[totallength];
                Int32 j = 0;
                for (i = 0; i < filenames.Length; i++)
                {
                    Array.Copy(tmpData[i], 0, dataY, j, recordlengths[i]);
                    j += recordlengths[i];
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error in " + this.ToString() + ":\n" + e.Message + "\nIn file: " + filenames[i],
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            finally
            {
                if (saShowProgress)
                {
                    sa_pf.Close();
                }
                sa_pf.Dispose();
            }
            return(true);
        }