| ofs | hex dump | ascii |
|---|
| 0000 | 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 40 00 00 00 40 08 06 00 00 00 aa 69 71 | .PNG........IHDR...@...@......iq |
| 0020 | de 00 00 00 04 73 42 49 54 08 08 08 08 7c 08 64 88 00 00 00 09 70 48 59 73 00 00 01 cf 00 00 01 | .....sBIT....|.d.....pHYs....... |
| 0040 | cf 01 e1 0c 10 c7 00 00 00 19 74 45 58 74 53 6f 66 74 77 61 72 65 00 77 77 77 2e 69 6e 6b 73 63 | ..........tEXtSoftware.www.inksc |
| 0060 | 61 70 65 2e 6f 72 67 9b ee 3c 1a 00 00 09 d6 49 44 41 54 78 da e5 9b 7b 50 53 57 1e c7 2f 88 40 | ape.org..<.....IDATx...{PSW../.@ |
| 0080 | b8 24 81 80 80 24 90 84 20 04 82 1a 40 a0 6e b7 56 c7 76 db 6e 67 ba 3b bb ad c3 da b5 db 4e 5b | .$...$......@.n.V.v.ng.;......N[ |
| 00a0 | a6 bb a3 62 78 b3 3e 16 5f 08 ca 2b bc 14 14 03 b6 b5 48 8d d5 dd ad d5 ae b5 62 15 5f 38 4a 50 | ...bx.>._..+......H.......b._8JP |
| 00c0 | 7c 80 52 54 9e c2 f2 48 f0 05 e1 ee b9 09 17 02 24 e1 de 9b 7b 41 bb 7f 7c 07 fe 60 86 fb f9 9c | |.RT...H........$...{A..|..`.... |
| 00e0 | 73 7e e7 fc ee 03 42 10 04 fa 7f ce b4 fe f3 ee 6f a4 4e 0f 8b c5 7e e0 57 eb 5f b8 00 c8 aa 5f | s~....B.........o.N...~.W._...._ |
| 0100 | 29 0d 53 2b 43 b6 6a 94 c1 95 fd ca e0 06 f0 f3 51 77 99 04 b9 b7 c9 13 b9 b7 99 8b 34 6f e3 0d | ).S+C.j.........Qw..........4o.. |
| 0120 | b4 6c e7 f7 b5 65 09 55 ed 79 a2 84 96 14 4f 87 17 5e 80 a6 22 cc 43 a3 94 66 03 d8 07 20 c8 98 | .l...e.U.y....O..^..".C..f...... |
| 0140 | 1c 94 22 cd e9 de 7a 01 46 72 7f 2b 77 a8 35 4b 70 a7 2d df f7 77 2f 9c 80 87 47 fc 99 6a 65 f0 | .."...z.Fr.+w.5Kp.-..w/...G..je. |
| 0160 | 66 00 aa 99 00 3e 9c ce 22 3f 93 f0 e3 d3 9a 25 bc f5 b0 50 14 fa 42 08 d0 1c 0c 79 03 00 76 98 | f....>.."?.....%...P..B....y..v. |
| 0180 | 02 47 d3 57 3e 1f b9 bf 85 8b 5b 00 b6 4c da 72 85 e5 cf b5 00 cd c1 e0 18 00 38 68 0e 1e 4d 9b | .G.W>.....[..L.r..........8h..M. |
| 01a0 | dc 87 18 bc e1 6c c8 16 d4 51 59 1f a8 81 4f 81 ac 35 ca 10 c5 64 e0 68 7a f6 49 48 c3 63 01 c5 | .....l...QY...O..5...d.hz.IH.c.. |
| 01c0 | b2 a7 73 a7 1f f7 b9 11 a0 39 14 bc 0d 0f 3c 98 21 66 0b 1f 21 09 3b 04 1d 40 bc cd b4 0b 50 1f | ..s......9....<.!f..!.;..@....P. |
| 01e0 | 0a 8e c4 05 8f 16 be 62 7f 4a e0 47 96 43 8e b0 7a 5a 05 a8 0f ce 97 a0 fb 39 1e f8 be 03 f3 d1 | .......b.J.G.C..zZ.......9...... |
| 0200 | ad 8d 52 01 68 da 72 45 79 d3 26 00 4c e9 a3 78 47 bf 3d 57 44 39 3c 9a 07 db 78 83 bd 59 12 ce | ..R.h.rEy.&.L..xG.=WD9<...x..Y.. |
| 0220 | 94 0b 50 2b e7 2f c1 0b df f3 79 10 2d f0 23 b3 40 2e 3c 3e e5 02 00 d8 45 bc 02 40 d5 a6 55 00 | ..P+./....y.-.#.@.<>....E..@..U. |
| 0240 | 7a 6a 6c cf f5 15 4d 99 80 7e 65 68 08 5e f8 ae 3d 62 5a e1 47 6b 01 b9 43 12 c9 e9 1f bc 09 0f | zjl...M..~eh.^..=bZ.Gk..C....... |
| 0260 | bc ba 02 2d 7c bc 29 11 d0 b2 83 df 39 65 02 40 f1 53 e1 2a 7c 79 a2 29 81 d7 1f 95 81 84 7c 21 | ...-|.).....9e.@.S.*|y.)......|! |
| 0280 | 9f 76 01 8f 0e 87 79 e1 2a 7c 5f 06 4d 1d 3c b6 0c f2 7c b2 68 17 a0 fe 5a ba 18 57 e1 cb e0 4f | .v....y.*|_.M.<...|.h...Z..W...O |
| 02a0 | bd 00 b9 cf b7 b4 0b 00 fd fd f2 49 0b 5f 89 78 ca e1 87 1b a5 cb f4 0b 38 24 8d 33 5f f8 a4 c8 | ...........I._.x........8$.3_... |
| 02c0 | 83 54 de b4 08 68 c9 14 34 4d c5 0c 48 37 27 a0 a3 c0 77 5a e0 c9 ee 04 c4 cf 00 07 43 d6 4e 18 | .T...h..4M..H7'...wZ........C.N. |
| 02e0 | 75 d0 e5 75 94 4a 7b 1b 32 82 ee d5 25 f3 06 ee fe 83 fa 33 7f fd 16 6f e4 d2 0e f1 c0 e9 cc b9 | u..u.J{.2...%......3...o........ |
| 0300 | 4f 6b d3 44 da 26 63 4b 20 83 df 4c bf 00 65 48 54 63 fe bc 0e e5 32 f1 b9 8d 02 9f cb 71 6c 41 | Ok.D.&cK...L..eHTc....2......qlA |
| 0320 | 6b 0c 53 38 10 c3 14 20 63 c2 12 f4 af 73 17 dc 2d 79 45 d0 78 ea 63 be ba 61 3d 7e d8 9f 53 bd | k.S8....c....s..-yE.x.c..a=~..S. |
| 0340 | 86 be c9 8b e8 8a 2a fb d3 4d 61 79 5c 3d b3 62 9d c6 b1 62 3d 32 2e 03 ce 15 6b 3b a4 fb 57 37 | ......*..May\=.b...b=2....k;..W7 |
| 0360 | ac db f3 4e f3 4f 99 92 81 96 2c e1 4d 5a 05 d4 06 b0 de de c5 9d fd f5 04 58 3c 01 42 f6 2e 12 | ...N.O....,.MZ...........X<.B... |
| 0380 | de bb 91 c8 d3 9a 02 af 4b f7 19 5c a9 58 76 9d 55 b1 be d5 08 f0 a4 e1 14 a6 1d 63 24 ee fb 35 | ........K..\.Xv.U..........c$..5 |
| 03a0 | e5 02 ae 8a 99 7e b5 81 ec 6f 41 90 1f 44 2e b7 49 09 18 cd b3 e2 85 c2 c6 3b eb 47 97 49 43 aa | .....~...oA..D..I........;.G.IC. |
| 03c0 | f7 d0 5f ca de af 63 56 ac d7 90 01 c7 c2 d8 54 f4 5f fb a4 52 c4 3e 49 51 0e 44 78 5b 2c e0 a6 | .._...cV.......T._..R.>IQ.Dx[,.. |
| 03e0 | bf 2b b3 56 c2 de 0e c0 9f a2 f0 58 62 99 fc 87 16 4a 40 12 38 82 d6 f3 7f e3 6b 0e e7 46 a8 dd | .+.V.......Xb....J@.8.....k..F.. |
| 0400 | 2a 92 9b 2c 01 d7 e5 c0 86 76 3d fc 48 1e d9 27 95 6d 82 e2 f6 c1 a4 04 d4 cc 73 74 bb 16 c8 be | *..,.....v=.H..'.m........st.... |
| 0420 | 65 08 8e 65 33 c7 eb bc a5 02 d0 44 79 cc a9 e4 ac 5b 51 67 31 3c 08 ac d8 7a 61 9c 00 2c d7 99 | e..e3......Dy....[Qg1<...za..,.. |
| 0440 | c9 65 2e 84 04 54 fb 38 b3 01 e8 15 63 f0 68 f6 f3 dc 2f 59 0a ff f1 6c bf 33 af 44 84 83 2b 80 | .e...T.8....c.h.../Y...l.3.D..+. |
| 0460 | 10 e7 c4 e5 37 2d 15 e0 90 99 77 d5 84 00 34 d5 50 ca 17 2c 5c 02 aa 43 3d 1d 6a 03 9d ce 98 82 | ....7-....w...4.P..,\..C=.j..... |
| 0480 | 47 53 2d 76 d2 c4 32 05 3d a4 e1 3d fd ab 50 78 4c 00 1a a7 f// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.IO;
using System.Text;
namespace Tango.Scripting.Editors.Utils
{
/// <summary>
/// Class that can open text files with auto-detection of the encoding.
/// </summary>
public static class FileReader
{
/// <summary>
/// Gets if the given encoding is a Unicode encoding (UTF).
/// </summary>
/// <remarks>
/// Returns true for UTF-7, UTF-8, UTF-16 LE, UTF-16 BE, UTF-32 LE and UTF-32 BE.
/// Returns false for all other encodings.
/// </remarks>
public static bool IsUnicode(Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException("encoding");
switch (encoding.CodePage) {
case 65000: // UTF-7
case 65001: // UTF-8
case 1200: // UTF-16 LE
case 1201: // UTF-16 BE
case 12000: // UTF-32 LE
case 12001: // UTF-32 BE
return true;
default:
return false;
}
}
/// <summary>
/// Reads the content of the given stream.
/// </summary>
/// <param name="stream">The stream to read.
/// The stream must support seeking and must be positioned at its beginning.</param>
/// <param name="defaultEncoding">The encoding to use if the encoding cannot be auto-detected.</param>
/// <returns>The file content as string.</returns>
public static string ReadFileContent(Stream stream, Encoding defaultEncoding)
{
using (StreamReader reader = OpenStream(stream, defaultEncoding)) {
return reader.ReadToEnd();
}
}
/// <summary>
/// Reads the content of the file.
/// </summary>
/// <param name="fileName">The file name.</param>
/// <param name="defaultEncoding">The encoding to use if the encoding cannot be auto-detected.</param>
/// <returns>The file content as string.</returns>
public static string ReadFileContent(string fileName, Encoding defaultEncoding)
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
return ReadFileContent(fs, defaultEncoding);
}
}
/// <summary>
/// Opens the specified file for reading.
/// </summary>
/// <param name="fileName">The file to open.</param>
/// <param name="defaultEncoding">The encoding to use if the encoding cannot be auto-detected.</param>
/// <returns>Returns a StreamReader that reads from the stream. Use
/// <see cref="StreamReader.CurrentEncoding"/> to get the encoding that was used.</returns>
public static StreamReader OpenFile(string fileName, Encoding defaultEncoding)
{
if (fileName == null)
throw new ArgumentNullException("fileName");
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
try {
return OpenStream(fs, defaultEncoding);
// don't use finally: the stream must be kept open until the StreamReader closes it
} catch {
fs.Dispose();
throw;
}
}
/// <summary>
/// Opens the specified stream for reading.
/// </summary>
/// <param name="stream">The stream to open.</param>
/// <param name="defaultEncoding">The encoding to use if the encoding cannot be auto-detected.</param>
/// <returns>Returns a StreamReader that reads from the stream. Use
/// <see cref="StreamReader.CurrentEncoding"/> to get the encoding that was used.</returns>
public static StreamReader OpenStream(Stream stream, Encoding defaultEncoding)
{
if (stream == null)
throw new ArgumentNullException("stream");
if (stream.Position != 0)
throw new ArgumentException("stream is not positioned at beginning.", "stream");
if (defaultEncoding == null)
throw new ArgumentNullException("defaultEncoding");
if (stream.Length >= 2) {
// the autodetection of StreamReader is not capable of detecting the difference
// between ISO-8859-1 and UTF-8 without BOM.
int firstByte = stream.ReadByte();
int secondByte = stream.ReadByte();
switch ((firstByte << 8) | secondByte) {
case 0x0000: // either UTF-32 Big Endian or a binary file; use StreamReader
case 0xfffe: // Unicode BOM (UTF-16 LE or UTF-32 LE)
case 0xfeff: // UTF-16 BE BOM
case 0xefbb: // start of UTF-8 BOM
// StreamReader autodetection works
stream.Position = 0;
return new StreamReader(stream);
default:
return AutoDetect(stream, (byte)firstByte, (byte)secondByte, defaultEncoding);
}
} else {
if (defaultEncoding != null) {
return new StreamReader(stream, defaultEncoding);
} else {
return new StreamReader(stream);
}
}
}
static StreamReader AutoDetect(Stream fs, byte firstByte, byte secondByte, Encoding defaultEncoding)
{
int max = (int)Math.Min(fs.Length, 500000); // look at max. 500 KB
const int ASCII = 0;
const int Error = 1;
const int UTF8 = 2;
const int UTF8Sequence = 3;
int state = ASCII;
int sequenceLength = 0;
byte b;
for (int i = 0; i < max; i++) {
if (i == 0) {
b = firstByte;
} else if (i == 1) {
b = secondByte;
} else {
b = (byte)fs.ReadByte();
}
if (b < 0x80) {
// normal ASCII character
if (state == UTF8Sequence) {
state = Error;
break;
}
} else if (b < 0xc0) {
// 10xxxxxx : continues UTF8 byte sequence
if (state == UTF8Sequence) {
--sequenceLength;
if (sequenceLength < 0) {
state = Error;
break;
} else if (sequenceLength == 0) {
state = UTF8;
}
} else {
state = Error;
break;
}
} else if (b >= 0xc2 && b < 0xf5) {
// beginning of byte sequence
if (state == UTF8 || state == ASCII) {
state = UTF8Sequence;
if (b < 0xe0) {
sequenceLength = 1; // one more byte following
} else if (b < 0xf0) {
sequenceLength = 2; // two more bytes following
} else {
sequenceLength = 3; // three more bytes following
}
} else {
state = Error;
break;
}
} else {
// 0xc0, 0xc1, 0xf5 to 0xff are invalid in UTF-8 (see RFC 3629)
state = Error;
break;
}
}
fs.Position = 0;
switch (state) {
case ASCII:
case Error:
// when the file seems to be ASCII or non-UTF8,
// we read it using the user-specified encoding so it is saved again
// using that encoding.
if (IsUnicode(defaultEncoding)) {
// the file is not Unicode, so don't read it using Unicode even if the
// user has choosen Unicode as the default encoding.
// If we don't do this, SD will end up always adding a Byte Order Mark
// to ASCII files.
defaultEncoding = Encoding.Default; // use system encoding instead
}
return new StreamReader(fs, defaultEncoding);
default:
return new StreamReader(fs);
}
}
}
}
|