Exemplo n.º 1
0
        public SVDParser(string path, SystemBus parent)
        {
            currentSystemBus = parent;
            XDocument document;
            Stream    possibleGzip;

            try
            {
                using (possibleGzip = File.OpenRead(path))
                {
                    if (possibleGzip.ReadByte() == 0x1F && possibleGzip.ReadByte() == 0x8B) // gzip header
                    {
                        parent.Log(LogLevel.Info, "Detected gzipped file, ungzipping.");
                        possibleGzip.Close();
                        possibleGzip = new GZipStream(File.OpenRead(path), CompressionMode.Decompress);
                        path         = TemporaryFilesManager.Instance.GetTemporaryFile();
                        using (var extractedFile = File.OpenWrite(path))
                        {
                            possibleGzip.CopyTo(extractedFile);
                        }
                        parent.Log(LogLevel.Info, "Successfully ungzipped.");
                    }
                }
                document = XDocument.Load(path);
            }
            catch (Exception ex)
            {
                if (ex is FileNotFoundException || ex is DirectoryNotFoundException || ex is PathTooLongException)
                {
                    throw new RecoverableException($"File '{path}' does not exist.");
                }
                else if (ex is UnauthorizedAccessException)
                {
                    throw new RecoverableException($"File '{path}' cannot be loaded due to insufficient permissions.");
                }
                else if (ex is IOException)
                {
                    throw new RecoverableException($"An I/O error occurred while opening the file '{path}'.");
                }
                else if (ex is XmlException)
                {
                    throw new RecoverableException($"Given SVD file could not be loaded due to an exception: {ex.Message}.");
                }
                throw;
            }
            var deviceNode = document.Elements().FirstOrDefault(x => x.Name == "device");

            if (deviceNode == null)
            {
                throw new RecoverableException($"There is no <device> element in the file '{path}'.");
            }
            var device = new SVDDevice(deviceNode, this);

            registerDictionary = new Dictionary <long, SVDRegister>();
            foreach (var register in device.Peripherals.SelectMany(x => x.Registers))
            {
                AppendRegisterToDictionary(register);
            }
            parent.Log(LogLevel.Info, "Loaded SVD: {0}. Name: {1}. Description: {2}.", path, device.Name, device.Description);
        }
Exemplo n.º 2
0
 public SVDPeripheral(string name, SVDDevice includedDevice)
 {
     Name         = name;
     Registers    = new List <SVDRegister>();
     ParentDevice = includedDevice;
 }