Ejemplo n.º 1
0
        /// <summary>
        /// Loads records from csv file.
        /// </summary>
        /// <param name="stream">Current stream.</param>
        /// <returns>Count loaded records.</returns>
        internal int LoadFromCsv(StreamReader stream)
        {
            FileCabinetRecordCsvReader reader = new FileCabinetRecordCsvReader(stream);

            this.records = reader.ReadAll().ToArray();
            return(this.records.Length);
        }
        /// <summary>
        /// Loads the records from csv file.
        /// </summary>
        /// <param name="reader">Csv reader.</param>
        /// <returns>List of imported records.</returns>
        public IList <FileCabinetRecord> LoadFromCsv(StreamReader reader)
        {
            FileCabinetRecordCsvReader csvReader = new FileCabinetRecordCsvReader(reader);
            IList <FileCabinetRecord>  records   = csvReader.ReadAll();

            return(records);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads from the CSV file.
        /// </summary>
        /// <param name="reader">Stream to read.</param>
        /// <returns>Count of records.</returns>
        public int LoadFromCsv(StreamReader reader)
        {
            var csvReader = new FileCabinetRecordCsvReader(reader);

            this.records = csvReader.ReadAll();

            return(this.records.Count);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Метод выгрузки из csv.
        /// </summary>
        /// <param name="fs">Filestream.</param>
        public void LoadFromCsv(FileStream fs)
        {
            StreamReader reader = new StreamReader(fs);
            FileCabinetRecordCsvReader csvReader = new FileCabinetRecordCsvReader(reader);
            List <FileCabinetRecord>   list      = new List <FileCabinetRecord>(csvReader.ReadAll());

            this.records = list.ToArray();
        }
        /// <summary>
        /// Load data from csv file.
        /// </summary>
        /// <param name="filestream">Input filestream.</param>
        public void LoadFromCsv(FileStream filestream)
        {
            if (filestream == null)
            {
                throw new ArgumentNullException(nameof(filestream));
            }

            using (StreamReader streamReader = new StreamReader(filestream.Name, Encoding.ASCII))
            {
                this.fileCabinetRecordCsvReader = new FileCabinetRecordCsvReader(streamReader);
                this.ListFromFile = new ReadOnlyCollection<FileCabinetRecord>(this.fileCabinetRecordCsvReader.ReadAll());
            }
        }
Ejemplo n.º 6
0
        private void ImportFromFormat(string format, string path)
        {
            string message = string.Empty;

            if (!File.Exists(path))
            {
                message = $"Import error: file {path} does not exist.";
            }

            FileStream fileStream = default;

            try
            {
                fileStream = new FileStream(path, FileMode.Open);
            }
            catch (UnauthorizedAccessException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine($"Export failed: can't open file {path}.");
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }

            if (fileStream != null)
            {
                using var stream = new StreamReader(fileStream);
                var snapshot = new FileCabinetServiceSnapshot(Array.Empty <FileCabinetRecord>());

                if (format.Equals(CsvString, StringComparison.InvariantCultureIgnoreCase))
                {
                    using var reader = new FileCabinetRecordCsvReader(stream);
                    message          = this.LoadFrom(reader, path, snapshot);
                }

                if (format.Equals(XmlString, StringComparison.InvariantCultureIgnoreCase))
                {
                    using var reader = new FileCabinetRecordXmlReader(stream);
                    message          = this.LoadFrom(reader, path, snapshot);
                }
            }

            Console.WriteLine(message);
        }
Ejemplo n.º 7
0
        /// <summary>Loads records from CSV file.</summary>
        /// <param name="reader">Reader.</param>
        /// <param name="validator">Validator.</param>
        /// <exception cref="ArgumentNullException">Thrown when reader or validator is null.</exception>
        public void LoadFromCsv(StreamReader reader, IRecordValidator validator)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (validator == null)
            {
                throw new ArgumentNullException(nameof(validator));
            }

            var csvReader = new FileCabinetRecordCsvReader(reader);
            IList <FileCabinetRecord> records;

            try
            {
                records = csvReader.ReadAll();
            }
            catch (FormatException)
            {
                throw;
            }
            catch (IndexOutOfRangeException)
            {
                throw;
            }

            UnverifiedData unverifiedData;

            foreach (var record in records)
            {
                unverifiedData = new UnverifiedData(record);
                try
                {
                    validator.ValidateParameters(unverifiedData);
                }
                catch (ArgumentException ex)
                {
                    this.invalidRecords.Add(new Tuple <FileCabinetRecord, string>(record, ex.Message));
                    continue;
                }

                this.records.Add(record);
            }
        }
        /// <summary>Loads the snapshot from CSV file.</summary>
        /// <param name="sr">The stream for a file.</param>
        public void LoadFromCsv(StreamReader sr)
        {
            int overwrittenElements        = 0;
            int lastElementIndex           = this.records.Length - 1;
            IList <FileCabinetRecord> list = new FileCabinetRecordCsvReader(sr).ReadAll();

            Array.Resize(ref this.records, this.records.Length + list.Count);
            foreach (var element in list)
            {
                if (lastElementIndex < 0)
                {
                    this.records[lastElementIndex + 1] = element;
                    lastElementIndex++;
                    continue;
                }

                for (int i = 0; i <= lastElementIndex; i++)
                {
                    if (this.records[i].Id == element.Id)
                    {
                        this.records[i] = element;
                        overwrittenElements++;
                        break;
                    }

                    if (i == lastElementIndex)
                    {
                        this.records[lastElementIndex + 1] = element;
                        lastElementIndex++;
                        break;
                    }
                }
            }

            Array.Resize(ref this.records, this.records.Length - overwrittenElements);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// This Methods load from file in csv-format.
        /// </summary>
        /// <param name="streamReader">Represents characters read.</param>
        /// <returns>List records.</returns>
        public IList <FileCabinetRecord> LoadFromCsv(StreamReader streamReader)
        {
            FileCabinetRecordCsvReader csvReader = new FileCabinetRecordCsvReader(streamReader);

            return(csvReader.ReadAll());
        }
        /// <summary>
        /// Load records from CSV file.
        /// </summary>
        /// <param name="reader">StreamReader.</param>
        public void LoadFromCsv(StreamReader reader)
        {
            FileCabinetRecordCsvReader fileCabinetRecordCsvReader = new FileCabinetRecordCsvReader(reader);

            this.Records = fileCabinetRecordCsvReader.ReadAll();
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Load records from csv file.
        /// </summary>
        /// <param name="reader">Stream Reader.</param>
        public void LoadFromCsv(StreamReader reader)
        {
            FileCabinetRecordCsvReader csvReader = new FileCabinetRecordCsvReader(reader);

            this.records = csvReader.ReadAll().ToArray();
        }