1/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17using System;
18using System.Text;
19
20namespace FlatBuffers
21{
22    /// <summary>
23    /// All tables in the generated code derive from this struct, and add their own accessors.
24    /// </summary>
25    public struct Table
26    {
27        public int bb_pos;
28        public ByteBuffer bb;
29
30        public ByteBuffer ByteBuffer { get { return bb; } }
31
32        // Look up a field in the vtable, return an offset into the object, or 0 if the field is not
33        // present.
34        public int __offset(int vtableOffset)
35        {
36            int vtable = bb_pos - bb.GetInt(bb_pos);
37            return vtableOffset < bb.GetShort(vtable) ? (int)bb.GetShort(vtable + vtableOffset) : 0;
38        }
39
40        public static int __offset(int vtableOffset, int offset, ByteBuffer bb)
41        {
42            int vtable = bb.Length - offset;
43            return (int)bb.GetShort(vtable + vtableOffset - bb.GetInt(vtable)) + vtable;
44        }
45
46        // Retrieve the relative offset stored at "offset"
47        public int __indirect(int offset)
48        {
49            return offset + bb.GetInt(offset);
50        }
51
52        public static int __indirect(int offset, ByteBuffer bb)
53        {
54            return offset + bb.GetInt(offset);
55        }
56
57        // Create a .NET String from UTF-8 data stored inside the flatbuffer.
58        public string __string(int offset)
59        {
60            offset += bb.GetInt(offset);
61            var len = bb.GetInt(offset);
62            var startPos = offset + sizeof(int);
63            return Encoding.UTF8.GetString(bb.Data, startPos , len);
64        }
65
66        // Get the length of a vector whose offset is stored at "offset" in this object.
67        public int __vector_len(int offset)
68        {
69            offset += bb_pos;
70            offset += bb.GetInt(offset);
71            return bb.GetInt(offset);
72        }
73
74        // Get the start of data of a vector whose offset is stored at "offset" in this object.
75        public int __vector(int offset)
76        {
77            offset += bb_pos;
78            return offset + bb.GetInt(offset) + sizeof(int);  // data starts after the length
79        }
80
81        // Get the data of a vector whoses offset is stored at "offset" in this object as an
82        // ArraySegment&lt;byte&gt;. If the vector is not present in the ByteBuffer,
83        // then a null value will be returned.
84        public ArraySegment<byte>? __vector_as_arraysegment(int offset)
85        {
86            var o = this.__offset(offset);
87            if (0 == o)
88            {
89                return null;
90            }
91
92            var pos = this.__vector(o);
93            var len = this.__vector_len(o);
94            return new ArraySegment<byte>(this.bb.Data, pos, len);
95        }
96
97        // Initialize any Table-derived type to point to the union at the given offset.
98        public T __union<T>(int offset) where T : struct, IFlatbufferObject
99        {
100            offset += bb_pos;
101            T t = new T();
102            t.__init(offset + bb.GetInt(offset), bb);
103            return t;
104        }
105
106        public static bool __has_identifier(ByteBuffer bb, string ident)
107        {
108            if (ident.Length != FlatBufferConstants.FileIdentifierLength)
109                throw new ArgumentException("FlatBuffers: file identifier must be length " + FlatBufferConstants.FileIdentifierLength, "ident");
110
111            for (var i = 0; i < FlatBufferConstants.FileIdentifierLength; i++)
112            {
113                if (ident[i] != (char)bb.Get(bb.Position + sizeof(int) + i)) return false;
114            }
115
116            return true;
117        }
118
119        // Compare strings in the ByteBuffer.
120        public static int CompareStrings(int offset_1, int offset_2, ByteBuffer bb)
121        {
122            offset_1 += bb.GetInt(offset_1);
123            offset_2 += bb.GetInt(offset_2);
124            var len_1 = bb.GetInt(offset_1);
125            var len_2 = bb.GetInt(offset_2);
126            var startPos_1 = offset_1 + sizeof(int);
127            var startPos_2 = offset_2 + sizeof(int);
128            var len = Math.Min(len_1, len_2);
129            byte[] bbArray = bb.Data;
130            for(int i = 0; i < len; i++) {
131                if (bbArray[i + startPos_1] != bbArray[i + startPos_2])
132                    return bbArray[i + startPos_1] - bbArray[i + startPos_2];
133            }
134            return len_1 - len_2;
135        }
136
137        // Compare string from the ByteBuffer with the string object
138        public static int CompareStrings(int offset_1, byte[] key, ByteBuffer bb)
139        {
140            offset_1 += bb.GetInt(offset_1);
141            var len_1 = bb.GetInt(offset_1);
142            var len_2 = key.Length;
143            var startPos_1 = offset_1 + sizeof(int);
144            var len = Math.Min(len_1, len_2);
145            byte[] bbArray = bb.Data;
146            for (int i = 0; i < len; i++) {
147                if (bbArray[i + startPos_1] != key[i])
148                    return bbArray[i + startPos_1] - key[i];
149            }
150            return len_1 - len_2;
151        }
152    }
153}
154