예제 #1
0
        public SqlChars ReadTextByCpName(SqlInt64 offset, SqlInt64 count, SqlBoolean detectEncoding, [SqlFacet(MaxSize = 128)] SqlString cpName)
        {
            if (IsNull)
            {
                return(SqlChars.Null);
            }

            var fileEncoding = cpName.IsNull ? SqlRuntime.FileEncoding : Encoding.GetEncoding(cpName.Value);

            using (var fs = _fi.OpenRead())
            {
                if (detectEncoding.IsTrue)
                {
                    var preamble = EncodingPreamble.Detect(fs.ReadBytesMost(1, 4) ?? new byte[0]);
                    if (preamble != null)
                    {
                        fileEncoding = Encoding.GetEncoding(preamble.CodePage);
                    }
                    else
                    {
                        fs.Position = 0;
                    }
                }
                if (!offset.IsNull)
                {
                    fs.Position = offset.Value;
                }
                return(fs.ReadSqlChars(fileEncoding, !count.IsNull ? count.Value : (fs.Length - fs.Position).Yield(t => (int)Comparable.Min(t, int.MaxValue), (t, r) => t - r, t => t > 0).Aggregate(0L, (t, p) => t + fileEncoding.GetMaxCharCount(p))));
            }
        }
예제 #2
0
        public SqlChars ReadAllTextByCpId(SqlBoolean detectEncoding, SqlInt32 cpId)
        {
            if (IsNull)
            {
                return(SqlChars.Null);
            }

            var fileEncoding = cpId.IsNull ? SqlRuntime.FileEncoding : Encoding.GetEncoding(cpId.Value);

            using (var fs = _fi.OpenRead())
            {
                if (detectEncoding.IsTrue)
                {
                    var preamble = EncodingPreamble.Detect(fs.ReadBytesMost(1, 4) ?? new byte[0]);
                    if (preamble != null)
                    {
                        fileEncoding = Encoding.GetEncoding(preamble.CodePage);
                    }
                    else
                    {
                        fs.Position = 0;
                    }
                }
                return(fs.ReadSqlChars(fileEncoding, (fs.Length - fs.Position).Yield(t => (int)Comparable.Min(t, int.MaxValue), (t, r) => t - r, t => t > 0).Aggregate(0L, (t, p) => t + fileEncoding.GetMaxCharCount(p))));
            }
        }
예제 #3
0
        public static IEnumerable ReadLinesByCpName(SqlFileInfo fileInfo, SqlInt64 offset, SqlString searchTerminator, SqlString delimiter, [DefaultValue("''")] SqlString newTerminator,
                                                    [DefaultValue("NULL")] SqlInt32 maxCount, SqlBoolean detectEncoding, [SqlFacet(MaxSize = 128)] SqlString cpName)
        {
            if (fileInfo.IsNull || searchTerminator.IsNull)
            {
                yield break;
            }

            var fileEncoding    = cpName.IsNull ? SqlRuntime.FileEncoding : Encoding.GetEncoding(cpName.Value);
            var terminatorsList = searchTerminator.IsNull || delimiter.Value.Length == 0 ? new[] { searchTerminator.Value } : searchTerminator.Value.Split(new[] { delimiter.Value }, StringSplitOptions.RemoveEmptyEntries);

            using (var fs = fileInfo.FileInfo.OpenRead())
            {
                if (detectEncoding.IsTrue)
                {
                    var preamble = EncodingPreamble.Detect(fs.ReadAt(0L, s => s.ReadBytesMost(1, 4)) ?? new byte[0]);
                    if (preamble != null)
                    {
                        fileEncoding = Encoding.GetEncoding(preamble.CodePage);
                    }
                }
                if (!offset.IsNull)
                {
                    fs.Position = offset.Value;
                }
                var position = fs.Position;
                foreach (var line in fs.ReadLines(fileEncoding, terminatorsList, newTerminator.IsNull ? null : newTerminator.Value, maxCount.IsNull ? int.MaxValue : maxCount.Value))
                {
                    yield return(Tuple.Create(position, line));

                    position = fs.Position;
                }
            }
        }
