Esempio n. 1
0
        static int Main(string[] args)
        {
            Logger.Logged += OnLogged;
            Logger.DisableFileLogging();

            ReturnCode optionParsingResult = ParseOptions(args);

            if (optionParsingResult != ReturnCode.Success)
            {
                return((int)optionParsingResult);
            }

            // parse importer name
            if (importerName != null && !importerName.Equals("auto", StringComparison.OrdinalIgnoreCase))
            {
                MapFormat importFormat;
                if (!EnumUtil.TryParse(importerName, out importFormat, true))
                {
                    Console.Error.WriteLine("MapConverter: Unsupported importer \"{0}\"", importerName);
                    PrintUsage();
                    return((int)ReturnCode.UnrecognizedImporter);
                }
                importer = MapUtility.GetImporter(importFormat);
                if (importer == null)
                {
                    Console.Error.WriteLine("MapConverter: Loading from \"{0}\" is not supported", importFormat);
                    PrintUsage();
                    return((int)ReturnCode.UnsupportedLoadFormat);
                }
            }

            // parse exporter format
            MapFormat exportFormat;

            if (!EnumUtil.TryParse(exporterName, out exportFormat, true))
            {
                Console.Error.WriteLine("MapConverter: Unrecognized exporter \"{0}\"", exporterName);
                PrintUsage();
                return((int)ReturnCode.UnrecognizedExporter);
            }
            exporter = MapUtility.GetExporter(exportFormat);
            if (exporter == null)
            {
                Console.Error.WriteLine("MapConverter: Saving to \"{0}\" is not supported", exportFormat);
                PrintUsage();
                return((int)ReturnCode.UnsupportedSaveFormat);
            }

            // check input paths
            bool hadFile = false,
                 hadDir  = false;

            foreach (string inputPath in inputPathList)
            {
                if (hadDir)
                {
                    Console.Error.WriteLine("MapConverter: Only one directory may be specified at a time.");
                    return((int)ReturnCode.ArgumentError);
                }
                // check if input path exists, and if it's a file or directory
                try {
                    if (File.Exists(inputPath))
                    {
                        hadFile = true;
                    }
                    else if (Directory.Exists(inputPath))
                    {
                        hadDir = true;
                        if (hadFile)
                        {
                            Console.Error.WriteLine("MapConverter: Cannot mix directories and files in input.");
                            return((int)ReturnCode.ArgumentError);
                        }
                        directoryMode = true;
                        if (!outputDirGiven)
                        {
                            outputDirName = inputPath;
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("MapConverter: Cannot locate \"{0}\"", inputPath);
                        return((int)ReturnCode.InputPathNotFound);
                    }
                } catch (Exception ex) {
                    Console.Error.WriteLine("MapConverter: {0}: {1}",
                                            ex.GetType().Name,
                                            ex.Message);
                    return((int)ReturnCode.PathError);
                }
            }

            // check recursive flag
            if (recursive && !directoryMode)
            {
                Console.Error.WriteLine("MapConverter: Recursive flag is given, but input is not a directory.");
                return((int)ReturnCode.ArgumentError);
            }

            // check input filter
            if (inputFilter != null && !directoryMode)
            {
                Console.Error.WriteLine("MapConverter: Filter param is given, but input is not a directory.");
                return((int)ReturnCode.ArgumentError);
            }

            // check regex filter
            if (useRegex)
            {
                try {
                    filterRegex = new Regex(inputFilter);
                } catch (ArgumentException ex) {
                    Console.Error.WriteLine("MapConverter: Cannot parse filter regex: {0}",
                                            ex.Message);
                    return((int)ReturnCode.ArgumentError);
                }
            }

            // check if output dir exists; create it if needed
            if (outputDirName != null)
            {
                try {
                    if (!Directory.Exists(outputDirName))
                    {
                        Directory.CreateDirectory(outputDirName);
                    }
                } catch (Exception ex) {
                    Console.Error.WriteLine("MapRenderer: Error checking output directory: {0}: {1}",
                                            ex.GetType().Name,
                                            ex.Message);
                }
            }

            // process inputs, one path at a time
            foreach (string inputPath in inputPathList)
            {
                ReturnCode code = ProcessInputPath(inputPath);
                if (code != ReturnCode.Success)
                {
                    return((int)code);
                }
            }
            return((int)ReturnCode.Success);
        }
Esempio n. 2
0
        static int Main(string[] args)
        {
            Logger.Logged += OnLogged;

            ReturnCode optionParsingResult = ParseOptions(args);

            if (optionParsingResult != ReturnCode.Success)
            {
                return((int)optionParsingResult);
            }

            // parse importer name
            if (importerName != null && !importerName.Equals("auto", StringComparison.OrdinalIgnoreCase))
            {
                MapFormat importFormat;
                if (!EnumUtil.TryParse(importerName, out importFormat, true) ||
                    (importer = MapUtility.GetImporter(importFormat)) == null)
                {
                    Console.Error.WriteLine("Unsupported importer \"{0}\"", importerName);
                    PrintUsage();
                    return((int)ReturnCode.UnrecognizedImporter);
                }
            }

            // parse exporter format
            MapFormat exportFormat;

            if (!EnumUtil.TryParse(exporterName, out exportFormat, true) ||
                (exporter = MapUtility.GetExporter(exportFormat)) == null)
            {
                Console.Error.WriteLine("Unsupported exporter \"{0}\"", exporterName);
                PrintUsage();
                return((int)ReturnCode.UnrecognizedExporter);
            }

            // check if input path exists, and if it's a file or directory
            bool directoryMode;

            try {
                if (File.Exists(inputPath))
                {
                    directoryMode = false;
                    if (outputDirName == null)
                    {
                        outputDirName = Paths.GetDirectoryNameOrRoot(inputPath);
                    }
                }
                else if (Directory.Exists(inputPath))
                {
                    directoryMode = true;
                    if (outputDirName == null)
                    {
                        outputDirName = Paths.GetDirectoryNameOrRoot(inputPath);
                    }
                }
                else
                {
                    Console.Error.WriteLine("MapConverter: Cannot locate \"{0}\"", inputPath);
                    return((int)ReturnCode.InputDirNotFound);
                }

                if (!Directory.Exists(outputDirName))
                {
                    Directory.CreateDirectory(outputDirName);
                }
            } catch (Exception ex) {
                Console.Error.WriteLine("MapConverter: {0}: {1}",
                                        ex.GetType().Name,
                                        ex.Message);
                return((int)ReturnCode.PathError);
            }

            // check recursive flag
            if (recursive && !directoryMode)
            {
                Console.Error.WriteLine("MapConverter: Recursive flag is given, but input is not a directory.");
            }

            // check input filter
            if (inputFilter != null && !directoryMode)
            {
                Console.Error.WriteLine("MapConverter: Filter param is given, but input is not a directory.");
            }

            if (!recursive && importer != null && importer.StorageType == MapStorageType.Directory)
            {
                // single-directory conversion
                ConvertOneMap(new DirectoryInfo(inputPath));
            }
            else if (!directoryMode)
            {
                // single-file conversion
                ConvertOneMap(new FileInfo(inputPath));
            }
            else
            {
                // possible single-directory conversion
                if (!recursive && ConvertOneMap(new DirectoryInfo(inputPath)))
                {
                    return((int)ReturnCode.Success);
                }

                // otherwise, go through all files inside the given directory
                SearchOption  recursiveOption = (recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
                DirectoryInfo inputDirInfo    = new DirectoryInfo(inputPath);
                if (inputFilter == null)
                {
                    inputFilter = "*";
                }
                foreach (var dir in inputDirInfo.GetDirectories(inputFilter, recursiveOption))
                {
                    ConvertOneMap(dir);
                }
                foreach (var file in inputDirInfo.GetFiles(inputFilter, recursiveOption))
                {
                    ConvertOneMap(file);
                }
            }

            return((int)ReturnCode.Success);
        }