示例#1
0
        public static bool MatchOneOf(string pattern, Collection <String> inputs)
        {
            foreach (var input in inputs)
            {
                if (!WildcardSearch.Match(pattern, input))
                {
                    continue;
                }

                return(true);
            }

            return(false);
        }
示例#2
0
      public string[] GetChildPaths( string searchPattern, bool recursive )
      {
         var children = this.GetChildren();

         if ( children.Length == 0 )
         {
            var path = this.GetPath();
            return WildcardSearch.Match( searchPattern, path ) ? new string[] { path } : new string[0];
         }

         var paths = new List<string>();
         foreach ( var child in children )
         {
            if ( !child.IsContainer || recursive )
            {
               var childPaths = child.GetChildPaths( searchPattern, recursive );
               paths.AddRange( childPaths );
            }
         }

         return paths.ToArray();
      }
示例#3
0
        static void Main(string[] args)
        {
            CommandLine <CLArguments> commandLine = null;

            try
            {
                commandLine = new CommandLine <CLArguments>();
                var arguments = commandLine.Parse(args);

                using (var devices = WpdDeviceCollection.Create())
                {
                    if (devices.Count == 0)
                    {
                        Console.WriteLine("No devices found.");
                    }
                    else if (arguments.CommandDiscover)
                    {
                        foreach (var device in devices)
                        {
                            Console.WriteLine("\"{0}\"{1}", device.Name, arguments.ShowDeviceID ? " " + device.DeviceID : string.Empty);
                        }
                    }
                    else if (arguments.CommandInfo)
                    {
                        var device = devices.Find(arguments.InfoDeviceName, false);
                        if (device == null)
                        {
                            Console.WriteLine("No device found with the name \"{0}\".", arguments.InfoDeviceName);
                        }
                        else
                        {
                            if (arguments.InfoCopyID)
                            {
                                ClipboardApi.Copy(device.DeviceID, true);
                            }

                            Console.WriteLine("Found {0} properties.", device.Properties.Count);

                            for (var i = 0; i < device.Properties.Count; i++)
                            {
                                Console.WriteLine("{0} {1} ({2}): {3}", i.ToString("D" + device.Properties.Count.CountDigits().ToString()), PortableDevicePKeys.FindKeyName(device.Properties[i].Key), device.Properties[i].Type, device.Properties[i].Value);
                            }
                        }
                    }
                    else if (arguments.CommandUpload)
                    {
                        var device = devices.Find(arguments.UploadDeviceName, false);
                        if (device == null)
                        {
                            Console.WriteLine("No device found with the name \"{0}\".", arguments.UploadDeviceName);
                        }
                        else
                        {
                            Console.WriteLine("Uploading...");

                            var components   = WildcardSearch.SplitPattern(arguments.UploadSourcePath);
                            var targetObject = device.ObjectFromPath(arguments.UploadTargetPath, arguments.CreatePath);

                            var commander = new DeviceCommander(device);
                            commander.DataCopyStarted += (sender, e) =>
                            {
                                Console.WriteLine("\r{0}", e.SourcePath);
                            };
                            commander.DataCopied += (sender, e) =>
                            {
                                var percent = 100.0 * ((double)e.CopiedBytes / (double)e.MaxBytes);
                                Console.Write("\r  {0}/{1} bytes ({2}%)", e.CopiedBytes, e.MaxBytes, percent.ToString("G3"));
                            };
                            commander.DataCopyEnded += (sender, e) =>
                            {
                            };
                            commander.DataCopyError += (sender, e) =>
                            {
                                Console.WriteLine("\r" + e.Exception.Message);
                            };

                            Console.WriteLine();
                            if (String.IsNullOrEmpty(components.Item2))
                            {
                                commander.Upload(targetObject, components.Item1, arguments.Overwrite);
                            }
                            else
                            {
                                commander.Upload(targetObject, components.Item1, arguments.Overwrite, components.Item2, arguments.Recursive, arguments.Flatten);
                            }
                            Console.WriteLine("\rCompleted                                                                     ");
                        }
                    }
                    else if (arguments.CommandDownload)
                    {
                        var device = devices.Find(arguments.DownloadDeviceName, false);
                        if (device == null)
                        {
                            Console.WriteLine("No device found with the name \"{0}\".", arguments.DownloadDeviceName);
                        }
                        else
                        {
                            Console.WriteLine("Downloading...");

                            var components   = WildcardSearch.SplitPattern(arguments.DownloadSourcePath);
                            var sourceObject = device.ObjectFromPath(components.Item1, false);

                            if (!System.IO.Directory.Exists(arguments.DownloadTargetPath))
                            {
                                if (arguments.CreatePath)
                                {
                                    System.IO.Directory.CreateDirectory(arguments.DownloadTargetPath);
                                }
                                else
                                {
                                    throw new System.IO.DirectoryNotFoundException(String.Format("The directory \"{0}\" was not found.", arguments.DownloadTargetPath));
                                }
                            }

                            var commander = new DeviceCommander(device);
                            commander.DataCopyStarted += (sender, e) =>
                            {
                                Console.WriteLine("\r{0}", e.SourcePath);
                            };
                            commander.DataCopied += (sender, e) =>
                            {
                                var percent = 100.0 * ((double)e.CopiedBytes / (double)e.MaxBytes);
                                Console.Write("\r  {0}/{1} bytes ({2}%)", e.CopiedBytes, e.MaxBytes, percent.ToString("G3"));
                            };
                            commander.DataCopyEnded += (sender, e) =>
                            {
                            };
                            commander.DataCopyError += (sender, e) =>
                            {
                                Console.WriteLine("\r" + e.Exception.Message);
                            };

                            Console.WriteLine();
                            if (String.IsNullOrEmpty(components.Item2))
                            {
                                commander.Download(sourceObject, arguments.DownloadTargetPath, arguments.Overwrite);
                            }
                            else
                            {
                                commander.Download(sourceObject, arguments.DownloadTargetPath, arguments.Overwrite, components.Item2, arguments.Recursive, arguments.Flatten);
                            }
                            Console.WriteLine("\rCompleted                                                                     ");
                        }
                    }
                    else
                    {
                        Console.WriteLine(commandLine.Help());
                    }
                }
            }
            catch (CommandLineDeclarationException ex)
            {
                if (commandLine != null)
                {
                    Console.WriteLine(commandLine.Help());
                }
                System.Diagnostics.Trace.WriteLine(ex);
                Console.WriteLine(ex);
            }
            catch (CommandLineException ex)
            {
                if (commandLine != null)
                {
                    Console.WriteLine(commandLine.Help());
                }
                System.Diagnostics.Trace.WriteLine(ex);
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                Console.WriteLine(ex);
            }
        }
