示例#1
0
        private static string GetNextToken(byte terminalCh, byte[] src, ref int offset, ref int count)
        {
            int num = CsvDecoder.IndexOf(terminalCh, src, offset, count);

            if (num == -1)
            {
                return(null);
            }
            int    num2   = num - offset;
            string result = (string)CsvDecoder.DecodeString(src, offset, num2);
            int    num3   = num2 + 1;

            offset += num3;
            count  -= num3;
            return(result);
        }
示例#2
0
        public static object DecodeKeyValuePair(byte[] src, int offset, int count)
        {
            string nextToken = CsvDecoder.GetNextToken(SpecialCharacters.ColonByte, src, ref offset, ref count);

            if (string.IsNullOrEmpty(nextToken))
            {
                return(null);
            }
            string nextToken2 = CsvDecoder.GetNextToken(SpecialCharacters.EqualsByte, src, ref offset, ref count);

            if (string.IsNullOrEmpty(nextToken2) || !SpecialCharacters.IsValidKey(nextToken2))
            {
                return(null);
            }
            object obj;

            if (nextToken.Equals("Dt", StringComparison.OrdinalIgnoreCase))
            {
                obj = CsvDecoder.DecodeDateTime(src, offset, count);
            }
            else if (nextToken.Equals("I32", StringComparison.OrdinalIgnoreCase))
            {
                obj = CsvDecoder.DecodeInt32(src, offset, count);
            }
            else if (nextToken.Equals("S", StringComparison.OrdinalIgnoreCase))
            {
                obj = CsvDecoder.DecodeString(src, offset, count);
            }
            else if (nextToken.Equals("F", StringComparison.OrdinalIgnoreCase))
            {
                obj = CsvDecoder.DecodeFloat(src, offset, count);
            }
            else
            {
                if (!nextToken.Equals("Dbl", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
                obj = CsvDecoder.DecodeDouble(src, offset, count);
            }
            if (obj == null)
            {
                return(null);
            }
            return(new KeyValuePair <string, object>(nextToken2, obj));
        }
示例#3
0
        public static CsvDecoderCallback GetDecoder(Type type)
        {
            CsvDecoderCallback result;

            if (CsvDecoder.decoders.TryGetValue(type, out result))
            {
                return(result);
            }
            if (type.IsEnum)
            {
                return(CsvDecoder.MapSafe(type, (byte[] src, int offset, int count) => CsvDecoder.DecodeEnum(type, src, offset, count)));
            }
            if (type.IsArray)
            {
                return(CsvDecoder.MapSafe(type, new CsvDecoderCallback(new CsvArrayDecoder(type.GetElementType()).Decode)));
            }
            return(null);
        }
示例#4
0
 private void InitializeMembers(CsvTable table, Version schemaVersion, Stream data, int readSize)
 {
     this.table         = table;
     this.schemaVersion = schemaVersion;
     this.data          = data;
     this.decoder       = new CsvDecoderCallback[table.Fields.Length];
     if (this.data.CanSeek)
     {
         this.position = this.data.Position;
     }
     this.readSize = readSize;
     for (int i = 0; i < table.Fields.Length; i++)
     {
         this.decoder[i] = CsvDecoder.GetDecoder(table.Fields[i].Type);
     }
     this.cacheVersion      = new long[table.Fields.Length];
     this.cache             = new object[table.Fields.Length];
     this.row               = new CsvRowBuffer(readSize);
     this.unsupportedFields = table.GetFieldsAddedAfterVersion(this.schemaVersion);
 }
示例#5
0
        private static object DecodeDateTime(byte[] src, int offset, int count)
        {
            if (count != "yyyy-MM-ddThh:mm:ss.sssZ".Length && count != "yyyy-MM-ddThh:mm:ss.ssssssZ".Length)
            {
                return(null);
            }
            int year;

            if (!CsvDecoder.TryDecodeDigits(src, offset, 4, out year))
            {
                return(null);
            }
            offset += 4;
            if (src[offset++] != 45)
            {
                return(null);
            }
            int month;

            if (!CsvDecoder.TryDecodeDigits(src, offset, 2, out month))
            {
                return(null);
            }
            offset += 2;
            if (src[offset++] != 45)
            {
                return(null);
            }
            int day;

            if (!CsvDecoder.TryDecodeDigits(src, offset, 2, out day))
            {
                return(null);
            }
            offset += 2;
            if (src[offset++] != 84)
            {
                return(null);
            }
            int hour;

            if (!CsvDecoder.TryDecodeDigits(src, offset, 2, out hour))
            {
                return(null);
            }
            offset += 2;
            if (src[offset++] != 58)
            {
                return(null);
            }
            int minute;

            if (!CsvDecoder.TryDecodeDigits(src, offset, 2, out minute))
            {
                return(null);
            }
            offset += 2;
            if (src[offset++] != 58)
            {
                return(null);
            }
            int second;

            if (!CsvDecoder.TryDecodeDigits(src, offset, 2, out second))
            {
                return(null);
            }
            offset += 2;
            if (src[offset++] != 46)
            {
                return(null);
            }
            int millisecond;

            if (!CsvDecoder.TryDecodeDigits(src, offset, 3, out millisecond))
            {
                return(null);
            }
            offset += 3;
            int num = 0;

            if (count == "yyyy-MM-ddThh:mm:ss.ssssssZ".Length)
            {
                if (!CsvDecoder.TryDecodeDigits(src, offset, 3, out num))
                {
                    return(null);
                }
                offset += 3;
            }
            if (src[offset++] != 90)
            {
                return(null);
            }
            object result;

            try
            {
                DateTime dateTime = new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Utc);
                dateTime += TimeSpan.FromTicks((long)(num * 10));
                result    = dateTime;
            }
            catch (ArgumentException)
            {
                result = null;
            }
            return(result);
        }
示例#6
0
        public object Decode(byte[] src, int offset, int count)
        {
            CsvDecoderCallback decoder = CsvDecoder.GetDecoder(this.elementType);
            List <object>      list    = new List <object>();

            new CsvParser(59, 39);
            byte[] array = new byte[4096];
            int    i     = offset;
            int    num   = offset + count;

            CsvArrayDecoder.State state = CsvArrayDecoder.State.Whitespace;
            int num2 = 0;

            while (i < num)
            {
                byte b = src[i++];
                switch (state)
                {
                case CsvArrayDecoder.State.Whitespace:
                    if (b == 39)
                    {
                        state = CsvArrayDecoder.State.QuotedField;
                        num2  = 0;
                    }
                    else
                    {
                        byte b2 = b;
                        switch (b2)
                        {
                        case 9:
                        case 10:
                        case 13:
                            continue;

                        case 11:
                        case 12:
                            break;

                        default:
                            if (b2 == 32)
                            {
                                continue;
                            }
                            break;
                        }
                        state = CsvArrayDecoder.State.Field;
                        i--;
                        num2 = i;
                    }
                    break;

                case CsvArrayDecoder.State.Field:
                    if (b == 59)
                    {
                        state = CsvArrayDecoder.State.Whitespace;
                        list.Add(decoder(src, num2, i - num2 - 1));
                        num2 = 0;
                    }
                    break;

                case CsvArrayDecoder.State.QuotedField:
                    if (b == 39)
                    {
                        state = CsvArrayDecoder.State.EndQuote;
                    }
                    else if (num2 < array.Length)
                    {
                        array[num2++] = b;
                    }
                    break;

                case CsvArrayDecoder.State.EndQuote:
                    if (b == 39)
                    {
                        state = CsvArrayDecoder.State.QuotedField;
                        if (num2 < array.Length)
                        {
                            array[num2++] = 39;
                        }
                    }
                    else
                    {
                        state = CsvArrayDecoder.State.EndQuoteIgnore;
                        i--;
                        list.Add(decoder(array, 0, num2));
                    }
                    break;

                case CsvArrayDecoder.State.EndQuoteIgnore:
                    if (b == 59)
                    {
                        state = CsvArrayDecoder.State.Whitespace;
                        num2  = 0;
                    }
                    break;
                }
            }
            switch (state)
            {
            case CsvArrayDecoder.State.Whitespace:
            case CsvArrayDecoder.State.QuotedField:
            case CsvArrayDecoder.State.EndQuote:
                list.Add(decoder(array, 0, num2));
                break;

            case CsvArrayDecoder.State.Field:
                list.Add(decoder(src, num2, i - num2));
                break;
            }
            Array array2 = Array.CreateInstance(this.elementType, list.Count);

            for (int j = 0; j < array2.Length; j++)
            {
                object obj = list[j];
                if (obj == null)
                {
                    if (this.elementType.IsValueType)
                    {
                        return(null);
                    }
                }
                else if (!this.elementType.Equals(obj.GetType()))
                {
                    return(null);
                }
                array2.SetValue(obj, j);
            }
            return(array2);
        }