public async Task <long> WriteAsync([CanBeNull] IFileReader reader, CancellationToken token) { if (reader == null) { return(-1); } HandleWriteStart(); m_SetMaxProcess?.Invoke(-1); try { var sourceAccess = new SourceAccess(m_FullPath, false, recipient: m_Recipient, keepEnencyrpted: m_KeepUnencrypted); if (!string.IsNullOrEmpty(m_IdentifierInContainer)) { sourceAccess.IdentifierInContainer = m_IdentifierInContainer; } using (var improvedStream = FunctionalDI.OpenStream(sourceAccess)) await WriteReaderAsync(reader, improvedStream as Stream, token).ConfigureAwait(false); } catch (Exception exc) { Logger.Error(exc, "Could not write file {filename}", FileSystemUtils.GetShortDisplayFileName(m_FullPath)); throw new FileWriterException($"Could not write file '{FileSystemUtils.GetShortDisplayFileName(m_FullPath)}'\n{exc.SourceExceptionMessage()}", exc); } finally { Logger.Debug("Finished writing {filesetting} Records: {records}", m_FileSettingDisplay, Records); WriteFinished?.Invoke(this, null); } return(Records); }
/// <summary> /// Resets the position and buffer to the first line, excluding headers, use /// ResetPositionToStart if you want to go to first data line /// </summary> private void ResetPositionToStartOrOpen() { if (SelfOpenedStream) { m_ImprovedStream?.Dispose(); m_ImprovedStream = FunctionalDI.OpenStream(new SourceAccess(FullPath)); } else { m_ImprovedStream.Seek(0, SeekOrigin.Begin); } // in case we can not seek need to reopen the stream reader m_StreamReader?.Close(); m_StreamReader = new StreamReader(m_ImprovedStream as Stream ?? throw new InvalidOperationException(), Encoding.UTF8, true, 4096, true); // End Line should be at 1, later on as the line is read the start line s set to this value StartLineNumber = 1; EndLineNumber = 1; RecordNumber = 0; EndOfFile = m_StreamReader.EndOfStream; m_JsonTextReader?.Close(); m_JsonTextReader = new JsonTextReader(m_StreamReader) { SupportMultipleContent = true }; }
private async void GuessNewline_Click(object sender, EventArgs e) { await buttonNewLine.RunWithHourglassAsync(async() => { using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings))) cboRecordDelimiter.SelectedValue = (int)await improvedStream.GuessNewline(m_ViewSettings.CodePageId, m_ViewSettings.SkipRows, m_ViewSettings.FileFormat.FieldQualifier, m_CancellationTokenSource.Token); }); }
private async void ButtonSkipLine_ClickAsync(object sender, EventArgs e) { await buttonSkipLine.RunWithHourglassAsync(async() => { using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings))) m_ViewSettings.SkipRows = await improvedStream.GuessStartRow(m_ViewSettings.CodePageId, m_ViewSettings.FileFormat.FieldDelimiter, m_ViewSettings.FileFormat.FieldQualifier, m_ViewSettings.FileFormat.CommentLine, m_CancellationTokenSource.Token); }); }
private async void ButtonGuessHeader_Click(object sender, EventArgs e) { await buttonGuessHeader.RunWithHourglassAsync(async() => { using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings))) { var res = await improvedStream.GuessHasHeader(m_ViewSettings.CodePageId, m_ViewSettings.SkipRows, m_ViewSettings.FileFormat.CommentLine, m_ViewSettings.FileFormat.FieldDelimiter, m_CancellationTokenSource.Token); m_ViewSettings.HasFieldHeader = res.Item1; fileSettingBindingSource.ResetBindings(false); _MessageBox.Show(res.Item2, "Checking headers"); } }); }
private async void ButtonGuessDelimiter_ClickAsync(object sender, EventArgs e) { await buttonGuessDelimiter.RunWithHourglassAsync(async() => { using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings))) { await improvedStream.GuessDelimiter(m_ViewSettings.CodePageId, m_ViewSettings.SkipRows, m_ViewSettings.FileFormat.EscapeCharacter, m_CancellationTokenSource.Token); } }); // GuessDelimiterAsync does set the values, refresh them fileFormatBindingSource.ResetBindings(false); }
private async void ButtonGuessCP_ClickAsync(object sender, EventArgs e) { await buttonGuessCP.RunWithHourglassAsync(async() => { using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings))) { var(codepage, bom) = await improvedStream.GuessCodePage(m_CancellationTokenSource.Token); m_ViewSettings.CodePageId = codepage; m_ViewSettings.ByteOrderMark = bom; } }); fileSettingBindingSource.ResetBindings(false); }
private async void ButtonGuessTextQualifier_Click(object sender, EventArgs e) { var qualifier = string.Empty; await buttonGuessTextQualifier.RunWithHourglassAsync(async() => { using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings))) qualifier = await improvedStream.GuessQualifier(m_ViewSettings.CodePageId, m_ViewSettings.SkipRows, m_ViewSettings.FileFormat.FieldDelimiter, m_CancellationTokenSource.Token); }); if (qualifier != null) { m_ViewSettings.FileFormat.FieldQualifier = qualifier; } else { _MessageBox.Show("No Column Qualifier found", "Qualifier", MessageBoxButtons.OK, MessageBoxIcon.Information); } }