public ProjectWriter(ByteOrderMark bom) { encoding = bom != null?Encoding.GetEncoding(bom.Name) : null; ByteOrderMark = bom; }
public void Load(string file) { using (FileStream fs = File.OpenRead(file)) { byte[] buf = new byte [1024]; int nread, i; if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { return; } if (ByteOrderMark.TryParse(buf, nread, out bom)) { i = bom.Length; } else { i = 0; } do { // Read to the first newline to figure out which line endings this file is using while (i < nread) { if (buf[i] == '\r') { newLine = "\r\n"; break; } else if (buf[i] == '\n') { newLine = "\n"; break; } i++; } if (newLine == null) { if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { newLine = "\n"; break; } i = 0; } } while (newLine == null); // Check for a blank line at the end endsWithEmptyLine = fs.Seek(-1, SeekOrigin.End) > 0 && fs.ReadByte() == (int)'\n'; } // Load the XML document doc = new XmlDocument(); doc.PreserveWhitespace = false; // HACK: XmlStreamReader will fail if the file is encoded in UTF-8 but has <?xml version="1.0" encoding="utf-16"?> // To work around this, we load the XML content into a string and use XmlDocument.LoadXml() instead. string xml = File.ReadAllText(file); doc.LoadXml(xml); }
public static void WriteFile(FilePath fileName, string content, string encoding, bool saveBOM) { ByteOrderMark bom = saveBOM && encoding != null?ByteOrderMark.GetByName(encoding) : null; WriteFile(fileName, content, encoding, bom, false); }
public void Read(Stream stream, string encoding) { ByteOrderMark bom = null; byte[] content = null; long nread; retry: stream.Seek(0, SeekOrigin.Begin); if (encoding == null) { if (ByteOrderMark.TryParse(stream, out bom)) { stream.Seek(bom.Length, SeekOrigin.Begin); } else { stream.Seek(0, SeekOrigin.Begin); } } content = new byte [bom != null ? stream.Length - bom.Length : stream.Length]; nread = 0; int n; while ((n = stream.Read(content, (int)nread, (content.Length - (int)nread))) > 0) { nread += n; } if (encoding != null) { string s = ConvertFromEncoding(content, nread, encoding); if (s == null) { // The encoding provided was wrong, fall back to trying to use the BOM if it exists... encoding = null; content = null; goto retry; } text = new StringBuilder(s); sourceEncoding = encoding; return; } if (bom != null) { string s = ConvertFromEncoding(content, nread, bom.Name); if (s != null) { HadBOM = true; sourceEncoding = bom.Name; text = new StringBuilder(s); return; } } // Fall back to trying all the encodings... foreach (TextEncoding co in TextEncoding.ConversionEncodings) { string s = ConvertFromEncoding(content, nread, co.Id); if (s != null) { sourceEncoding = co.Id; text = new StringBuilder(s); return; } } throw new Exception("Unknown text file encoding"); }
public static void WriteFile(FilePath fileName, string content, ByteOrderMark bom, bool onlyIfChanged) { WriteFile(fileName, content, bom != null ? bom.Name : null, bom, onlyIfChanged); }
public static void WriteFile(FilePath fileName, string content, string encoding, ByteOrderMark bom, bool onlyIfChanged) { byte[] buf = Encoding.UTF8.GetBytes(content); WriteFile(fileName, buf, encoding, bom, onlyIfChanged); }
public static void WriteFile(FilePath fileName, byte[] content, string encoding, ByteOrderMark bom, bool onlyIfChanged) { int contentLength = content.Length + (bom != null ? bom.Length : 0); byte[] converted; if (encoding != null) { converted = ConvertToBytes(content, content.LongLength, encoding, "UTF-8"); } else { converted = content; } if (onlyIfChanged) { FileInfo finfo = new FileInfo(fileName); if (finfo.Exists && finfo.Length == contentLength) { bool changed = false; // Open the file on disk and compare them byte by byte... using (FileStream stream = finfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] buf = new byte [4096]; int bomOffset = 0; int offset = 0; int nread; int i; while (!changed && (nread = stream.Read(buf, 0, buf.Length)) > 0) { i = 0; if (bom != null && bomOffset < bom.Length) { while (i < nread && bomOffset < bom.Length) { if (bom.Bytes[bomOffset] != buf[i]) { changed = true; break; } bomOffset++; i++; } if (changed) { break; } } while (i < nread && offset < converted.Length) { if (converted[offset] != buf[i]) { changed = true; break; } offset++; i++; } if (offset == converted.Length && i < nread) { changed = true; } } if (offset < converted.Length) { changed = true; } } if (!changed) { return; } } // Content has changed... } string tempName = Path.GetDirectoryName(fileName) + Path.DirectorySeparatorChar + ".#" + Path.GetFileName(fileName); FileStream fs = new FileStream(tempName, FileMode.Create, FileAccess.Write); if (bom != null) { fs.Write(bom.Bytes, 0, bom.Length); } fs.Write(converted, 0, converted.Length); fs.Flush(); fs.Close(); FileService.SystemRename(tempName, fileName); }
public static TextFormatInfo GetTextFormatInfo(string file) { var info = new TextFormatInfo(); string newLine = null; ByteOrderMark bom; using (FileStream fs = File.OpenRead(file)) { byte[] buf = new byte [1024]; int nread, i; if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { return(info); } if (ByteOrderMark.TryParse(buf, nread, out bom)) { i = bom.Length; } else { i = 0; } do { // Read to the first newline to figure out which line endings this file is using while (i < nread) { if (buf[i] == '\r') { newLine = "\r\n"; break; } else if (buf[i] == '\n') { newLine = "\n"; break; } i++; } if (newLine == null) { if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { newLine = "\n"; break; } i = 0; } } while (newLine == null); // Check for a blank line at the end info.EndsWithEmptyLine = fs.Seek(-1, SeekOrigin.End) > 0 && fs.ReadByte() == (int)'\n'; info.NewLine = newLine; info.ByteOrderMark = bom; return(info); } }
public void Read(FilePath fileName, string encoding) { // Reads the file using the specified encoding. // If the encoding is null, it autodetects the // required encoding. this.name = fileName; ByteOrderMark bom = null; byte[] content = null; long nread; retry: using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (encoding == null) { if (ByteOrderMark.TryParse(stream, out bom)) { stream.Seek(bom.Length, SeekOrigin.Begin); } else { stream.Seek(0, SeekOrigin.Begin); } } content = new byte [bom != null ? stream.Length - bom.Length : stream.Length]; nread = 0; int n; while ((n = stream.Read(content, (int)nread, (content.Length - (int)nread))) > 0) { nread += n; } } if (encoding != null) { string s = ConvertFromEncoding(content, nread, encoding); if (s == null) { // The encoding provided was wrong, fall back to trying to use the BOM if it exists... encoding = null; content = null; goto retry; } text = new StringBuilder(s); sourceEncoding = encoding; return; } else if (bom != null) { string s = ConvertFromEncoding(content, nread, bom.Name); if (s != null) { HadBOM = true; sourceEncoding = bom.Name; text = new StringBuilder(s); return; } } // Fall back to trying all the encodings... foreach (TextEncoding co in TextEncoding.ConversionEncodings) { string s = ConvertFromEncoding(content, nread, co.Id); if (s != null) { sourceEncoding = co.Id; text = new StringBuilder(s); return; } } throw new Exception("Unknown text file encoding"); }