예제 #1
0
 /// Parse string to a number. 10 and hex numbers (like 0x222) are allowed. Suffixes like 20.3f are also allowed.
 public static ValueType ParseNumber(string str)
 {
     if (str == null)
     {
         throw new ArgumentNullException("str");
     }
     using (var sr = new ParsingReader(str))
     {
         sr.SkipWhiteSpaceAndComments();
         ValueType o = sr.ReadNumber();
         if (o == null)
         {
             throw new ParsingException("Invalid numeric expression at " + sr.ReadLine());
         }
         sr.SkipWhiteSpaceAndComments();
         if (sr.Peek() != -1)
         {
             throw new ParsingException("Invalid numeric expression, unexpected characters at " + sr.ReadLine());
         }
         return(o);
     }
 }
예제 #2
0
파일: RowSet.cs 프로젝트: xsharper/xsharper
        private IEnumerable <Vars> getTransformedRowsInternal(ColumnsInfo ci, ParsingReader csvReader)
        {
            int rowNo = 0;

            string quote     = Context.TransformStr(Quote, Transform);
            string separator = Context.TransformStr(Separator, Transform);
            char?  quoteChar = string.IsNullOrEmpty(quote) ? (char?)null : quote[0];
            bool   trim      = (Options & RowsetOptions.Trim) != 0;

            // Read the rest of CSV, if available
            if (csvReader != null)
            {
                // If there are no columns, treat data as the list of strings
                if (ci.Count == 0)
                {
                    string s;
                    while ((s = csvReader.ReadLine()) != null)
                    {
                        if ((Options & RowsetOptions.Trim) == RowsetOptions.Trim)
                        {
                            s = s.Trim();
                        }
                        if (string.IsNullOrEmpty(s) && (Options & RowsetOptions.IgnoreEmpty) == RowsetOptions.IgnoreEmpty)
                        {
                            continue;
                        }

                        Vars r = new Vars();
                        r[string.Empty] = s;

                        Context.CheckAbort();

                        yield return(r);
                    }
                }
                else
                {
                    // We have columns, and therefore a complete CSV
                    string[] data;
                    while ((data = Utils.ReadCsvRow(csvReader, quoteChar, separator[0], trim)) != null)
                    {
                        if ((Options & RowsetOptions.IgnoreEmpty) != 0 && (data.Length == 0 || (data.Length == 1 && data[0].Length == 0)))
                        {
                            continue;
                        }
                        rowNo++;

                        if (data.Length > ci.Count)
                        {
                            throw new ScriptRuntimeException(string.Format("{0} columns expected in row #{1}, but {2} found.",
                                                                           ci.Count, rowNo, data.Length));
                        }
                        Vars r = new Vars();
                        for (int i = 0; i < Math.Min(ci.Count, data.Length); ++i)
                        {
                            object o = Context.Transform(data[i], Verbatim ? TransformRules.None : Transform);
                            if (i < ci.Count)
                            {
                                r[ci[i].Name] = ci[i].AdjustType(o);
                            }
                            else
                            {
                                r[ci[i].Name] = o;
                            }
                        }

                        ci.ApplyDefaults(r);
                        Context.CheckAbort();

                        var c = checkWhere(r);
                        if (c == null)
                        {
                            yield break;
                        }
                        if (c.Value)
                        {
                            yield return(r);
                        }
                    }
                }
            }

            // Rowset ID
            string id = Context.TransformStr(RowsetId, Transform);

            if (!string.IsNullOrEmpty(id))
            {
                var rs = Context.Find <RowSet>(id, true);
                foreach (var v in rs.GetData())
                {
                    rowNo++;
                    Vars sv1 = null;
                    if (ci.Count == 0)
                    {
                        sv1 = v;
                    }
                    else
                    {
                        sv1 = new Vars();
                        Context.ExecuteWithVars(() =>
                        {
                            foreach (var col in ci)
                            {
                                sv1.Set(col.Name, col.AdjustType(v.GetOrDefault(col.Name, null)));
                            }
                            return(null);
                        }, v, null);
                    }
                    ci.ApplyDefaults(sv1);
                    Context.CheckAbort();

                    var c = checkWhere(sv1);
                    if (c == null)
                    {
                        yield break;
                    }
                    if (c.Value)
                    {
                        yield return(sv1);
                    }
                }
            }

            // Try XmlDoc
            id = Context.TransformStr(XmlDocId, Transform);
            if (!string.IsNullOrEmpty(id))
            {
                XmlDoc doc = Context.Find <XmlDoc>(id, true);
                foreach (XmlNode n in doc.Nodes(Context.TransformStr(XPath, Transform)))
                {
                    Vars sv = new Vars();
                    rowNo++;
                    if (ci.Count > 0)
                    {
                        foreach (var col in ci)
                        {
                            var node = n.SelectSingleNode(col.Value);
                            if (node != null)
                            {
                                sv[col.Name] = col.AdjustType(node.Value);
                            }
                        }
                    }
                    else
                    {
                        foreach (XmlAttribute attr in n.Attributes)
                        {
                            sv[attr.LocalName] = attr.Value;
                        }
                        sv[string.Empty] = n.InnerText;
                    }
                    ci.ApplyDefaults(sv);
                    Context.CheckAbort();

                    var c = checkWhere(sv);
                    if (c == null)
                    {
                        yield break;
                    }
                    if (c.Value)
                    {
                        yield return(sv);
                    }
                }
            }

            foreach (var row in Rows)
            {
                rowNo++;
                Vars sv = new Vars();
                foreach (Var v in row)
                {
                    var val = (Verbatim) ? v.Value : Context.Transform(v.Value, Transform);
                    sv.Set(v.Name, ci.AdjustType(v.Name, val));
                }
                ci.ApplyDefaults(sv);
                Context.CheckAbort();

                var c = checkWhere(sv);
                if (c == null)
                {
                    yield break;
                }
                if (c.Value)
                {
                    yield return(sv);
                }
            }
        }