예제 #4
0
        public SqlInt64 WriteTextByCpName(SqlChars chars, SqlString terminator, SqlInt64 offset, SqlBoolean insert, SqlByte useEncoding, [SqlFacet(MaxSize = 128)] SqlString cpName)
        {
            if (IsNull || chars.IsNull)
            {
                return(SqlInt64.Null);
            }

            var fileEncoding = cpName.IsNull ? SqlRuntime.FileEncoding : Encoding.GetEncoding(cpName.Value);
            var usePreamble  = useEncoding.IsNull ? 0 : useEncoding.Value;

            using (var fs = _fi.Open(FileMode.OpenOrCreate, usePreamble > 0 ? FileAccess.ReadWrite : FileAccess.Write))
            {
                if (usePreamble > 0)
                {
                    var preamble = EncodingPreamble.Detect(fs.ReadAt(0L, s => s.ReadBytesMost(1, 4)) ?? new byte[0]);
                    if (preamble != null)
                    {
                        fileEncoding = Encoding.GetEncoding(preamble.CodePage);
                    }
                    else if (usePreamble == 2)
                    {
                        var bom = fileEncoding.GetPreamble();
                        if (bom != null && bom.Length > 0)
                        {
                            fs.Move(bom.Length, fs.Length, SqlRuntime.IoBufferSize);
                            fs.Locate(0).WriteBytes(bom);
                            if (!offset.IsNull)
                            {
                                offset = new SqlInt64(offset.Value + bom.Length);
                            }
                        }
                    }
                }
                if (!offset.IsNull)
                {
                    fs.Position = offset.Value;
                }
                else
                {
                    fs.Position = fs.Length;
                }
                Int64 position = fs.Position;
                if (!offset.IsNull && insert.IsTrue)
                {
                    long size = fileEncoding.GetByteCount(chars.Buffer, 0, (int)chars.Length) + (!terminator.IsNull ? fileEncoding.GetByteCount(terminator.Value) : 0);
                    fs.Move(offset.Value + size, size, SqlRuntime.IoBufferSize);
                    fs.Position = position;
                }
                fs.WriteSqlChars(chars, fileEncoding);
                if (!terminator.IsNull)
                {
                    fs.WriteString(terminator.Value, fileEncoding);
                }
                fs.Flush();
                return(fs.Position - position);
            }
        }
예제 #5
0
        public SqlInt32 DetectCodePage()
        {
            if (IsNull)
            {
                return(SqlInt32.Null);
            }

            using (var fs = _fi.OpenRead())
            {
                var preamble = EncodingPreamble.Detect(fs.ReadBytesMost(1, 4) ?? new byte[0]);
                return(preamble != null ? preamble.CodePage : SqlInt32.Null);
            }
        }
예제 #6
0
        public SqlInt64 SearchTextByCpName(SqlInt64 offset, SqlInt32 skip, SqlChars pattern, SqlBoolean detectEncoding, [SqlFacet(MaxSize = 128)] SqlString cpName)
        {
            if (IsNull || pattern.IsNull)
            {
                return(SqlInt64.Null);
            }

            var fileEncoding = cpName.IsNull ? SqlRuntime.FileEncoding : Encoding.GetEncoding(cpName.Value);

            using (var fs = _fi.OpenRead())
            {
                if (detectEncoding.IsTrue)
                {
                    var preamble = EncodingPreamble.Detect(fs.ReadBytesMost(1, 4) ?? new byte[0]);
                    if (preamble != null)
                    {
                        fileEncoding = Encoding.GetEncoding(preamble.CodePage);
                    }
                    else
                    {
                        fs.Position = 0L;
                    }
                }
                if (!offset.IsNull)
                {
                    fs.Position = offset.Value;
                }
                if (!skip.IsNull)
                {
                    using (var e = fs.ReadChars(fileEncoding).GetEnumerator())
                        for (int count = skip.Value; count > 0 && e.MoveNext(); count--)
                        {
                            ;
                        }
                }
                long start = fs.Position;
                long found = fs.Find(fileEncoding, long.MaxValue, (int)pattern.Length, (char v, int i, int j) => pattern[i] == (j >= 0 ? pattern[j] : v));
                return(found >= 0L ? start + found : found);
            }
        }