Пример #1
0
 /// <summary>
 /// Load content depend on parsed settings
 /// </summary>
 public void LoadContent()
 {
     IOHandler io = new IOHandler();
     if (this.Input.Mode == InputMode.File)
     {
         if (this.IsBinaryLoad)
         {
             this.Input.BinaryContent = io.LoadBin(this.Input.Paths);
         }
         else
         {
             this.Input.Content = io.Load(this.Input.Paths);
         }
     }
 }
Пример #2
0
        /// <summary>
        /// Parse given args array to options object, will clear replace current object
        /// </summary>
        /// <param name="args">Arguments strings</param>
        public void Parse(List<string> args)
        {
            this.Reset();
            this.RawArguments = args;

            // Parse the first group
            //for (int i = 0; i < args.Count; i++)
            //{
            //    if (args[i] != null && args[i].Length > 0 && args[i][0] == '-')
            //    {
            //        string option = args[i].Substring(1).ToLower();
            //        if (option.Length <= 0)
            //        {
            //            option = " ";
            //        }
            //        List<string> optionArgs = new List<string>();
            //        switch (option[0])
            //        {
            //            case 'l':
            //                // If user set the line break...
            //                if (option.Length > 1)
            //                {
            //                    switch (option)
            //                    {
            //                        case "lw":
            //                            this.NewLine = NewLines.Win;
            //                            break;
            //                        case "lm":
            //                            this.NewLine = NewLines.Mac;
            //                            break;
            //                        case "lu":
            //                            this.NewLine = NewLines.Unix;
            //                            break;
            //                        default:
            //                            this.Warnings.Add((i + 1) + "# Unrecognized switch: \"" + option.Substring(1) + "\" in " + args[i]);
            //                            break;
            //                    }
            //                }
            //                else // ...else set it to windows type
            //                {
            //                    this.NewLine = NewLines.Win;
            //                }
            //                break;
            //            default:
            //                break;
            //        }
            //    }
            //}

            // Parse the second group
            for (int i = 0; i < args.Count; i++)
            {
                if ( args[i] != null && args[i].Length > 0 && args[i][0] == '-')
                {
                    List<string> optionArgs = new List<string>();
                    string option = args[i].Substring(1).ToLower();
                    if (option.Length <= 0)
                    {
                        option = " ";
                    }

                    switch (option[0])
                    {
                        case 'i':
                            // If user set the input mode...
                            if (option.Length > 1)
                            {
                                switch (option)
                                {
                                    case "if":
                                        this.Input.Mode = InputMode.File;
                                        break;
                                    case "is":
                                        this.Input.Mode = InputMode.String;
                                        break;
                                    default:
                                        this.Errors.Add("Invalid input switch: \"" + option.Substring(1) + "\" in " + args[i]);
                                        break;
                                }
                            }
                            else // ...else set it to file mode
                            {
                                this.Input.Mode = InputMode.File;
                            }

                            if (this.Errors.Count <= 0)
                            {
                                // Then get all the file paths / strings
                                optionArgs = this.GetAllOptionArgs(args, i + 1);
                                // Skip the next optionArgs.Length args, becuase they are count as option args
                                i += optionArgs.Count;
                                this.Input.Content = new List<List<string>>(optionArgs.Count);
                                IOHandler io = new IOHandler();

                                if (this.Input.Mode == InputMode.File)
                                {
                                    this.Input.Paths = optionArgs;
                                }
                                else if (this.Input.Mode == InputMode.String)
                                {
                                    this.Input.Content.Add(new List<string>(optionArgs.Count));

                                    for (int j = 0; j < optionArgs.Count; j++)
                                    {
                                        this.Input.Content[0].Add(optionArgs[j]);
                                    }
                                }
                            }

                            break;
                        case 'p':
                            if (option.Length > 1)
                            {
                                this.Warnings.Add((i + 1) + "# Unrecognized option: " + args[i]);
                            }
                            else
                            {
                                // Then get all the problem parameters including problem id
                                optionArgs = this.GetAllOptionArgs(args, i + 1);
                                // Skip the next optionArgs.Length args, becuase they are count as option args
                                i += optionArgs.Count;
                                this.Problem.Args = new List<string>(optionArgs.Count);

                                if (optionArgs.Count > 0)
                                {
                                    this.Problem.ID = optionArgs[0];
                                    this.Problem.Args = new List<string>(optionArgs.Count - 1);
                                    for (int j = 1; j < optionArgs.Count; j++)
                                    {
                                        this.Problem.Args.Add(optionArgs[j]);
                                    }
                                }
                            }

                            break;
                        case 'o':
                            // If user set the output mode...
                            if (option.Length > 1)
                            {
                                if (option.Contains('f') && option.Contains('c')) // fc
                                {
                                    this.Output.Mode = OutputMode.FileAndConsole;
                                }
                                else if (option.Contains('f')) // f
                                {
                                    this.Output.Mode = OutputMode.FileOnly;
                                }
                                else if (option.Contains('c')) // c
                                {
                                    this.Output.Mode = OutputMode.ConsoleOnly;
                                }
                                else
                                {
                                    this.Errors.Add("Invalid output switch: \"" + option.Substring(1) + "\" in " + args[i]);
                                }

                                string unknownSwitches = option.Substring(1).Replace("f", "").Replace("c", "");
                                if (unknownSwitches.Length > 0)
                                {
                                    this.Warnings.Add((i + 1) + "# Unrecognized switch: \"" + unknownSwitches + "\" in " + args[i]);
                                }
                            }
                            else // ...else set it to file only mode
                            {
                                this.Output.Mode = OutputMode.FileOnly;
                            }

                            if (this.Errors.Count <= 0)
                            {
                                // Then get all the file paths / strings
                                optionArgs = this.GetAllOptionArgs(args, i + 1);
                                // Skip the next optionArgs.Length args, becuase they are count as option args
                                i += optionArgs.Count;

                                this.Output.Paths = optionArgs;
                            }

                            break;
                        case 'l':
                            if (option.Length > 1)
                            {
                                this.Warnings.Add((i + 1) + "# Unrecognized option: " + args[i]);
                            }
                            else
                            {
                                this.ShouldListProblemIDs = true;
                            }

                            break;
                        case 'c':
                            // If user set the output mode with switches...
                            if (option.Length > 1)
                            {
                                if (option.Contains('e') && option.Contains('w')) // ew
                                {
                                    this.ConsoleMode = ConsoleDisplayMode.ErrorsAndWarnings;
                                }
                                else if (option.Contains('e')) // c
                                {
                                    this.ConsoleMode = ConsoleDisplayMode.ErrorsOnly;
                                }
                                else if (option.Contains('w')) // w
                                {
                                    this.ConsoleMode = ConsoleDisplayMode.WarningsOnly;
                                }
                                else
                                {
                                    this.Errors.Add("Invalid console switch: \"" + option.Substring(1) + "\" in " + args[i]);
                                }

                                string unknownSwitches = option.Substring(1).Replace("e", "").Replace("w", "");
                                if (unknownSwitches.Length > 0)
                                {
                                    this.Warnings.Add((i + 1) + "# Unrecognized switch: \"" + unknownSwitches + "\" in " + args[i]);
                                }
                            }
                            else // ...or only -c
                            {
                                this.ConsoleMode = ConsoleDisplayMode.None;
                            }

                            break;
                        case 'h':
                            if (option.Length > 1)
                            {
                                this.Warnings.Add((i + 1) + "# Unrecognized option: " + args[i]);
                            }
                            else
                            {
                                this.ShouldShowHelp = true;
                            }

                            break;
                        default:
                            this.Warnings.Add((i + 1) + "# Unrecognized option: " + args[i]);

                            break;
                    }

                    // Write to input file dir if output paths is not set
                    if (this.Input.Paths.Count > 0 && this.Output.Paths.Count < this.Input.Paths.Count)
                    {
                        for (int j = this.Output.Paths.Count; j < this.Input.Paths.Count; j++)
                        {
                            this.Output.Paths.Add(
                                System.IO.Path.Combine(System.IO.Path.GetDirectoryName(this.Input.Paths[j]),
                                    System.IO.Path.GetFileNameWithoutExtension(this.Input.Paths[j]))
                                + ".out");
                        }
                    }
                }
                else
                {
                    this.Warnings.Add((i + 1) + "# Unrecognized argument: " + args[i]);
                }
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            Options options = new Options();
            IOHandler io = new IOHandler();
            ProblemSolverHelper solverHelper = new ProblemSolverHelper();

            // Try parse options from args
            try
            {
                options.Parse(args.ToList());
                options.Validate();
            }
            catch (Exception exception)
            {
                options.Errors.Add(exception.Message);
            }

            // Treat empty args call as help request
            if (args.Length <= 0)
            {
                options.ShouldShowHelp = true;
            }

            // Continue only if no errors or show help instead if it's a help request
            if (options.Errors.Count == 0
                && !(options.ShouldShowHelp || options.ShouldListProblemIDs))
            {
                // Main process
                ProblemSolverHelper.SolveState solveState =
                    solverHelper.CallSolver(
                                                options.Problem.ID.ToLower(),
                                                ref options
                                            );

                if (solveState == ProblemSolverHelper.SolveState.SolverNotExist)
                {
                    options.Errors.Add("Unrecognized problem ID: " + options.Problem.ID);
                }

                // Result output: console display
                if (options.Output.Mode == Options.OutputMode.ConsoleOnly
                    || options.Output.Mode == Options.OutputMode.FileAndConsole)
                {
                    if (options.IsBinaryWrite)
                    {
                        io.Show(new string[] { "[Binary content]" });
                    }
                    else
                    {
                        foreach (var output in options.Output.Content)
                        {
                            io.Show(output);
                        }
                    }
                }

                // Result output: file output
                if (options.Output.Mode == Options.OutputMode.FileAndConsole
                    || options.Output.Mode == Options.OutputMode.FileOnly)
                {
                    if (options.IsBinaryWrite)
                    {
                        for (int i = 0; i < options.Output.BinaryContent.Count; i++)
                        {
                            try
                            {
                                io.Write(options.Output.Paths[i], options.Output.BinaryContent[i]);
                            }
                            catch (Exception exception)
                            {
                                options.Errors.Add(exception.Message);
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < options.Output.Content.Count; i++)
                        {
                            try
                            {
                                io.Write(options.Output.Paths[i], options.Output.Content[i]);
                            }
                            catch (Exception exception)
                            {
                                options.Errors.Add(exception.Message);
                            }
                        }
                    }
                }
            }

            // Show help in console
            if (options.ShouldShowHelp)
            {
                io.Show(Options.Help);
            }
            // Show problem IDs list in console
            if (options.ShouldListProblemIDs)
            {
                io.Show(solverHelper.ProblemSolvers.Keys);
            }

            if (options.Errors.Count > 0
                && !(options.ShouldShowHelp || options.ShouldListProblemIDs)
                && options.ConsoleMode != Options.ConsoleDisplayMode.WarningsOnly
                && options.ConsoleMode != Options.ConsoleDisplayMode.None)
            {
                // Show errors
                Console.WriteLine("Process aborted");
                Console.WriteLine("Error" + (options.Errors.Count > 1 ? "s" : "") + ":");
                foreach (string error in options.Errors)
                {
                    Console.WriteLine("  " + error);
                }
            }

            if (options.Warnings.Count > 0
                && !(options.ShouldShowHelp || options.ShouldListProblemIDs)
                && options.ConsoleMode != Options.ConsoleDisplayMode.ErrorsOnly
                && options.ConsoleMode != Options.ConsoleDisplayMode.None)
            {
                // Show warnings
                Console.WriteLine("Warning" + (options.Warnings.Count > 1 ? "s" : "") + ":");
                foreach (string waning in options.Warnings)
                {
                    Console.WriteLine("  " + waning);
                }
            }
        }