-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put common token reader methods in one class
- Loading branch information
Showing
5 changed files
with
97 additions
and
167 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
ValveKeyValue/ValveKeyValue/Deserialization/KVTokenReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace ValveKeyValue.Deserialization | ||
{ | ||
abstract class KVTokenReader : IDisposable | ||
{ | ||
public KVTokenReader(TextReader textReader) | ||
{ | ||
Require.NotNull(textReader, nameof(textReader)); | ||
|
||
this.textReader = textReader; | ||
} | ||
|
||
protected TextReader textReader; | ||
protected int? peekedNext; | ||
protected bool disposed; | ||
|
||
public void Dispose() | ||
{ | ||
if (!disposed) | ||
{ | ||
textReader.Dispose(); | ||
textReader = null; | ||
|
||
disposed = true; | ||
} | ||
} | ||
|
||
protected char Next() | ||
{ | ||
int next; | ||
|
||
if (peekedNext.HasValue) | ||
{ | ||
next = peekedNext.Value; | ||
peekedNext = null; | ||
} | ||
else | ||
{ | ||
next = textReader.Read(); | ||
} | ||
|
||
if (IsEndOfFile(next)) | ||
{ | ||
throw new EndOfStreamException(); | ||
} | ||
|
||
return (char)next; | ||
} | ||
|
||
protected int Peek() | ||
{ | ||
if (peekedNext.HasValue) | ||
{ | ||
return peekedNext.Value; | ||
} | ||
|
||
var next = textReader.Read(); | ||
peekedNext = next; | ||
|
||
return next; | ||
} | ||
|
||
protected void ReadChar(char expectedChar) | ||
{ | ||
var next = Next(); | ||
if (next != expectedChar) | ||
{ | ||
throw new InvalidDataException($"The syntax is incorrect, expected '{expectedChar}' but got '{next}'."); | ||
} | ||
} | ||
|
||
protected void SwallowWhitespace() | ||
{ | ||
while (PeekWhitespace()) | ||
{ | ||
Next(); | ||
} | ||
} | ||
|
||
protected bool PeekWhitespace() | ||
{ | ||
var next = Peek(); | ||
return !IsEndOfFile(next) && char.IsWhiteSpace((char)next); | ||
} | ||
|
||
protected bool IsEndOfFile(int value) => value == -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters