示例#1
0
        /// <summary>
        /// Loads the ASN.1 document to import.
        /// </summary>
        /// <exception cref="FileNotFoundException">If the import file is not found.</exception>
        public Asn1Document LoadDocument()
        {
            if (!(this.importedDocument is null))
            {
                return(this.importedDocument);
            }

            string Folder    = Path.GetDirectoryName(this.document.Location);
            string FileName  = Path.Combine(Folder, this.module);
            string Extension = Path.GetExtension(this.document.Location);

            if (!File.Exists(FileName))
            {
                FileName = null;

                foreach (string ImportFolder in this.document.ImportFolders)
                {
                    FileName = Path.Combine(ImportFolder, this.module) + Extension;
                    if (File.Exists(FileName))
                    {
                        break;
                    }
                    else
                    {
                        FileName = null;
                    }
                }

                if (FileName is null)
                {
                    throw new FileNotFoundException("Unable to find import file for module " + this.module);
                }
            }

            this.importedDocument = Asn1Document.FromFile(FileName, this.document.ImportFolders);

            return(this.importedDocument);
        }
示例#2
0
 public static Asn1Document ParseAsn1Document(string FileName)
 {
     return(Asn1Document.FromFile(Path.Combine("Examples", FileName), ImportFolders));
 }
示例#3
0
        static int Main(string[] args)
        {
            try
            {
                CSharpExportSettings      Settings        = new CSharpExportSettings(string.Empty, EncodingSchemes.None);
                List <string>             ImportFolders   = new List <string>();
                Dictionary <string, bool> ExportedModules = new Dictionary <string, bool>();
                SearchOption SearchOption = SearchOption.TopDirectoryOnly;
                string       OutputFolder = null;
                string       s;
                int          i = 0;
                int          c = args.Length;

                while (i < c)
                {
                    s = args[i++].ToLower();

                    switch (s)
                    {
                    case "-ber":
                        Settings.Codecs |= EncodingSchemes.BER;
                        break;

                    case "-cer":
                        Settings.Codecs |= EncodingSchemes.CER;
                        break;

                    case "-der":
                        Settings.Codecs |= EncodingSchemes.DER;
                        break;

                    case "-n":
                        if (i >= c)
                        {
                            throw new Exception("Missing namespace.");
                        }

                        Settings.BaseNamespace = args[i++];
                        break;

                    case "-o":
                        if (i >= c)
                        {
                            throw new Exception("Missing output folder.");
                        }

                        OutputFolder = Path.GetFullPath(args[i++]);

                        if (!Directory.Exists(OutputFolder))
                        {
                            Directory.CreateDirectory(OutputFolder);
                        }
                        break;

                    case "-f":
                        if (i >= c)
                        {
                            throw new Exception("Missing import folder.");
                        }

                        s = Path.GetFullPath(args[i++]);

                        if (!Directory.Exists(s))
                        {
                            throw new Exception("Import folder does not exist: " + s);
                        }

                        if (!ImportFolders.Contains(s))
                        {
                            ImportFolders.Add(s);
                        }
                        break;

                    case "-t":
                        SearchOption = SearchOption.TopDirectoryOnly;
                        break;

                    case "-r":
                        SearchOption = SearchOption.AllDirectories;
                        break;

                    case "-i":

                        if (string.IsNullOrWhiteSpace(Settings.BaseNamespace))
                        {
                            throw new Exception("No base namespace provided.");
                        }

                        if (string.IsNullOrWhiteSpace(OutputFolder))
                        {
                            throw new Exception("No output folder provided.");
                        }

                        if (i >= c)
                        {
                            throw new Exception("Missing import folder.");
                        }

                        s = args[i++];

                        List <string> InputFiles = new List <string>();

                        if (File.Exists(s))
                        {
                            InputFiles.Add(s);
                        }
                        else
                        {
                            string Pattern = Path.GetFileName(s);
                            string Folder  = Path.GetFullPath(Path.GetDirectoryName(s));

                            foreach (string FileName in Directory.GetFiles(Folder, Pattern, SearchOption))
                            {
                                InputFiles.Add(FileName);
                            }
                        }

                        foreach (string FileName in InputFiles)
                        {
                            Console.Out.WriteLine("Loading " + FileName + "...");

                            Asn1Document Doc = Asn1Document.FromFile(FileName, ImportFolders.ToArray());

                            Console.Out.WriteLine("Generating C#...");

                            string CSharp         = Doc.ExportCSharp(Settings);
                            string OutputFileName = Path.Combine(OutputFolder, Path.ChangeExtension(Path.GetFileName(FileName), "cs"));

                            Console.Out.WriteLine("Exporting " + OutputFileName + "...");

                            File.WriteAllText(OutputFileName, CSharp, Encoding.UTF8);

                            foreach (string ImportedModule in Settings.Modules)
                            {
                                if (ExportedModules.ContainsKey(ImportedModule))
                                {
                                    continue;
                                }

                                CSharp         = Settings.GetCode(ImportedModule);
                                OutputFileName = Path.Combine(OutputFolder, Path.ChangeExtension(ImportedModule, "cs"));

                                Console.Out.WriteLine("Exporting " + OutputFileName + "...");

                                File.WriteAllText(OutputFileName, CSharp, Encoding.UTF8);

                                ExportedModules[ImportedModule] = true;
                            }
                        }

                        break;

                    case "-?":
                        Console.Out.WriteLine("Creates C# files from definitions made in ASN.1 files.");
                        Console.Out.WriteLine();
                        Console.Out.WriteLine("Command line switches:");
                        Console.Out.WriteLine();
                        Console.Out.WriteLine("-BER                  Adds support for BER enconding.");
                        Console.Out.WriteLine("-CER                  Adds support for CER enconding.");
                        Console.Out.WriteLine("-DER                  Adds support for DER enconding.");
                        Console.Out.WriteLine("-n NAMESPACE          Defines a base namespace. Can be");
                        Console.Out.WriteLine("                      used multiple times, to place code");
                        Console.Out.WriteLine("                      in different namespaces.");
                        Console.Out.WriteLine("-o FOLDER             Path to folder where C# files will be");
                        Console.Out.WriteLine("                      stored. Can be used multiple times");
                        Console.Out.WriteLine("                      in one call, to place different files");
                        Console.Out.WriteLine("                      in different output folders.");
                        Console.Out.WriteLine("-f IMPORT_FOLDER      Path to a folder where import files");
                        Console.Out.WriteLine("                      may be stored. Can be used multiple");
                        Console.Out.WriteLine("                      times to define different folders.");
                        Console.Out.WriteLine("-t                    Only load files from top input");
                        Console.Out.WriteLine("                      folder(s).");
                        Console.Out.WriteLine("-r                    Load files recursively from input");
                        Console.Out.WriteLine("                      folder(s) and their subfolders.");
                        Console.Out.WriteLine("-i PATTERN            Search pattern for ASN.1 files, or");
                        Console.Out.WriteLine("                      single ASN.1 file to convert to C#.");
                        Console.Out.WriteLine("-?                    Help.");
                        break;

                    default:
                        throw new Exception("Unrecognized switch: " + s);
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
                return(-1);
            }
        }