예제 #3
0
파일: RowSet.cs 프로젝트: xsharper/xsharper
        private IEnumerable <Vars> getTransformedRows(string columnsOverride, string sortOverride, string whereExpression)
        {
            bool overrideSort = (sortOverride != null);

            if (!overrideSort)
            {
                sortOverride = Context.TransformStr(SortColumns, Transform);
            }

            //
            ColumnsInfo ci = new ColumnsInfo();

            // Load CSV, if present
            string text      = GetTransformedValueStr();
            string quote     = Context.TransformStr(Quote, Transform);
            string separator = Context.TransformStr(Separator, Transform);
            char?  quoteChar = string.IsNullOrEmpty(quote) ? (char?)null : quote[0];
            bool   trim      = (Options & RowsetOptions.Trim) != 0;
            string colNames  = Context.TransformStr(Columns, Transform);;
            // Read additional column names from the first CSV row, if specified
            ParsingReader csvReader = null;

            if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text.Trim()))
            {
                csvReader = new ParsingReader(new StringReader(text));
                if ((Options & RowsetOptions.Header) == RowsetOptions.Header)
                {
                    csvReader.SkipWhiteSpace();
                    colNames = Context.TransformStr(csvReader.ReadLine(), Verbatim ? TransformRules.None : Transform);
                }
            }

            char sep = string.IsNullOrEmpty(separator) ? '\0' : separator[0];

            // Add these columns
            if (!string.IsNullOrEmpty(colNames))
            {
                ci.AddParsed(Context, colNames, quoteChar, sep, trim);
            }

            // Add extra columns
            if (Cols != null)
            {
                foreach (var col in Cols)
                {
                    ColumnInfo cc = new ColumnInfo();
                    cc.Name      = Context.TransformStr(col.Name, col.Transform);
                    cc.Value     = Context.TransformStr(col.Value, col.Transform);
                    cc.IsDefault = (col.Default != null);
                    if (cc.IsDefault)
                    {
                        cc.Default = Context.Transform(col.Default, col.Transform);
                    }
                    object t = Context.Transform(col.Type, col.Transform);
                    if (t != null)
                    {
                        if (t is Type)
                        {
                            cc.Type = (Type)t;
                        }
                        else
                        {
                            cc.Type = Context.FindType(t.ToString());
                        }
                    }
                    ci.Add(cc);
                }
            }

            SortInfos si = new SortInfos(sortOverride, quoteChar, sep, trim);

            if (!overrideSort && SortCols != null)
            {
                foreach (var col in SortCols)
                {
                    var ss = new SortInfo
                    {
                        Name = Context.TransformStr(col.Name, col.Transform),
                        Sort = col.Sort
                    };

                    var cmp = Context.Transform(col.Comparer, Transform);
                    if (cmp != null)
                    {
                        object c1 = null;
                        if (cmp is Type)
                        {
                            ss.Comparer = (IComparer)Utils.CreateInstance((Type)cmp);
                        }
                        else if (cmp is string && !string.IsNullOrEmpty((string)cmp))
                        {
                            string smp = (string)cmp;
                            if (smp.ToUpperInvariant() == "IGNORECASE" || smp.ToUpperInvariant() == "NOCASE" || smp.ToUpperInvariant() == "IC")
                            {
                                ss.Comparer = StringComparer.CurrentCultureIgnoreCase;
                            }
                            else if (Utils.TryGetProperty(null, typeof(StringComparer), smp, null, false, out c1))
                            {
                                ss.Comparer = (IComparer)c1;
                            }
                            else
                            {
                                ss.Comparer = (IComparer)Utils.CreateInstance(Context.FindType(smp));
                            }
                        }
                        else
                        {
                            ss.Comparer = (IComparer)cmp;
                        }
                    }
                    si.Add(ss);
                }
            }

            var ret = si.SortAndFilter(Context, columnsOverride, whereExpression, getTransformedRowsInternal(ci, csvReader));

            if (columnsOverride == null)
            {
                return(ret);
            }


            ci = new ColumnsInfo();
            ci.AddParsed(Context, columnsOverride, quoteChar, separator[0], trim);
            return(ci.Process(ret));
        }