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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
// HtmlAgilityPack V1.3.1.0 - Simon Mourier <simonm@microsoft.com>
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace HtmlAgilityPack
{
/// <summary>
/// Represents a rewindable buffered TextReader specifically well suited for parsing operations.
/// </summary>
public class ParseReader: Stream
{
private StringBuilder _sb;
private int _baseReaderPosition;
private int _maxReaderPosition;
private int _position;
private TextReader _baseReader;
/// <summary>
/// Initializes an instance of the ParserReader class, based on an existing TextReader instance.
/// </summary>
/// <param name="baseReader">The TextReader to base parsing on. Must not be null.</param>
public ParseReader(TextReader baseReader)
{
if (baseReader == null)
throw new ArgumentNullException("baseReader");
_baseReader = baseReader;
_sb = new StringBuilder();
_position = 0;
_baseReaderPosition = 0;
_maxReaderPosition = int.MaxValue;
}
/// <summary>
/// Gets the length in bytes of the stream.
/// Always throws a NotSupportedException for the ParserReader class.
/// </summary>
public override long Length
{
get
{
throw new NotSupportedException();
}
}
/// <summary>
/// Gets or sets the position within the stream.
/// </summary>
public override long Position
{
get
{
return _position;
}
set
{
if (value < 0)
throw new ArgumentException("value is negative: " + value + ".");
if (value > int.MaxValue)
throw new ArgumentException("value must not be larger than int32 MaxValue.");
_position = (int)value;
}
}
/// <summary>
/// Checks the length of the underlying stream.
/// </summary>
/// <param name="length">The required length.</param>
/// <returns>true if the underlying stream's length is greater than the required length, false otherwise.</returns>
public bool CheckLength(int length)
{
if (length <= 0)
throw new ArgumentException("length must be greater than zero.");
if (BufferedTextLength >= length)
return true;
Seek(length, SeekOrigin.Begin);
return (BufferedTextLength >= length);
}
/// <summary>
/// Gets a value indicating whether the current stream supports seeking.
/// Always returns true for the ParserReader class.
/// </summary>
public override bool CanSeek
{
get
{
return true;
}
}
/// <summary>
/// Gets a value indicating whether the current stream supports reading.
/// Always returns true for the ParserReader class.
/// </summary>
public override bool CanRead
{
get
{
return true;
}
}
/// <summary>
/// Gets a value indicating whether the current stream supports writing.
/// Always returns false for the ParserReader class.
/// </summary>
public override bool CanWrite
{
get
{
return false;
}
}
/// <summary>
/// Sets the length of the current stream.
/// Always throws a NotSupportedException for the ParserReader class.
/// </summary>
/// <param name="value">The desired length of the current stream in bytes.</param>
public override void SetLength(long value)
{
throw new NotSupportedException();
}
/// <summary>
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
public override void Flush()
{
// nothing to do
}
/// <summary>
/// Gets the position within the underlying stream.
/// </summary>
public int BaseReaderPosition
{
get
{
return _baseReaderPosition;
}
}
/// <summary>
/// Gets the maximum position within the underlying stream.
/// </summary>
public int MaxReaderPosition
{
get
{
return _maxReaderPosition;
}
}
private void CheckBaseReader()
{
if (_baseReader == null)
throw new InvalidOperationException("Cannot read from a closed ParseReader.");
}
/// <summary>
/// Closes the current underlying stream.
/// </summary>
public void CloseBaseReader()
{
if (_maxReaderPosition != int.MaxValue) // we have already closed it
return;
CheckBaseReader();
_baseReader.Close();
_baseReader = null;
}
private void InternalCloseBaseReader()
{
CloseBaseReader();
_maxReaderPosition = _position;
}
/// <summary>
/// Returns the next available character but does not consume it.
/// </summary>
/// <returns>The next character to be read, or -1 if no more characters are available.</returns>
public int Peek()
{
if (_position < _baseReaderPosition)
return Convert.ToInt32(this[_position]);
if (_position == _maxReaderPosition)
return -1;
CheckBaseReader();
int i = _baseReader.Peek();
if (i < 0)
{
InternalCloseBaseReader();
return i;
}
Debug.Assert(_position >= _baseReaderPosition);
if (_position == _baseReaderPosition)
{
if (_sb.Length < (_position + 1))
{
_sb.Append(Convert.ToChar(i));
}
}
return i;
}
/// <summary>
/// Reads the next character and advances the character position by one character.
/// </summary>
/// <returns>The next character represented as an Int32, or -1 if no more characters are available.</returns>
public int Read()
{
int i;
if (_position < _baseReaderPosition)
{
i = Convert.ToInt32(_sb[_position]);
_position++;
return i;
}
if (_position == _maxReaderPosition)
return -1;
CheckBaseReader();
i = _baseReader.Read();
if (i < 0)
{
InternalCloseBaseReader();
return i;
}
if (_position >= _baseReaderPosition)
{
if (_sb.Length < (_position + 1))
{
_sb.Append(Convert.ToChar(i));
}
}
_baseReaderPosition++;
_position++;
return i;
}
/// <summary>
/// Move the position starting from the current position.
/// </summary>
/// <param name="count">A character offset relative to the current position.</param>
/// <returns>The new position.</returns>
public int Seek(int count)
{
int i;
if (count < 0)
{
if ((_position + count ) < 0)
{
i = _position;
_position = 0;
return i;
}
else
{
_position += count;
return - count;
}
}
for(i=0;i<count;i++)
{
int c = Read();
if (c < 0)
{
break;
}
}
return i;
}
/// <summary>
/// Reads a string from the current position.
/// </summary>
/// <param name="count">The number of characters to read.</param>
/// <returns>The read string or null, if an error occurred.</returns>
public string ReadString(int count)
{
int first = (int)Position;
Seek(count);
int last = (int)Position;
if (first >= _sb.Length)
return null;
return _sb.ToString(first, last - first);
}
/// <summary>
/// Reads a string, represented as an array of System.Int32, from the current position.
/// </summary>
/// <param name="count">The number of characters to read.</param>
/// <returns>The read string or null, if an error occurred.</returns>
public int[] Read(int count)
{
string s = ReadString(count);
if (s == null)
return null;
char[] chars = s.ToCharArray();
int[] ints = new int[chars.Length];
chars.CopyTo(ints, 0);
return ints;
}
/// <summary>
/// reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
/// </summary>
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count- 1) replaced by the bytes read from the current source.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
public override int Read(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
// we don't really know how to read count bytes... so we read count chars
string s = ReadString(count);
if (s == null)
return 0;
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s); // probably around 2*count bytes
int read = 0;
for(int i=0;i<bytes.Length;i++)
{
buffer[offset + i] = bytes[i];
read++;
if (read == count) // enough?
break;
}
return read;
}
/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// Always throws a NotSupportedException for the ParserReader class.
/// </summary>
/// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
/// <param name="count">The number of bytes to be written to the current stream.</param>
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
/// <summary>
/// Sets the position within the current stream.
/// </summary>
/// <param name="offset">A byte offset relative to the origin parameter.</param>
/// <param name="origin">A value of type SeekOrigin indicating the reference point used to obtain the new position.</param>
/// <returns>The new position within the current stream.</returns>
public override long Seek(long offset, SeekOrigin origin)
{
if (offset > int.MaxValue)
throw new ArgumentException("offset must not be larger than int32 MaxValue.");
switch(origin)
{
case SeekOrigin.Begin:
_position = 0;
break;
case SeekOrigin.End:
Seek(int.MaxValue);
break;
case SeekOrigin.Current:
break;
}
return Seek((int)offset);
}
/// <summary>
/// Gets the character at the specified index or -1 if no more characters are available.
/// </summary>
public int this[int index]
{
get
{
if (index >= _baseReaderPosition)
{
int count = Seek(index - _baseReaderPosition);
if (count < (index - _baseReaderPosition))
return -1;
int i = Peek();
if (i < 0)
return -1;
}
return _sb[index];
}
}
/// <summary>
/// Gets the length of the currently buffered text.
/// </summary>
public int BufferedTextLength
{
get
{
return _sb.Length;
}
}
/// <summary>
/// Gets the currently buffered text.
/// </summary>
public string BufferedText
{
get
{
return _sb.ToString();
}
}
/// <summary>
/// Extracts a string out of the buffered text.
/// </summary>
/// <param name="offset">The zero-based byte offset in buffered text at which to begin extracting.</param>
/// <param name="length">The maximum number of bytes to be read from the buffered text.</param>
/// <returns></returns>
public string GetBufferedString(int offset, int length)
{
if (offset > BufferedTextLength)
{
return null;
}
if ((offset + length) > BufferedTextLength)
{
length -= (offset + length) - BufferedTextLength;
}
return BufferedText.Substring(offset, length);
}
}
}
|