Example #1
0
        public static async Task <long> CountRowsAsync(CsvOptions options, CsvColumnFilter filter)
        {
            if (!options.IsValid())
            {
                return(0);
            }

            if (FileReader?.Path != options.FilePath)
            {
                FileReader = FileEnumerable.ReadOnly(options.FilePath, options.Encoding).EnableBuffer();
            }

            long rowCount;

            if (filter.IsValid(out string msg))
            {
                rowCount = await FileReader.CountAsync(x => x.Split(options.Delimiter.Character)[filter.Index]
                                                       .WithCondition(filter.Keyword, filter.Condition));
            }
            else
            {
                rowCount = await FileReader.CountAsync();
            }

            return(rowCount);
        }
Example #2
0
        public static async Task <List <string> > GetCsvRows(CsvOptions options, CsvColumnFilter filter, int page, int pageSize)
        {
            if (options.FilePath.IsNullOrEmpty())
            {
                throw new ArgumentException(@"File path is empty.");
            }

            if (FileReader?.Path != options.FilePath)
            {
                FileReader = FileEnumerable.ReadOnly(options.FilePath, options.Encoding).EnableBuffer();
            }


            var rowEnumerable = FileReader
                                .Reset()
                                .Skip((page - 1) * pageSize)
                                .Take(pageSize);

            if (!options.CommentSymbol.IsNullOrEmpty())
            {
                rowEnumerable = rowEnumerable.Where(x => !x.StartsWith(options.CommentSymbol));
            }

            if (!rowEnumerable.Any())
            {
                return(new List <string>());
            }

            List <string> rows;

            if (options.GetColumnNamesFromFirstRow && await rowEnumerable.HasMoreLinesThanAsync(1))
            {
                var header = rowEnumerable.First();

                if (filter?.IsValid(out var msg) ?? false)
                {
                    rowEnumerable = rowEnumerable
                                    .Where(x => x.Split(options.Delimiter.Character)[filter.Index].WithCondition(filter.Keyword, filter.Condition));
                }

                rows = await rowEnumerable.Skip(rowEnumerable.SkipAmount + 1).ToListAsync();

                rows.Insert(0, header);
                return(rows);
            }

            if (filter?.IsValid(out var msg2) ?? false)
            {
                rowEnumerable = rowEnumerable
                                .Where(x => x.Split(options.Delimiter.Character)[filter.Index].WithCondition(filter.Keyword, filter.Condition));
            }

            rows = await rowEnumerable.ToListAsync();

            return(rows);
        }
Example #3
0
        /// <summary>
        ///     The User wants to reset the filter.
        /// </summary>
        private async void ResetFilterButton_Click(object sender, EventArgs e)
        {
            KeywordTextBox.Text                   = "";
            ColumnDropDown.SelectedIndex          = 0;
            SearchConditionDropDown.SelectedIndex = 0;

            Filter = new CsvColumnFilter();

            await LoadCsvData();
        }
Example #4
0
        /// <summary>
        ///     Open a <see cref="FileDialog"/> from the tool strip menu item.
        /// </summary>
        private async void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StatusStripLabel.Text = Strings.SELECTING_FILE;
            var dialog = new OpenFileDialog
            {
                Filter           = $@"{Strings.CSV_FILE}|*.csv|{Strings.TXT_FILE}|*.txt",
                RestoreDirectory = true
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Page = 1;
                StatusStripLabel.Text = string.Empty;

                Filter = new CsvColumnFilter();
                KeywordTextBox.Text          = "";
                ColumnDropDown.SelectedIndex = -1;

                var delimiters = CsvOptions.Delimiters.Select(x => x.Character).ToList();
                var fileName   = dialog.FileName;
                var rows       = FileEnumerable
                                 .ReadOnly(fileName)
                                 .Where(x => !x.StartsWith("//") && !x.StartsWith("#") && !x.IsNullOrEmpty())
                                 .Take(10);

                try
                {
                    var delimiter = rows.FindDelimiter(CsvOptions.Quotes.Select(x => x.Character), CsvOptions.Delimiters.Select(x => x.Character));
                    var index     = delimiters.IndexOf(delimiter);
                    DelimiterDropDown.SelectedIndex = index;
                }
                catch
                {
                    StatusStripLabel.Text           = Strings.UNABLE_TO_READ_DELIMITER;
                    DelimiterDropDown.SelectedIndex = 0;
                }

                FileWatcher?.Dispose();

                var fileInfo = new FileInfo(fileName);
                FileWatcher                     = new FileSystemWatcher(fileInfo.DirectoryName);
                FileWatcher.Changed            += FileWatcherOnChanged;
                FileWatcher.EnableRaisingEvents = true;

                Options.FilePath = fileName;
                Text             = $@"CsvViewer - {Options.FilePath}";

                await LoadCsvData();
            }
        }
Example #5
0
        public static async Task SaveFile(CsvOptions options, CsvColumnFilter filter, string path)
        {
            if (FileReader == null)
            {
                throw new InvalidOperationException(@"The file reader is expected to be initialized beforehand.");
            }

            var rowEnumerable = FileReader.Reset();

            List <string> rows;

            if (options.GetColumnNamesFromFirstRow && await rowEnumerable.HasMoreLinesThanAsync(1))
            {
                var header = rowEnumerable.First();

                if (filter?.IsValid(out var msg) ?? false)
                {
                    rowEnumerable = rowEnumerable
                                    .Where(x => x.Split(options.Delimiter.Character)[filter.Index]
                                           .WithCondition(filter.Keyword, filter.Condition));
                }

                rows = await rowEnumerable.Skip(rowEnumerable.SkipAmount + 1).ToListAsync();

                rows.Insert(0, header);
            }
            else
            {
                if (filter?.IsValid(out var msg2) ?? false)
                {
                    rowEnumerable = rowEnumerable
                                    .Where(x => x.Split(options.Delimiter.Character)[filter.Index].WithCondition(filter.Keyword, filter.Condition));
                }

                rows = await rowEnumerable.ToListAsync();
            }

            File.WriteAllLines(path, rows, options.Encoding);
        }