コード例 #1
0
ファイル: Program.cs プロジェクト: vvaske/PLUtil
        static void WriteXMLPropertyList(object plist, string fn, string result)
        {
            var mem = new MemoryStream();

            try
            {
                using (var ofs = new FileStream(fn, FileMode.Create))
                {
                    using (var writer = XmlWriter.Create(mem, new XmlWriterSettings()
                    {
                        Encoding = Encoding.UTF8,
                        Indent = true,
                        IndentChars = "\t",
                        OmitXmlDeclaration = false,
                        CloseOutput = false
                    }))
                        PropertyList2XML.GenerateXMLPropertyListToData(writer, plist);
                    mem.Seek(0L, SeekOrigin.Begin);
                    mem.WriteTo(ofs);
                    if (result != null)
                    {
                        Console.WriteLine(result, fn);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}" + txtPListError + "{1}", fn, ex.Message);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: vvaske/PLUtil
        static object ReadPropertyList(string fn, string result)
        {
            object       plist;
            string       version = null;
            MemoryStream mem     = null;
            string       type    = null;

            try
            {
                using (var ifs = new FileStream(fn, FileMode.Open))
                    mem = CreateBuffer(ifs);
                var    reader = new BinaryReader(mem, Encoding.UTF8);
                string header = new String(reader.ReadChars(6));
                mem.Seek(0L, SeekOrigin.Begin);
                if (header == "bplist")
                {
                    type  = "Binary";
                    plist = Binary2PropertyList.BinaryPlistCreate(mem, out version);
#if DEBUG
                    if (result != null && !result.Contains("=>"))
                    {
                        var mem2 = new MemoryStream();
                        if (version == "1.5")
                        {
                            PropertyList2Binary.BinaryPlistWrite15(plist, mem2);
                        }
                        else
                        {
                            PropertyList2Binary.BinaryPlistWrite(plist, mem2);
                        }
                        if (!DataComparer.Equals(mem.ToArray(), mem2.ToArray()))
                        {
                            using (var tmp = new FileStream(fn + ".tmp", FileMode.Create))
                                mem2.WriteTo(tmp);
                            throw new InvalidOperationException("Read/write operation failed: " + fn + ".tmp");
                        }
                    }
#endif
                    version = " v" + version;
                }
                else if (header.StartsWith("<?xml"))
                {
                    type = "XML";
                    var doc = new XPathDocument(mem);
                    var nav = doc.CreateNavigator();
                    plist = XML2PropertyList.PropertyListCreateFromXML(nav);
#if DEBUG
                    if (result != null && !result.Contains("=>"))
                    {
                        var mem2 = new MemoryStream();
                        using (var writer = XmlWriter.Create(mem2, new XmlWriterSettings()
                        {
                            Encoding = Encoding.UTF8,
                            Indent = true,
                            IndentChars = "\t",
                            OmitXmlDeclaration = false,
                            CloseOutput = false
                        }))
                            PropertyList2XML.GenerateXMLPropertyListToData(writer, plist);
                        if (!DataComparer.Equals(mem.ToArray(), mem2.ToArray()))
                        {
                            using (var tmp = new FileStream(fn + ".tmp", FileMode.Create))
                                mem2.WriteTo(tmp);
                            throw new InvalidOperationException("Read/write operation failed: " + fn + ".tmp");
                        }
                    }
#endif
                }
                else
                {
                    throw new NotSupportedException("Unknown property list file format");
                }
                if (result != null)
                {
                    Console.WriteLine(result, fn, type, version);
                }
                return(plist);
            }
            catch (XmlException ex)
            {
                Console.WriteLine("{0}({1},{2})" + txtPListError + "{3}", fn, ex.LineNumber, ex.LinePosition, (ex.InnerException ?? ex).Message);
                return(null);
            }
            catch (Exception ex)
            {
                if (mem != null && mem.CanRead)
                {
                    Console.WriteLine("{0}+{1:X}" + txtPListError + "{2}", fn, mem.Position, ex.Message);
                }
                else
                {
                    Console.WriteLine("{0}" + txtPListError + "{1}", fn, ex.Message);
                }
                return(null);
            }
        }