Example #1
0
        private string GetValue(string key)
        {
            CryXMLNodeIndex nFirst = _node( ).nFirstAttributeIndex;
            CryXMLNodeIndex nLast  = nFirst + _node( ).nAttributeCount;

            for (CryXMLNodeIndex i = nFirst; i < nLast; i++)
            {
                XmlString attrKey = _string(m_pData.pAttributes[i].nKeyStringOffset);
                if (key == attrKey)
                {
                    string attrValue = _string(m_pData.pAttributes[i].nValueStringOffset);
                    return(attrValue);
                }
            }
            return("");
        }
Example #2
0
        // Find node with specified tag.
        public override CryXmlNodeRef findChild(string tag)
        {
            CryXMLNode      pNode      = _node( );
            CryXMLNodeIndex nFirst     = pNode.nFirstChildIndex;
            CryXMLNodeIndex nAfterLast = pNode.nFirstChildIndex + pNode.nChildCount;

            for (CryXMLNodeIndex i = nFirst; i < nAfterLast; i++)
            {
                string sChildTag = _string(m_pData.pNodes[m_pData.pChildIndices[i]].nTagStringOffset);
                if (tag == sChildTag)
                {
                    CryXmlNodeRef n = m_pData.pBinaryNodes[m_pData.pChildIndices[i]];
                    return(n);
                }
            }
            return(null);
        }
Example #3
0
        /// <summary>
        /// Reads the binary buffer into the internal structures
        ///  Note: use GetErrorDescription() if the result is not Success
        /// </summary>
        /// <param name="fileContents">An filled byte buffer</param>
        /// <param name="result">Out: the result of the collection of file content</param>
        /// <returns>A valid memory context or null</returns>
        private CryXmlBinContext Create(byte[] fileContents, out EResult result)
        {
            result = EResult.Error;

            CryXmlBinContext pData = new CryXmlBinContext( );

            if (pData == null)
            {
                SetErrorDescription("Can't allocate memory for binary XML object.");
                return(null);
            }

            CryXMLHeader header = Conversions.ByteToType <CryXMLHeader>(fileContents); // reads the header, mapping from binary to struct

            if (!header.HasCorrectSignature)
            {
                SetErrorDescription("File is not a binary XML object (wrong header signature).");
                return(null);
            }

            // Create binary nodes to wrap the ones to read later
            pData.pBinaryNodes = new List <CryXmlBinNode>( ); // dynamic list used, not an array
            for (int i = 0; i < header.nNodeCount; i++)
            {
                pData.pBinaryNodes.Add(new CryXmlBinNode( ));
            }

            // map file content to binary.. here we really allocate arrays of elements and copy rather than the original which worked well with ptrs in c++

            try {
                pData.pAttributes = ( CryXMLAttribute[] )Array.CreateInstance(typeof(CryXMLAttribute), header.nAttributeCount); // alloc enough
                UInt32 incr = CryXMLAttribute.MySize( );                                                                        // size of one element to read
                for (UInt32 aIdx = 0; aIdx < header.nAttributeCount; aIdx++)
                {
                    pData.pAttributes[aIdx] = Conversions.ByteToType <CryXMLAttribute>(fileContents, header.nAttributeTablePosition + aIdx * incr);
                }
            } catch (Exception e) {
                SetErrorDescription(string.Format("EXC Attributes: {0}", e.Message));
                return(null);
            }

            try {
                pData.pChildIndices = ( CryXMLNodeIndex[] )Array.CreateInstance(typeof(CryXMLNodeIndex), header.nChildCount); // alloc enough
                UInt32 incr = CryXMLNodeIndex.MySize( );                                                                      // size of one element to read
                for (UInt32 aIdx = 0; aIdx < header.nChildCount; aIdx++)
                {
                    pData.pChildIndices[aIdx] = Conversions.ByteToType <CryXMLNodeIndex>(fileContents, header.nChildTablePosition + aIdx * incr);
                }
            } catch (Exception e) {
                SetErrorDescription(string.Format("EXC ChildIndices: {0}", e.Message));
                return(null);
            }

            try {
                pData.pNodes = ( CryXMLNode[] )Array.CreateInstance(typeof(CryXMLNode), header.nNodeCount); // alloc enough
                UInt32 incr = CryXMLNode.MySize( );                                                         // size of one element to read
                for (UInt32 aIdx = 0; aIdx < header.nNodeCount; aIdx++)
                {
                    pData.pNodes[aIdx] = Conversions.ByteToType <CryXMLNode>(fileContents, header.nNodeTablePosition + aIdx * incr);
                }
            } catch (Exception e) {
                SetErrorDescription(string.Format("EXC Nodes: {0}", e.Message));
                return(null);
            }

            try {
                pData.pStringDataLength = header.nStringDataSize;
                pData.pStringData       = fileContents.SliceL(header.nStringDataPosition, header.nStringDataSize);
            } catch (Exception e) {
                SetErrorDescription(string.Format("EXC StringData: {0}", e.Message));
                return(null);
            }

            // init binary nodes
            for (UInt32 nNode = 0; nNode < header.nNodeCount; ++nNode)
            {
                pData.pBinaryNodes[( int )nNode].m_pData      = pData; // add data space
                pData.pBinaryNodes[( int )nNode].m_pNodeIndex = nNode; // self ref..
            }

            // and back..
            result = EResult.Success;
            return(pData);
        }