示例#1
0
        public static RootEntry parse(String data)
        {
            RootEntry result = new RootEntry();

            String[] lines = data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            var stack = new Stack <List <Entry> >();

            stack.Push(result.menu);
            Entry parent = null;

            bool isRoot = true;

            foreach (var line in lines)
            {
                if (line == "")
                {
                    continue;
                }

                var entry = parseLine(line);

                if (isRoot && entry.isSeparator)
                {
                    isRoot = false;
                    continue;
                }

                if (isRoot)
                {
                    result.children.Add(entry);
                }
                else
                {
                    var currentDepth = stack.Count - 1;

                    if (entry.depth < currentDepth)
                    {
                        for (var i = currentDepth; i > entry.depth; i--)
                        {
                            stack.Pop();
                        }
                    }
                    else if (entry.depth > currentDepth && parent != null)
                    {
                        stack.Push(parent.children);
                    }

                    stack.First().Add(entry);
                }
                parent = entry;
            }

            return(result);
        }
示例#2
0
        public RootEntry ExecuteCommand()
        {
            var output = new StringBuilder();
            var error  = new StringBuilder();

            var processInfo = new ProcessStartInfo("cmd.exe", "/Q /c " + this.file);

            processInfo.CreateNoWindow         = true;
            processInfo.UseShellExecute        = false;
            processInfo.RedirectStandardError  = true;
            processInfo.RedirectStandardOutput = true;
            processInfo.WorkingDirectory       = System.IO.Path.GetDirectoryName(file);

            var process = Process.Start(processInfo);

            int currentDepth = 0;

            process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
            {
                output.AppendLine(e.Data);
            };

            process.BeginOutputReadLine();

            process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
            {
                error.AppendLine(e.Data);
            };

            process.BeginErrorReadLine();

            process.WaitForExit();

            RootEntry res;

            Debug.WriteLine(error.ToString());
            if (process.ExitCode == 0)
            {
                res = OutputParser.parse(output.ToString());
            }
            else
            {
                res              = new RootEntry();
                res.error        = true;
                res.errorMessage = error.ToString();
            }

            process.Close();

            return(res);
        }
示例#3
0
        private void OnTimedEvent(object stateInfo)
        {
            RootEntry root = exe.ExecuteCommand();

            this.Dispatcher.Invoke(() =>
            {
                outputLabel.ContextMenu = new ContextMenu();
                if (root.error)
                {
                    outputLabel.Content = "Error \u26A0";
                    var item            = new MenuItem();
                    item.Header         = root.errorMessage;
                    outputLabel.ContextMenu.Items.Add(item);
                }
                else
                {
                    outputLabel.Content = root.children[0].text;
                    this.buildMenu(outputLabel.ContextMenu.Items, root.menu);
                }
            }
                                   );
        }