示例#1
0
        public IEnumerable <ExpandoObject> Parse(Ssv format)
        {
            var tableNames = format.GetTablesNames().ToArray();

            foreach (var line in format.GetTableData(tableNames[0]))
            {
                IDictionary <string, object> obj = new ExpandoObject();

                var columnNamesLine = format.GetTableColumnNamesLine();
                var columnNames     = columnNamesLine.Values.Select(c => c.Trim()).ToArray();

                for (int i = 0; i < line.Values.Count; i++)
                {
                    var v = line.Values[i].Trim();
                    if (v.StartsWith("\"") && v.EndsWith("\""))
                    {
                        obj[columnNames[i]] = v;
                    }
                    else if (int.TryParse(v, out int result))
                    {
                        obj[columnNames[i]] = result;
                    }
                    else if (double.TryParse(v, out double dresult))
                    {
                        obj[columnNames[i]] = dresult;
                    }

                    obj[columnNames[i]] = v;
                }
                yield return((ExpandoObject)obj);
            }
        }
示例#2
0
        public Ssv Parse(IEnumerable <string> contentLines)
        {
            var ssv = new Ssv();

            var previousLine = new Ssv.Line();

            foreach (var line in contentLines)
            {
                //empty
                if (line == string.Empty)
                {
                    previousLine = ssv.InsertEmptyLine();
                    continue;
                }

                //table
                if (line.TrimStart().StartsWith(SsvNotation.TableStartMark))
                {
                    var name = line.Trim().Replace(SsvNotation.TableStartMark, string.Empty).Replace(SsvNotation.TableEndMak, string.Empty);
                    previousLine = ssv.InsertTableNameLine(name);
                    continue;
                }

                //header
                if (line.TrimStart().StartsWith(SsvNotation.HeaderStartMark))
                {
                    var columnNames    = line.Trim().Replace(SsvNotation.HeaderStartMark, string.Empty).Replace(SsvNotation.HeaderEndMark, string.Empty);
                    var columnNamesRow = ParseHeaderRowLine(columnNames, previousLine.LineType);
                    previousLine = ssv.Insert(columnNamesRow);
                    continue;
                }

                //comment
                if (line.TrimStart().StartsWith(SsvNotation.RowExcludeMark))
                {
                    var coment = line.Trim().Replace(SsvNotation.RowBlockExcludeStartMark, string.Empty);
                    ssv.Insert(new Ssv.CommentLine(coment));
                    continue;
                }

                //data
                var dataRow = ParseDataLine(line);
                ssv.Insert(dataRow);
            }

            return(ssv);
        }
示例#3
0
        public string Format(Ssv ssv)
        {
            var sb = new StringBuilder();

            foreach (var line in ssv.Lines)
            {
                switch (line.LineType)
                {
                case Ssv.LineType.Data:
                    sb.AppendLine(line.ToString(SsvNotation.ValueDelimiter));
                    continue;

                case Ssv.LineType.Comment:
                    if ((line as Ssv.CommentLine)?.Content != null)
                    {
                        sb.Append(SsvNotation.RowExcludeMark)
                        .AppendLine((line as Ssv.CommentLine).Content);
                    }
                    else
                    {
                        sb.AppendLine();
                    }
                    continue;

                case Ssv.LineType.TableName:
                    sb.Append(SsvNotation.TableStartMark)
                    .Append(line.Name)
                    .AppendLine(SsvNotation.TableEndMak);
                    continue;

                case Ssv.LineType.HeaderColumns:
                case Ssv.LineType.Header:
                    sb.Append(SsvNotation.HeaderStartMark)
                    .Append(line.ToString(SsvNotation.ValueDelimiter))
                    .AppendLine(SsvNotation.HeaderEndMark);
                    continue;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            sb.Remove(sb.Length - SsvNotation.LineDelimiter.Length, SsvNotation.LineDelimiter.Length);

            return(sb.ToString());
        }
示例#4
0
        public IEnumerable <T> Parse(Ssv format, Func <T> initialObjectProvider = null)
        {
            if (!Map.TryGetValue(typeof(T), out var tableMap))
            {
                if (!Map.TryGetValue(null, out tableMap))
                {
                    throw new Exception();
                }
            }

            var tableName  = tableMap.Item1;
            var properties = tableMap.Item2;

            foreach (var line in format.GetTableData(tableName))
            {
                var obj = initialObjectProvider == null?Activator.CreateInstance <T>() : initialObjectProvider();

                for (int i = 0; i < line.Values.Count; i++)
                {
                    if (i >= properties.Count)
                    {
                        break;
                    }
                    var propertyName = properties[i];
                    if (propertyName == null)
                    {
                        continue;
                    }
                    if (propertyName == string.Empty)
                    {
                        //take name from ssv
                        //todo
                    }
                    var propertyInfo       = obj.GetType().GetProperty(propertyName);
                    var formattedValue     = line.Values[i].Trim();
                    var formattedValueType = Convert.ChangeType(formattedValue, propertyInfo.PropertyType, CultureInfo.InvariantCulture);
                    propertyInfo.SetValue(obj, formattedValueType);
                }
                yield return(obj);
            }
        }