Beispiel #1
0
        public Csv(Buf data)
        {
            byte[] src = data.ByteData;
            int    bomSize;

            Encoding enc = Str.CheckBOM(src, out bomSize);

            if (bomSize >= 1)
            {
                src = Util.RemoveStartByteArray(src, bomSize);
            }

            init(new Buf(src), enc);
        }
Beispiel #2
0
        void init(Buf data, Encoding encoding)
        {
            if (encoding == null)
            {
                encoding = defaultEncoding;
            }

            int      bomSize = 0;
            Encoding enc2    = null;

            if (data != null)
            {
                enc2 = Str.CheckBOM(data.ByteData, out bomSize);
            }
            if (bomSize >= 1)
            {
                data = new Buf(Util.RemoveStartByteArray(data.ByteData, bomSize));
            }
            if (enc2 != null)
            {
                encoding = enc2;
            }
            this.encoding = encoding;

            entryList = new List <CsvEntry>();

            if (data != null)
            {
                MemoryStream ms = new MemoryStream(data.ByteData);
                StreamReader sr = new StreamReader(ms, this.encoding);

                while (true)
                {
                    string s = sr.ReadLine();

                    if (s == null)
                    {
                        break;
                    }

                    char[]   sep     = { ',' };
                    string[] strings = s.Trim().Split(sep, StringSplitOptions.None);

                    CsvEntry e = new CsvEntry(strings);
                    Add(e);
                }
            }
        }
Beispiel #3
0
        public static string ReadAllTextWithAutoGetEncoding(string fileName)
        {
            fileName = InnerFilePath(fileName);

            byte[] data = File.ReadAllBytes(fileName);

            int      bomSize;
            Encoding enc = Str.GetEncoding(data, out bomSize);

            if (enc == null)
            {
                enc = Encoding.Default;
            }

            data = Util.RemoveStartByteArray(data, bomSize);

            return(enc.GetString(data));
        }