forked from JeremyDurnell/locbaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceTextReader.cs
More file actions
260 lines (230 loc) · 9.48 KB
/
ResourceTextReader.cs
File metadata and controls
260 lines (230 loc) · 9.48 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//---------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Description: ResourceTextReader class
// It reads values from a CSV file or tab-separated TXT file
//
//---------------------------------------------------------------------------
using System;
using System.IO;
using System.Text;
using System.Resources;
using System.Collections;
using System.Globalization;
using System.Diagnostics;
namespace BamlLocalization
{
/// <summary>
/// Reader that reads value from a CSV file or Tab-separated TXT file
/// </summary>
internal class ResourceTextReader : IDisposable
{
internal ResourceTextReader(TranslationFileType fileType, Stream stream)
{
_delimiter = LocBamlConst.GetDelimiter(fileType);
if(stream == null)
throw new ArgumentNullException("stream");
_reader = new StreamReader(stream);
}
internal bool ReadRow()
{
// currentChar is the first char after newlines
int currentChar = SkipAllNewLine();
if (currentChar < 0)
{
// nothing else to read
return false;
}
ReadState currentState = ReadState.TokenStart;
_columns = new ArrayList();
StringBuilder buffer = new StringBuilder();
while (currentState != ReadState.LineEnd)
{
switch(currentState)
{
// start of a token
case ReadState.TokenStart:
{
if (currentChar == _delimiter)
{
// it is the end of the token when we see a delimeter
// Store token, and reset state. and ignore this char
StoreTokenAndResetState(ref buffer, ref currentState);
}
else if (currentChar == '\"')
{
// jump to Quoted content if it token starts with a quote.
// and also ignore this quote
currentState = ReadState.QuotedContent;
}
else if (currentChar == '\n' ||
(currentChar == '\r' && _reader.Peek() == '\n'))
{
// we see a '\n' or '\r\n' sequence. Go to LineEnd
// ignore these chars
currentState = ReadState.LineEnd;
}
else
{
// safe to say that this is part of a unquoted content
buffer.Append((Char) currentChar);
currentState = ReadState.UnQuotedContent;
}
break;
}
// inside of an unquoted content
case ReadState.UnQuotedContent :
{
if (currentChar == _delimiter)
{
// It is then end of a toekn.
// Store the token value and reset state
// igore this char as well
StoreTokenAndResetState(ref buffer, ref currentState);
}
else if (currentChar == '\n' ||
(currentChar == '\r' && _reader.Peek() == '\n'))
{
// see a new line
// igorne these chars and jump to LineEnd
currentState = ReadState.LineEnd;
}
else
{
// we are good. store this char
// notice, even we see a '\"', we will just treat it like
// a normal char
buffer.Append((Char) currentChar);
}
break;
}
// inside of a quoted content
case ReadState.QuotedContent :
{
if (currentChar == '\"')
{
// now it depends on whether the next char is quote also
if (_reader.Peek() == '\"')
{
// we will ignore the next quote.
currentChar = _reader.Read();
buffer.Append( (Char) currentChar);
}
else
{ // we have a single quote. We fall back to unquoted content state
// and igorne the curernt quote
currentState = ReadState.UnQuotedContent;
}
}
else
{
// we are still inside of a quote, anything is accepted
buffer.Append((Char) currentChar);
}
break;
}
}
// read in the next char
currentChar = _reader.Read();
if (currentChar < 0)
{
// break out of the state machine if we reach the end of the file
break;
}
}
// we got to here either we are at LineEnd, or we are end of file
if (buffer.Length > 0)
{
_columns.Add(buffer.ToString());
}
return true;
}
internal string GetColumn(int index)
{
if (_columns!= null && index < _columns.Count && index >= 0)
{
return (string) _columns[index];
}
else
{
return null;
}
}
internal void Close()
{
if (_reader != null)
{
_reader.Close();
}
}
void IDisposable.Dispose()
{
Close();
}
//---------------------------------
// private functions
//---------------------------------
private void StoreTokenAndResetState(ref StringBuilder buffer, ref ReadState currentState)
{
// add the token into buffer. The token can be empty
_columns.Add(buffer.ToString());
// create a new buffer for the next token.
buffer = new StringBuilder();
// we continue to token state state
currentState = ReadState.TokenStart;
}
// skip all new line and return the first char after newlines.
// newline means '\r\n' or '\n'
private int SkipAllNewLine()
{
int _char;
while ((_char = _reader.Read())>=0)
{
if (_char == '\n')
{
continue; // continue if it is '\n'
}
else if (_char == '\r' && _reader.Peek() == '\n')
{
// skip the '\n' in the next position
_reader.Read();
// and continue
continue;
}
else
{
// stop here
break;
}
}
return _char;
}
private TextReader _reader; // internal text reader
private int _delimiter; // delimiter
private ArrayList _columns; // An arraylist storing all the columns of a row
/// <summary>
/// Enum representing internal states of the reader when reading
/// the CSV or tab-separated TXT file
/// </summary>
private enum ReadState
{
/// <summary>
/// State in which the reader is at the start of a column
/// </summary>
TokenStart,
/// <summary>
/// State in which the reader is reading contents that are quoted
/// </summary>
QuotedContent,
/// <summary>
/// State in which the reader is reading contents not in quotes
/// </summary>
UnQuotedContent,
/// <summary>
/// State in which the end of a line is reached
/// </summary>
LineEnd,
}
}
}