示例#4
0
        public static bool Match(string pattern, string input)
        {
            if (pattern == null || pattern.Length == 0)
            {
                return(false);
            }

            if (input == null || input.Length == 0)
            {
                return(false);
            }

            var nextIndex    = 0;
            var patternSlice = string.Empty;

            while (pattern.Length > 0)
            {
                if (pattern[0] == '*')
                {
                    pattern = WildcardSearch.LStrip(pattern, "*?");
                    if (pattern.Length == 0)
                    {
                        return(true);
                    }

                    nextIndex    = WildcardSearch.GetNextInterestingCharacter(pattern);
                    patternSlice = pattern.Substring(0, nextIndex);

                    if (input.IndexOf(patternSlice) >= 0)
                    {
                        pattern = pattern.Substring(nextIndex);
                        var inputIndex = input.LastIndexOf(patternSlice);
                        input = input.Substring(inputIndex + patternSlice.Length);
                    }
                    else
                    {
                        return(false);
                    }
                }

                else if (pattern[0] == '?')
                {
                    if (input.Length < 1)
                    {
                        return(false);
                    }
                    pattern = pattern.Substring(1);
                    input   = input.Substring(1);
                }
                else
                {
                    nextIndex    = GetNextInterestingCharacter(pattern);
                    patternSlice = pattern.Substring(0, nextIndex);
                    pattern      = pattern.Substring(nextIndex);

                    if (input.Length < nextIndex)
                    {
                        return(false);
                    }

                    var matchSlice = input.Substring(0, nextIndex);

                    if (!matchSlice.Equals(patternSlice))
                    {
                        return(false);
                    }

                    input = input.Substring(nextIndex);
                }
            }

            return(input.Length == 0);
        }