Esempio n. 1
0
        static void Main(string[] args)
        {
            Options options = null;

            try
            {
                ParseCommandlineParameters(args, out options);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            if (options == null)
            {
                throw new ArgumentException(nameof(options));
            }

            // ReSharper disable once PossibleNullReferenceException
            using (_logStream = string.IsNullOrWhiteSpace(options.LogFile)
                ? Console.OpenStandardOutput()
                : File.OpenWrite(ReplaceWildCards(options.LogFile)))
            {
                try
                {
                    var maxTasks = SetMaxConcurrencyLevel(options);

                    if (options.InputIsList)
                    {
                        _itemsToConvert = new ConcurrentQueue <ConversionItem>();
                        _itemsConverted = new ConcurrentQueue <ConversionItem>();

                        WriteToLog($"Reading input file '{options.Input}'");
                        var lines = File.ReadAllLines(options.Input);
                        foreach (var line in lines)
                        {
                            var inputUri   = new ConvertUri(line);
                            var outputPath = Path.GetFullPath(options.Output);

                            var outputFile = inputUri.IsFile
                                ? Path.GetFileName(inputUri.AbsolutePath)
                                : FileManager.RemoveInvalidFileNameChars(inputUri.ToString());

                            _itemsToConvert.Enqueue(new ConversionItem(inputUri,
                                                                       Path.Combine(outputPath, outputFile)));
                        }

                        WriteToLog($"{_itemsToConvert.Count} items read");

                        if (options.UseMultiThreading)
                        {
                            _workerTasks = new List <Task>();

                            WriteToLog($"Starting {maxTasks} processing tasks");
                            for (var i = 0; i < maxTasks; i++)
                            {
                                var i1 = i;
                                _workerTasks.Add(_taskFactory.StartNew(() =>
                                                                       ConvertWithTask(options, (i1 + 1).ToString())));
                            }

                            WriteToLog("Started");

                            // Waiting until all tasks are finished
                            foreach (var task in _workerTasks)
                            {
                                task.Wait();
                            }
                        }
                        else
                        {
                            ConvertWithTask(options, null);
                        }

                        // Write conversion information to output file
                        using (var output = File.OpenWrite(options.Output))
                        {
                            foreach (var itemConverted in _itemsConverted)
                            {
                                var bytes = new UTF8Encoding(true).GetBytes(itemConverted.OutputLine);
                                output.Write(bytes, 0, bytes.Length);
                            }
                        }
                    }
                    else
                    {
                        Convert(options);
                    }

                    Environment.Exit(0);
                }
                catch (Exception exception)
                {
                    WriteToLog(exception.StackTrace + ", " + exception.Message);
                    Environment.Exit(1);
                }
            }
        }