Пример #1
0
        public ConversionMgr(ParameterParser args)
        {
            this.args   = args;
            conversions = new Dictionary <FileType, IConverter>();
            Array formats = Enum.GetValues(typeof(FileType));

            foreach (FileType type in formats)
            {
                conversions[type] = null;
            }

            var convTypes = args["target", true] as List <object>;

            if (convTypes != null)
            {
                foreach (string target in convTypes)
                {
                    FileType targetType = FileType.Unknown;
                    if (!Enum.TryParse <FileType>(target, true, out targetType))
                    {
                        throw new ParameterException("Unknown target format: \"" + target + "\"");
                    }
                    foreach (FileType type in formats)
                    {
                        IConverter conv = findConverter(type, targetType);
                        if (conv != null)
                        {
                            conversions[type] = conv;
                        }
                    }
                }
            }

            convTypes = args["from-to", true] as List <object>;
            if (convTypes != null)
            {
                foreach (object s in convTypes)
                {
                    object[] ss = s as object[];
                    FileType fromType = FileType.Unknown, toType = FileType.Unknown;
                    if (!Enum.TryParse <FileType>(ss[0] as string, true, out fromType))
                    {
                        throw new Exception("Unknown source format: \"" + (ss[0] as string) + "\"");
                    }
                    if (!Enum.TryParse <FileType>(ss[1] as string, true, out toType))
                    {
                        throw new Exception("Unknown target format: \"" + (ss[1] as string) + "\"");
                    }
                    IConverter conv = findConverter(fromType, toType);
                    if (conv == null)
                    {
                        throw new Exception("Conversions from " + fromType + " to " + toType + " are not supported.");
                    }
                    conversions[fromType] = conv;
                }
            }
        }
Пример #2
0
        public void addFromParameters(ParameterParser args)
        {
            List <object> entries = args["input", true] as List <object>;

            if (entries != null)
            {
                foreach (object s in entries)
                {
                    if (!addSingleFile(s as string))
                    {
                        Console.Error.WriteLine("Warning: did not include \"" + s + "\"");
                    }
                }
            }

            entries = args["finput", true] as List <object>;
            if (entries != null)
            {
                foreach (object s in entries)
                {
                    object[] ss = s as object[];
                    if (!addFilterDir(ss[0] as string, ss[1] as string))
                    {
                        Console.Error.WriteLine("Warning: did not include \"" + ss[0] + "\"");
                    }
                }
            }

            entries = args["rinput", true] as List <object>;
            if (entries != null)
            {
                foreach (object s in entries)
                {
                    object[] ss = s as object[];
                    if (!addRegexDir(ss[0] as string, ss[1] as string))
                    {
                        Console.Error.WriteLine("Warning: did not include \"" + ss[0] + "\"");
                    }
                }
            }

            entries = args["linput", true] as List <object>;
            if (entries != null)
            {
                foreach (object s in entries)
                {
                    if (!addFromListFile(s as string))
                    {
                        Console.Error.WriteLine("Warning: did not include at least one file from \"" + s + "\"");
                    }
                }
            }
        }
Пример #3
0
 public static void initialize(ParameterParser args)
 {
     if (args["stdout"] != null)
     {
         if (stdoutStream != null)
         {
             stdoutWriter.Flush();
             stdoutStream.Close();
         }
         try
         {
             stdoutStream           = new FileStream(args["stdout"] as string, FileMode.OpenOrCreate, FileAccess.Write);
             stdoutWriter           = new StreamWriter(stdoutStream);
             stdoutWriter.AutoFlush = true;
             Console.SetOut(stdoutWriter);
         }
         catch (Exception)
         {
             Console.Error.WriteLine("Could not redirect stdout to \"" + (args["stdout"] as string) + "\"");
         }
     }
     else if (args["stderr"] != null)
     {
         if (stderrStream != null)
         {
             stderrWriter.Flush();
             stderrStream.Close();
         }
         try
         {
             stderrStream           = new FileStream(args["stderr"] as string, FileMode.OpenOrCreate, FileAccess.Write);
             stderrWriter           = new StreamWriter(stderrStream);
             stderrWriter.AutoFlush = true;
             Console.SetError(stderrWriter);
         }
         catch (Exception)
         {
             Console.Error.WriteLine("Could not redirect stderr to \"" + (args["stderr"] as string) + "\"");
         }
     }
 }
