/// <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 }; }
/// <summary> /// Creates an instance of the TextReader /// </summary> /// <param name="improvedStream">An Improved Stream</param> /// <param name="codePageId">The assumed code page id</param> /// <param name="skipLines"> /// Number of lines that should be skipped at the beginning of the file /// </param> /// <remarks> /// This routine uses a TextReader to allow character decoding, it will always read they the /// first few bytes of the source stream to look at a possible existing BOM if found, it will /// overwrite the provided data /// </remarks> public ImprovedTextReader([NotNull] IImprovedStream improvedStream, int codePageId = 65001, int skipLines = 0) { m_SkipLines = skipLines; m_ImprovedStream = improvedStream as Stream ?? throw new ArgumentNullException(nameof(improvedStream)); // read the BOM in any case var buff = new byte[4]; m_ImprovedStream.Read(buff, 0, buff.Length); var intCodePageByBom = EncodingHelper.GetEncodingByByteOrderMark(buff); improvedStream.Seek(0, SeekOrigin.Begin); var byteOrderMark = false; if (intCodePageByBom != null) { byteOrderMark = true; m_CodePage = intCodePageByBom.CodePage; } else { try { m_CodePage = codePageId; } catch (Exception) { Logger.Warning("Codepage {0} not supported, using UTF8", codePageId); m_CodePage = Encoding.UTF8.CodePage; } } m_BomLength = byteOrderMark ? EncodingHelper.BOMLength(m_CodePage) : 0; ToBeginning(); }
public JsonFileReader([NotNull] IImprovedStream improvedStream, [CanBeNull] IEnumerable <IColumn> columnDefinition = null, long recordLimit = 0, bool treatNbspAsSpace = false, bool trim = false, string treatTextAsNull = null) : this(columnDefinition, recordLimit, treatNbspAsSpace, trim, treatTextAsNull, null) { m_ImprovedStream = improvedStream; }
public FindSkipRows(ICsvFile csvFile) { InitializeComponent(); fileSetting = csvFile; fileSettingBindingSource.DataSource = csvFile; fileFormatBindingSource.DataSource = csvFile.FileFormat; m_Stream = new ImprovedStream(new SourceAccess(csvFile)); UpdateHighlight(); }
private void OriginalStream() { m_MemoryStream?.Dispose(); m_MemoryStream = null; m_Stream = new ImprovedStream(new SourceAccess(m_FullPath)); textBox.OpenBindingStream(m_Stream as Stream, Encoding.GetEncoding(m_CodePage, new EncoderReplacementFallback("?"), new DecoderReplacementFallback("?"))); HighlightVisibleRange(); prettyPrintJsonToolStripMenuItem.Checked = false; originalFileToolStripMenuItem.Checked = true; }
public override void Close() { base.Close(); m_JsonTextReader?.Close(); ((IDisposable)m_JsonTextReader)?.Dispose(); m_JsonTextReader = null; m_StreamReader?.Dispose(); m_StreamReader = null; if (!SelfOpenedStream) { return; } m_ImprovedStream?.Dispose(); m_ImprovedStream = null; }
/// <summary> /// CSV File to display /// </summary> public void OpenFile(bool json, string qualifier, string delimiter, string escape, int codePage, int skipLines, string comment) { if (!FileSystemUtils.FileExists(m_FullPath)) { textBox.Text = $"\nThe file '{m_FullPath}' does not exist."; } else { try { if (json) { m_HighLighter = new SyntaxHighlighterJson(textBox); textBox.ContextMenuStrip = contextMenuJson; } else { m_HighLighter = new SyntaxHighlighterDelimitedText(textBox, qualifier, delimiter, escape, comment); } m_Stream = new ImprovedStream(new SourceAccess(m_FullPath)); m_SkipLines = !json ? skipLines : 0; m_CodePage = codePage; OriginalStream(); } catch (Exception ex) { m_HighLighter = null; textBox.Text = $"Issue opening the file {m_FullPath} for display:\n\n\n{ex.Message}"; } } }