-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParser.vb
215 lines (180 loc) · 6.58 KB
/
Parser.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#Region "Microsoft.VisualBasic::19a4c2bd7f409d5a1c246371174b4b0c, studio\RData\Parser.vb"
' Author:
'
' asuka ([email protected])
' xie ([email protected])
' xieguigang ([email protected])
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Code Statistics:
' Total Lines: 161
' Code Lines: 110 (68.32%)
' Comment Lines: 28 (17.39%)
' - Xml Docs: 100.00%
'
' Blank Lines: 23 (14.29%)
' File Size: 5.26 KB
' Module Parser
'
' Function: bits, bytes, decode, file_type, is_special_r_object_type
' parse_r_object_info, rdata_format
'
' /********************************************************************************/
#End Region
Imports System.IO
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.Data.IO
Imports Microsoft.VisualBasic.Language
Imports Microsoft.VisualBasic.Text
Imports Microsoft.VisualBasic.Text.Parser
Imports SMRUCC.Rsharp.RDataSet.Flags
Imports SMRUCC.Rsharp.RDataSet.Struct
''' <summary>
''' Parser interface for a R file.
''' </summary>
Module Parser
Public ReadOnly NA_INT As Integer = -1 * Math.Pow(2, 31)
Public Const NA_STRING As Integer = -1
''' <summary>
''' This is a special R constant value
''' </summary>
Public Const NA_REAL As Long = &H7FF00000000007A2
Friend ReadOnly magic_dict As New Dictionary(Of FileTypes, Byte()) From {
{FileTypes.bzip2, bytes("\x42\x5a\x68")},
{FileTypes.gzip, bytes("\x1f\x8b")},
{FileTypes.xz, bytes("\xFD7zXZ\x00")},
{FileTypes.rdata_binary_v2, bytes("RDX2\n")},
{FileTypes.rdata_binary_v3, bytes("RDX3\n")}
}
Friend ReadOnly format_dict As New Dictionary(Of RdataFormats, Byte()) From {
{RdataFormats.XDR, bytes("X\n")},
{RdataFormats.ASCII, bytes("A\n")},
{RdataFormats.binary, bytes("B\n")}
}
Private Function bytes(binaryStr As String) As Byte()
Dim bits As New List(Of Byte)
Dim bin As Char() = New Char(2 - 1) {}
Dim i As i32 = Scan0
Dim chars As CharPtr = binaryStr
Do While Not chars.EndRead
Dim c As Char = ++chars
If c = "\" AndAlso chars.Current = "x" Then
c = ++chars
bin(0) = ++chars
bin(1) = ++chars
bits += CByte(i32.GetHexInteger(bin(0) & bin(1)))
ElseIf c = "\" AndAlso chars.Current = "n" Then
c = ++chars
bits += CByte(ASCII.Byte.LF)
Else
bits += CByte(Asc(c))
End If
Loop
Return bits.ToArray
End Function
''' <summary>
''' Returns the type of the file.
''' </summary>
''' <param name="file"></param>
''' <returns></returns>
Public Function file_type(file As BinaryDataReader) As FileTypes
For Each item In magic_dict
If item.Value.SequenceEqual(file.ReadBytes(item.Value.Length)) Then
Return item.Key
Else
file.Seek(-item.Value.Length, SeekOrigin.Current)
End If
Next
Return Nothing
End Function
Public Function rdata_format(file As BinaryDataReader) As RdataFormats
For Each item In format_dict
If item.Value.SequenceEqual(file.ReadBytes(item.Value.Length)) Then
Return item.Key
Else
file.Seek(-item.Value.Length, SeekOrigin.Current)
End If
Next
Return Nothing
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
<Extension>
Friend Function decode(bytes As Byte(), encoding As Encodings) As String
Return encoding.CodePage.GetString(bytes)
End Function
''' <summary>
''' Parse the internal information of an object.
''' </summary>
''' <param name="info_int"></param>
''' <returns></returns>
Public Function parse_r_object_info(info_int As Integer) As RObjectInfo
Dim type_exp As RObjectType = bits(info_int, 0, 8)
Dim reference = 0
Dim object_flag As Boolean
Dim attributes As Boolean
Dim tag As Boolean
Dim gp As Integer
If is_special_r_object_type(type_exp) Then
object_flag = False
attributes = False
tag = False
gp = 0
Else
object_flag = CBool(bits(info_int, 8, 9))
attributes = CBool(bits(info_int, 9, 10))
tag = CBool(bits(info_int, 10, 11))
gp = bits(info_int, 12, 28)
End If
If type_exp = RObjectType.REF Then
reference = bits(info_int, 8, 32)
End If
Return New RObjectInfo With {
.type = type_exp,
.[object] = object_flag,
.attributes = attributes,
.tag = tag,
.gp = gp,
.reference = reference
}
End Function
''' <summary>
''' Check if a R type has a different serialization than the usual one.
''' </summary>
''' <param name="r_object_type"></param>
''' <returns></returns>
Public Function is_special_r_object_type(r_object_type As RObjectType) As Boolean
Return r_object_type = RObjectType.NILVALUE OrElse r_object_type = RObjectType.REF
End Function
''' <summary>
''' Read bits [start, stop) of an integer.
''' </summary>
''' <param name="data"></param>
''' <param name="start"></param>
''' <param name="[stop]"></param>
''' <returns></returns>
Public Function bits(data As Integer, start As Integer, [stop] As Integer) As Integer
Dim count = [stop] - start
Dim mask = ((1 << count) - 1) << start
Dim bitvalue = data And mask
Return bitvalue >> start
End Function
End Module