/// <summary> /// get a short friendly name of the format /// </summary> /// <returns></returns> public string GetShortName() { StringBuilder shorty = new StringBuilder(); if (TextFormatValue.HasFlag(TextFormat.COMPRESSED)) { if (TextFormatValue.HasFlag(TextFormat.NO_COMRPESSION) == false) { shorty.Append("Compressed "); } else { } } else { } if (TextFormatValue.HasFlag(TextFormat.UNICODE)) { if (TextFormatValue.HasFlag(TextFormat.ANSI) == false) { shorty.Append("Unicode "); } else { shorty.Append("Ansi "); } } else { shorty.Append("Ansi "); } if (TextFormatValue.HasFlag(TextFormat.RTF)) { if (TextFormatValue.HasFlag(TextFormat.NO_RTF) == false) { shorty.Append("Rich "); } else { shorty.Append("Plain "); } } else { shorty.Append("Plain Text "); } return(shorty.ToString()); }
/// <summary> /// get the Windows Dialog Filter for this format. Caller is expacted to add the all files filter /// </summary> /// <returns></returns> public string GetDialogBoxExt() { StringBuilder ret = new StringBuilder(); string ext = ".txt"; if (TextFormatValue.HasFlag(TextFormat.RTF)) { if (TextFormatValue.HasFlag(TextFormat.NO_RTF) == false) { ext = ".rtf"; } } ret.Append(MakeDialogFilter(GetShortName(), ext)); return(ret.ToString()); }
/// <summary> /// return the preferref extesion for this file type handler if any /// </summary> /// <returns></returns> public string GetPreferredExtension() { StringBuilder ret = new StringBuilder("."); if (TextFormatValue.HasFlag(TextFormat.AUTO_ADD_TXT)) { if (TextFormatValue.HasFlag(TextFormat.RTF)) { if (TextFormatValue.HasFlag(TextFormat.NO_RTF) == false) { ret.Append("RTF"); } else { ret.Append("TXT"); } } else { ret.Append("TXT"); } if (TextFormatValue.HasFlag(TextFormat.COMPRESSED)) { if (TextFormatValue.HasFlag(TextFormat.NO_COMRPESSION) == false) { ret.Append(".GZ"); } else { // append nothing. } } return(ret.ToString()); } return(string.Empty); }
/// <summary> /// From Text to Data /// </summary> /// <param name="Source">contains the tet to encode in the supported file</param> /// <param name="Output"> writing the text into the encoded file</param> public void WriteData(StreamReader Source, StreamWriter Output) { bool WantCompression = false; if (TextFormatValue.HasFlag(TextFormat.COMPRESSED)) { if (TextFormatValue.HasFlag(TextFormat.NO_COMRPESSION) == false) { WantCompression = true; } } if (WantCompression) { using (var Compressed = new GZipStream(Output.BaseStream, CompressionLevel.Optimal, true)) { string txt = Source.ReadToEnd(); byte[] txt_bytes; if (TextFormatValue.HasFlag(TextFormat.UNICODE)) { if (!TextFormatValue.HasFlag(TextFormat.ANSI)) { txt_bytes = Encoding.UTF8.GetBytes(txt); } else { txt_bytes = Encoding.ASCII.GetBytes(txt); } } else { txt_bytes = Encoding.ASCII.GetBytes(txt); } Compressed.Write(txt_bytes, 0, txt_bytes.Length); } } else { string Text; Text = Source.ReadToEnd(); byte[] Data; if (TextFormatValue.HasFlag(TextFormat.UNICODE)) { if (TextFormatValue.HasFlag(TextFormat.ANSI) == false) { // unicode Data = Encoding.Unicode.GetBytes(Text); } else { // ansi Data = Encoding.ASCII.GetBytes(Text); } } else { Data = Encoding.ASCII.GetBytes(Text); } Output.BaseStream.Write(Data, 0, Data.Length); } }
/// <summary> /// using to read from input encoded format and output as text /// </summary> /// <param name="Source"></param> /// <param name="Output"></param> public void ReadData(StreamReader Source, StreamWriter Output, out bool ContainsRtfTags) { bool UseCompression = false; ContainsRtfTags = false; if (TextFormatValue.HasFlag(TextFormat.RTF)) { if (TextFormatValue.HasFlag(TextFormat.NO_RTF) == false) { ContainsRtfTags = true; } } if (TextFormatValue.HasFlag(TextFormat.COMPRESSED)) { if (TextFormatValue.HasFlag(TextFormat.NO_COMRPESSION) == false) { UseCompression = true; } } if (UseCompression) { // NOTICE: This inheritly caps any compressed text as a max of 4GB\ // sources say that the origina size is at the END of the file -4 and necodied as a uint32 byte[] SizeByte = new byte[4]; int Size; Source.BaseStream.Seek(-4, SeekOrigin.End); Source.BaseStream.Read(SizeByte, 0, 4); Size = (int)BitConverter.ToUInt32(SizeByte, 0); // back to start Source.BaseStream.Seek(0, SeekOrigin.Begin); // read and uncompress the data using (var CompressedStream = new GZipStream(Source.BaseStream, CompressionMode.Decompress, true)) { byte[] UncompressedData = new byte[Size]; if (CompressedStream.Read(UncompressedData, 0, Size) == Size) { // we're ok? string text; if (TextFormatValue.HasFlag(TextFormat.UNICODE)) { if (TextFormatValue.HasFlag(TextFormat.ANSI) == false) { text = Encoding.Unicode.GetString(UncompressedData); } else { text = Encoding.ASCII.GetString(UncompressedData); } } else { text = Encoding.ASCII.GetString(UncompressedData); } // text should now contain the data we need to copy to Output Output.Write(text); } } } else { // no compression, can just interact with the stream. string text; byte[] Data = new byte[Source.BaseStream.Length]; Source.BaseStream.Read(Data, 0, (int)Source.BaseStream.Length); if (TextFormatValue.HasFlag(TextFormat.UNICODE)) { if (TextFormatValue.HasFlag(TextFormat.ANSI) == false) { text = Encoding.Unicode.GetString(Data); } else { text = Encoding.ASCII.GetString(Data); } } else { text = Encoding.ASCII.GetString(Data); } Output.Write(text); } }