Пример #4
0
        public static void Main(string[] args)
        {
            CommandLine     cl          = new CommandLine();
            ParameterParser paramParser = null;

            try
            {
                paramParser = new ParameterParser(cl, paramTypes);
            }
            catch (ParameterException exp)
            {
                Console.Error.WriteLine(exp.Message);
                Environment.ExitCode = -1;
                return;
            }
            OutputHelper.initialize(paramParser);

            if (paramParser["help"] != null)
            {
                Console.Out.WriteLine("usage: zzio_cli.exe <options>");
                Console.Out.WriteLine();
                Console.Out.WriteLine("Options:");
                foreach (ParameterInfo info in paramTypes)
                {
                    Console.Out.Write("  ");
                    if (info.shortName != (char)0)
                    {
                        Console.Out.Write("-" + info.shortName + ", ");
                    }
                    Console.Out.Write("--" + info.longName + " ");
                    if (info.values.Length == 1 && info.values[0] == ParameterType.Boolean)
                    {
                        Console.Out.WriteLine("- Enables " + info.description);
                        Console.Out.Write("  ");
                        if (info.shortName != (char)0)
                        {
                            Console.Out.Write("-d" + info.shortName + ", ");
                        }
                        Console.Out.WriteLine("--disable-" + info.longName + " - Disables " + info.description);
                    }
                    else
                    {
                        Console.Out.WriteLine(info.description);
                    }
                }
                Console.Out.WriteLine();
                Console.Out.WriteLine("Formats:");
                Array formats = Enum.GetValues(typeof(FileType));
                foreach (FileType type in formats)
                {
                    if (type != FileType.Unknown)
                    {
                        Console.Out.WriteLine("  " + type.ToString());
                    }
                }
                return;
            }

            FileSelection fs = new FileSelection();

            fs.addFromParameters(paramParser);
            ConversionMgr convMgr = null;

            try
            {
                convMgr = new ConversionMgr(paramParser);
            }
            catch (ParameterException e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(-1);
                return;
            }
            FileType[] types = ConversionMgr.scanFiles(fs.Files);

            bool doMapDB  = (bool)paramParser["map-db"];
            bool ignoreDB = false;

            if (doMapDB)
            {
                int indexI = -1, firstDataI = -1;
                for (int i = 0; i < types.Length; i++)
                {
                    if (types[i] == FileType.FBS_Index)
                    {
                        if (indexI < 0)
                        {
                            indexI = 0;
                        }
                        else
                        {
                            Console.Error.WriteLine("Warning: Multiple database index files, no database output");
                            ignoreDB = true;
                            break;
                        }
                    }
                    else if (types[i] == FileType.FBS_Data && firstDataI < 0)
                    {
                        firstDataI = i;
                    }
                }
                if (!ignoreDB && (indexI < 0 || firstDataI < 0) && !(indexI < 0 && firstDataI < 0))
                {
                    Console.Error.WriteLine("Warning: Database mapping requires both an index and a module file included, no database output");
                    ignoreDB = true;
                }
            }

            string outDir = Path.GetFullPath(paramParser["output"] as string);

            for (int i = 0; i < fs.Files.Count; i++)
            {
                string f = fs.Files[i];
                if (types[i] == FileType.Unknown)
                {
                    Console.Error.WriteLine("Warning: The format of " + Path.GetFileName(f) + " could not be determined, it will be ignored");
                    continue;
                }
                else if (ignoreDB && (types[i] == FileType.FBS_Data || types[i] == FileType.FBS_Index))
                {
                    continue;
                }
                FileType targetType = convMgr.getTargetFileType(types[i]);
                if (targetType == FileType.Unknown)
                {
                    Console.Error.WriteLine("Warning: No " + types[i] + " converter enabled for " + Path.GetFileName(f) + ", it will be ignored");
                    continue;
                }

                var        outFn = Path.Combine(outDir, Path.GetFileName(f)) + ConversionMgr.getFileTypeExt(targetType);
                FileStream fromStream = null, toStream = null;
                try
                {
                    //open files
                    fromStream = new FileStream(f, FileMode.Open, FileAccess.Read);
                    try
                    {
                        toStream = new FileStream(outFn, FileMode.OpenOrCreate, FileAccess.Write);
                    }
                    catch (Exception)
                    {
                        Console.Error.WriteLine("Warning: Could not open output file: " + outFn);
                    }

                    //convert
                    try
                    {
                        convMgr.convertScannedFile(Path.GetFileName(f), fromStream, toStream, types[i]);
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Warning: Could not convert " + Path.GetFileName(f) + ": " + e.Message);
                    }

                    //close files
                    if (toStream != null)
                    {
                        toStream.Close();
                    }
                    fromStream.Close();
                }
                catch (Exception)
                {
                    Console.Error.WriteLine("Warning: Could not open input file: " + f);
                }
            }
        }