Esempio n. 1
0
        public override string ToString()
        {
            string _Divider = Divider == ' ' ? " " : (Divider == '-' ? " - " : Divider.ToString() + ' ');

            if (isStart())
            {
                return("START " + Current);
            }
            else if (isEnd())
            {
                return(PrePrevious + ' ' + Previous + _Divider + "END");
            }
            else if (PrePrevious == null)
            {
                return("START " + Previous + _Divider + Current);
            }
            else
            {
                return(PrePrevious + ' ' + Previous + _Divider + Current);
            }
        }
Esempio n. 2
0
        //файлы на соответствие формату проверяются хреново
        //если пользователь подсунул программе URL картинок с котиками
        //то он сам дурак
        public void LoadToDataset()
        {
            if (Loading != null)
            {
                Loading();
            }
            dsTorData.Clear();
            dsTorData.Tables.Clear();
            FileStream        fs = null;
            StreamReader      sr = null;
            CSVErrorEventArgs e  = new CSVErrorEventArgs();
            int stringCount      = 0;

            try
            {
                fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                sr = new StreamReader(fs, Encoding.GetEncoding(1251));
            }
            catch (Exception ex)
            {
                ErrorMessage   = ex.Message;
                e.ErrorMessage = ex.Message;
                if (CSVError != null)
                {
                    CSVError(e);
                }
                if (sr != null)
                {
                    sr.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                return;
            }

            string buf = "";

            string[] splitbuf = null;
            bool     fields   = true;
            int      ColCount = 0;

            while (buf != null) //читаем построчно и анализируем
            {
                buf = sr.ReadLine();
                stringCount++;
                if (buf == null)
                {
                    break;        //прерываем чтение
                }
                buf = buf.Trim(); //удаляем граничные пробелы (а то бывало)
                if (buf == string.Empty)
                {
                    continue;                      //пустые строки пропускаем
                }
                if (buf.EndsWith(Divider.ToString()))
                {
                    buf = buf.Substring(0, buf.Length - 1);                                   //обрезаем последний разделитель, если он есть
                }
                //если досюда не вылетели, значит читаем первую строку
                //в ней должны быть заголовки полей
                if (fields)
                {
                    if (!CreateDataTable(buf))
                    {
                        e.ErrorMessage = ErrorMessage;
                        if (CSVError != null)
                        {
                            CSVError(e);
                        }
                        if (sr != null)
                        {
                            sr.Close();
                        }
                        if (fs != null)
                        {
                            fs.Close();
                        }
                        return;
                    }
                    fields = false;
                    //тут сохраняем количество столбцов (определяется по заголовку)
                    ColCount = dsTorData.Tables[TableName].Columns.Count;
                    continue;
                }

                splitbuf = buf.Split(Divider);   //делим строку на составляющие
                if (splitbuf.Length == ColCount) //еще небольшая проверка формата
                {
                    for (int i = 0; i < splitbuf.Length; i++)
                    {
                        Type t = dsTorData.Tables[TableName].Columns[i].DataType;
                        if (t == typeof(bool))
                        {
                            splitbuf[i] = splitbuf[i].Replace(TrueValue, "true");
                            splitbuf[i] = splitbuf[i].Replace(FalseValue, "false");
                        }
                    }
                    dsTorData.Tables[TableName].Rows.Add(splitbuf);
                }
                else
                {
                    ErrorMessage = "Bad fields count in string "
                                   + stringCount.ToString();
                    e.ErrorMessage = ErrorMessage;
                    if (CSVError != null)
                    {
                        CSVError(e);
                    }
                    if (sr != null)
                    {
                        sr.Close();
                    }
                    if (fs != null)
                    {
                        fs.Close();
                    }
                    return;
                }
            }

            sr.Close();
            fs.Close();

            if (dsTorData.Tables[TableName].Rows.Count == 0)
            {
                ErrorMessage   = "No data in file!";
                e.ErrorMessage = ErrorMessage;
                if (CSVError != null)
                {
                    CSVError(e);
                }
                return;
            }

            GetTimestamp();
            if (OK != null)
            {
                OK();
            }
        }