Exemplo n.º 1
0
 public FileSelector(string selectionCriteria, bool traverseDirectoryReparsePoints)
 {
     if (!string.IsNullOrEmpty(selectionCriteria))
     {
         this._Criterion = FileSelector._ParseCriterion(selectionCriteria);
     }
     this.TraverseReparsePoints = traverseDirectoryReparsePoints;
 }
Exemplo n.º 2
0
        private static SelectionCriterion _ParseCriterion(string s)
        {
            if (s == null)
            {
                return(null);
            }
            s = FileSelector.NormalizeCriteriaExpression(s);
            if (s.IndexOf(" ") == -1)
            {
                s = "name = " + s;
            }
            string[] array = s.Trim().Split(new char[]
            {
                ' ',
                '\t'
            });
            if (array.Length < 3)
            {
                throw new ArgumentException(s);
            }
            SelectionCriterion selectionCriterion  = null;
            Stack <FileSelector.ParseState> stack  = new Stack <FileSelector.ParseState>();
            Stack <SelectionCriterion>      stack2 = new Stack <SelectionCriterion>();

            stack.Push(FileSelector.ParseState.Start);
            int i = 0;

            while (i < array.Length)
            {
                string text  = array[i].ToLower();
                string text2 = text;
                if (text2 != null)
                {
                    if (FileSelector.< > f__switch$map0 == null)
                    {
                        FileSelector.< > f__switch$map0 = new Dictionary <string, int>(14)
                        {
                            {
                                "and",
                                0
                            },
                            {
                                "xor",
                                0
                            },
                            {
                                "or",
                                0
                            },
                            {
                                "(",
                                1
                            },
                            {
                                ")",
                                2
                            },
                            {
                                "atime",
                                3
                            },
                            {
                                "ctime",
                                3
                            },
                            {
                                "mtime",
                                3
                            },
                            {
                                "length",
                                4
                            },
                            {
                                "size",
                                4
                            },
                            {
                                "filename",
                                5
                            },
                            {
                                "name",
                                5
                            },
                            {
                                "type",
                                6
                            },
                            {
                                string.Empty,
                                7
                            }
                        };
                    }
                    int num;
                    if (FileSelector.< > f__switch$map0.TryGetValue(text2, out num))
                    {
                        FileSelector.ParseState parseState;
                        switch (num)
                        {
                        case 0:
                        {
                            parseState = stack.Peek();
                            if (parseState != FileSelector.ParseState.CriterionDone)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            if (array.Length <= i + 3)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            LogicalConjunction conjunction = (LogicalConjunction)Enum.Parse(typeof(LogicalConjunction), array[i].ToUpper(), true);
                            selectionCriterion = new CompoundCriterion
                            {
                                Left        = selectionCriterion,
                                Right       = null,
                                Conjunction = conjunction
                            };
                            stack.Push(parseState);
                            stack.Push(FileSelector.ParseState.ConjunctionPending);
                            stack2.Push(selectionCriterion);
                            break;
                        }

                        case 1:
                            parseState = stack.Peek();
                            if (parseState != FileSelector.ParseState.Start && parseState != FileSelector.ParseState.ConjunctionPending && parseState != FileSelector.ParseState.OpenParen)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            if (array.Length <= i + 4)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            stack.Push(FileSelector.ParseState.OpenParen);
                            break;

                        case 2:
                            parseState = stack.Pop();
                            if (stack.Peek() != FileSelector.ParseState.OpenParen)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            stack.Pop();
                            stack.Push(FileSelector.ParseState.CriterionDone);
                            break;

                        case 3:
                        {
                            if (array.Length <= i + 2)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            DateTime dateTime;
                            try
                            {
                                dateTime = DateTime.ParseExact(array[i + 2], "yyyy-MM-dd-HH:mm:ss", null);
                            }
                            catch (FormatException)
                            {
                                try
                                {
                                    dateTime = DateTime.ParseExact(array[i + 2], "yyyy/MM/dd-HH:mm:ss", null);
                                }
                                catch (FormatException)
                                {
                                    try
                                    {
                                        dateTime = DateTime.ParseExact(array[i + 2], "yyyy/MM/dd", null);
                                    }
                                    catch (FormatException)
                                    {
                                        try
                                        {
                                            dateTime = DateTime.ParseExact(array[i + 2], "MM/dd/yyyy", null);
                                        }
                                        catch (FormatException)
                                        {
                                            dateTime = DateTime.ParseExact(array[i + 2], "yyyy-MM-dd", null);
                                        }
                                    }
                                }
                            }
                            dateTime           = DateTime.SpecifyKind(dateTime, DateTimeKind.Local).ToUniversalTime();
                            selectionCriterion = new TimeCriterion
                            {
                                Which    = (WhichTime)Enum.Parse(typeof(WhichTime), array[i], true),
                                Operator = (ComparisonOperator)EnumUtil.Parse(typeof(ComparisonOperator), array[i + 1]),
                                Time     = dateTime
                            };
                            i += 2;
                            stack.Push(FileSelector.ParseState.CriterionDone);
                            break;
                        }

                        case 4:
                        {
                            if (array.Length <= i + 2)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            string text3 = array[i + 2];
                            long   size;
                            if (text3.ToUpper().EndsWith("K"))
                            {
                                size = long.Parse(text3.Substring(0, text3.Length - 1)) * 1024L;
                            }
                            else if (text3.ToUpper().EndsWith("KB"))
                            {
                                size = long.Parse(text3.Substring(0, text3.Length - 2)) * 1024L;
                            }
                            else if (text3.ToUpper().EndsWith("M"))
                            {
                                size = long.Parse(text3.Substring(0, text3.Length - 1)) * 1024L * 1024L;
                            }
                            else if (text3.ToUpper().EndsWith("MB"))
                            {
                                size = long.Parse(text3.Substring(0, text3.Length - 2)) * 1024L * 1024L;
                            }
                            else if (text3.ToUpper().EndsWith("G"))
                            {
                                size = long.Parse(text3.Substring(0, text3.Length - 1)) * 1024L * 1024L * 1024L;
                            }
                            else if (text3.ToUpper().EndsWith("GB"))
                            {
                                size = long.Parse(text3.Substring(0, text3.Length - 2)) * 1024L * 1024L * 1024L;
                            }
                            else
                            {
                                size = long.Parse(array[i + 2]);
                            }
                            selectionCriterion = new SizeCriterion
                            {
                                Size     = size,
                                Operator = (ComparisonOperator)EnumUtil.Parse(typeof(ComparisonOperator), array[i + 1])
                            };
                            i += 2;
                            stack.Push(FileSelector.ParseState.CriterionDone);
                            break;
                        }

                        case 5:
                        {
                            if (array.Length <= i + 2)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            ComparisonOperator comparisonOperator = (ComparisonOperator)EnumUtil.Parse(typeof(ComparisonOperator), array[i + 1]);
                            if (comparisonOperator != ComparisonOperator.NotEqualTo && comparisonOperator != ComparisonOperator.EqualTo)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            string text4 = array[i + 2];
                            if (text4.StartsWith("'") && text4.EndsWith("'"))
                            {
                                text4 = text4.Substring(1, text4.Length - 2).Replace("\u0006", " ");
                            }
                            selectionCriterion = new NameCriterion
                            {
                                MatchingFileSpec = text4,
                                Operator         = comparisonOperator
                            };
                            i += 2;
                            stack.Push(FileSelector.ParseState.CriterionDone);
                            break;
                        }

                        case 6:
                        {
                            if (array.Length <= i + 2)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            ComparisonOperator comparisonOperator2 = (ComparisonOperator)EnumUtil.Parse(typeof(ComparisonOperator), array[i + 1]);
                            if (comparisonOperator2 != ComparisonOperator.NotEqualTo && comparisonOperator2 != ComparisonOperator.EqualTo)
                            {
                                throw new ArgumentException(string.Join(" ", array, i, array.Length - i));
                            }
                            selectionCriterion = new TypeCriterion
                            {
                                AttributeString = array[i + 2],
                                Operator        = comparisonOperator2
                            };
                            i += 2;
                            stack.Push(FileSelector.ParseState.CriterionDone);
                            break;
                        }

                        case 7:
                            stack.Push(FileSelector.ParseState.Whitespace);
                            break;

                        default:
                            goto IL_7BA;
                        }
                        parseState = stack.Peek();
                        if (parseState == FileSelector.ParseState.CriterionDone)
                        {
                            stack.Pop();
                            if (stack.Peek() == FileSelector.ParseState.ConjunctionPending)
                            {
                                while (stack.Peek() == FileSelector.ParseState.ConjunctionPending)
                                {
                                    CompoundCriterion compoundCriterion = stack2.Pop() as CompoundCriterion;
                                    compoundCriterion.Right = selectionCriterion;
                                    selectionCriterion      = compoundCriterion;
                                    stack.Pop();
                                    parseState = stack.Pop();
                                    if (parseState != FileSelector.ParseState.CriterionDone)
                                    {
                                        throw new ArgumentException("??");
                                    }
                                }
                            }
                            else
                            {
                                stack.Push(FileSelector.ParseState.CriterionDone);
                            }
                        }
                        if (parseState == FileSelector.ParseState.Whitespace)
                        {
                            stack.Pop();
                        }
                        i++;
                        continue;
                    }
                }
IL_7BA:
                throw new ArgumentException("'" + array[i] + "'");
            }
            return(selectionCriterion);
        }