Exemple #1
0
        public static object CsvEntryToObject(int index, GetCsvCell getCsvCell, Type type)
        {
            var retval = Activator.CreateInstance(type);
            var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsArray)
                {
                    ArrayList arrayContents = new ArrayList();
                    try
                    {
                        int       i = 0;
                        const int ArrayMaxLength = 9999;
                        while (i < ArrayMaxLength)
                        {
                            arrayContents.Add(getCsvCell(index, field.Name + "[" + i + "]"));
                            ++i;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    if (arrayContents.Count == 0)
                    {
                        continue;
                    }
                    Type elementType = field.FieldType.GetElementType();
                    var  array       = (Array)Array.CreateInstance(elementType, arrayContents.Count);
                    for (int i = 0; i < arrayContents.Count; ++i)
                    {
                        try
                        {
                            array.SetValue(Convert.ChangeType(arrayContents[i], elementType), i);
                        }
                        catch (System.Exception)
                        {
                        }
                    }
                    field.SetValue(retval, array);
                    continue;
                }

                try
                {
                    string cell = getCsvCell(index, field.Name);
                    field.SetValue(retval, Convert.ChangeType(cell, field.FieldType));
                }
                catch (System.Exception)
                {
#if UNITY
                    UnityEngine.Debug.LogFormat("Couldn't set object.{0} = ({1})'{2}'", field.Name, field.FieldType.Name, cell);
#endif
                }
            }
            return(retval);
        }
Exemple #2
0
        internal static GetCsvCell _readCsv(string text, out int length, bool transpose)
        {
            var lines = Regex.Split(text, s_LINE_SPLIT_RE);

            if (lines.Length <= 1)
            {
                length = 0;
                return(delegate(int row, string column)
                {
                    throw new System.ArgumentOutOfRangeException("No data in this CSV");
                });
            }

            var firstLine = Regex.Split(lines[0], s_SPLIT_RE);

            var labelToIndex = new Dictionary <string, int>();

            string[,] data;

            if (transpose)
            {
                data = new string[firstLine.Length - 1, lines.Length];
            }
            else
            {
                data = new string[lines.Length - 1, firstLine.Length];
                for (var i = 0; i < firstLine.Length; ++i)
                {
                    labelToIndex[firstLine[i]] = i;
                }
            }

            var lineSplitRegex = new Regex(s_SPLIT_RE);
            var currentRow     = 0;

            for (var i = 0; i < lines.Length; i++)
            {
                if (!transpose && i == 0)
                {
                    continue;
                }
                var values = lineSplitRegex.Split(lines[i]);
                if (values.Length == 0 || values[0] == "")
                {
                    continue;
                }
                for (var j = 0; j < firstLine.Length && j < values.Length; ++j)
                {
                    string value = values[j];
                    if (transpose && j == 0)
                    {
                        labelToIndex[value] = i;
                        continue;
                    }
                    value = value.Trim(s_TRIM_CHARS).Replace("\\", "");
                    if (transpose)
                    {
                        data[j - 1, i] = value;
                    }
                    else
                    {
                        data[currentRow, j] = value;
                    }
                }
                ++currentRow;
            }

            if (transpose)
            {
                length = firstLine.Length - 1;
            }
            else
            {
                length = currentRow;
            }

            int        lengthCaptured = length;
            GetCsvCell retval         = delegate(int index, string label)
            {
                if (index < 0 || index >= lengthCaptured)
                {
                    throw new System.ArgumentOutOfRangeException("Index out of range");
                }
                int labelIndex;
                if (!labelToIndex.TryGetValue(label, out labelIndex))
                {
                    throw new System.ArgumentException("Unknown label: " + label);
                }
                return(data[index, labelIndex]);
            };

            return(retval);
        }
    public static GetCsvCell ReadCsv(string text, out int rows)
    {
        var lines = Regex.Split(text, LINE_SPLIT_RE);

        if (lines.Length <= 1)
        {
            rows = 0;
            return(delegate(int row, string column)
            {
                throw new System.ArgumentOutOfRangeException("No data in this CSV");
            });
        }

        var header = Regex.Split(lines[0], SPLIT_RE);

        var columnNameToIndex = new Dictionary <string, int>();
        var data = new string[lines.Length - 1, header.Length];

        for (var i = 0; i < header.Length; ++i)
        {
            columnNameToIndex[header[i]] = i;
        }

        var lineSplitRegex = new Regex(SPLIT_RE);
        var currentRow     = 0;

        for (var i = 1; i < lines.Length; i++)
        {
            var values = lineSplitRegex.Split(lines[i]);
            if (values.Length == 0 || values[0] == "")
            {
                continue;
            }
            for (var j = 0; j < header.Length && j < values.Length; ++j)
            {
                string value = values[j];
                value = value.Trim(TRIM_CHARS).Replace("\\", "");
                data[currentRow, j] = value;
            }

            ++currentRow;
        }

        rows = currentRow;

        GetCsvCell retval = delegate(int row, string columnName)
        {
            if (row < 0 || row >= currentRow)
            {
                throw new System.ArgumentOutOfRangeException("Row index out of range");
            }
            int columnIndex;
            if (!columnNameToIndex.TryGetValue(columnName, out columnIndex))
            {
                throw new System.ArgumentException("Unknown column name: " + columnName);
            }
            return(data[row, columnIndex]);
        };

        return(retval);
    }