Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename TextReader and TextWriter to TextDataReader and TextDataWriter #162

Merged
merged 1 commit into from Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 22 additions & 21 deletions docs/guides/Yarhl-nutshell.md
Expand Up @@ -99,28 +99,28 @@ need to write padding bytes, then
[`WritePadding`](<xref:Yarhl.IO.DataWriter.WritePadding(System.Byte,System.Int32)>)
will be your friend.

### TextReader and TextWriter
### TextDataReader and TextDataWriter

So far, `DataReader` and `DataWriter` have been very useful when you are dealing
with a file that contains some integer fields for size or offset, arrays of
bytes and maybe null-terminated strings. But, what about if you need to work
with a file that only contains text and you are interested in reading line by
line? In that case, you need [`TextReader`](xref:Yarhl.IO.TextReader) and
[`TextWriter`](xref:Yarhl.IO.TextWriter).
line? In that case, you need [`TextDataReader`](xref:Yarhl.IO.TextDataReader)
and [`TextDataWriter`](xref:Yarhl.IO.TextDataWriter).

#### New lines

By default, `TextWriter` uses always (Windows too) the new line `\n`. It doesn't
use `\r\n`. The reason is that most file formats uses `\n` and in some games
having the `\r` may crash. It's sometimes difficult to notice that. If you want
to use any other new line string (you can even use `<br/>`), you just need to
change the [`NewLine`](xref:Yarhl.IO.TextWriter.NewLine) property.
By default, `TextDataWriter` uses always (Windows too) the new line `\n`. It
doesn't use `\r\n`. The reason is that most file formats uses `\n` and in some
games having the `\r` may crash. It's sometimes difficult to notice that. If you
want to use any other new line string (you can even use `<br/>`), you just need
to change the [`NewLine`](xref:Yarhl.IO.TextDataWriter.NewLine) property.

In the case of the `TextReader` the behavior is different. The default value for
the [`NewLine`](xref:Yarhl.IO.TextReader.NewLine) property depends on the OS
(Windows: `\r\n`, Unix: `\n`). In addition, we provided with an automatic
In the case of the `TextDataReader` the behavior is different. The default value
for the [`NewLine`](xref:Yarhl.IO.TextDataReader.NewLine) property depends on
the OS (Windows: `\r\n`, Unix: `\n`). In addition, we provided with an automatic
mechanism enabled by default:
[`AutoNewLine`](xref:Yarhl.IO.TextReader.AutoNewLine*). If it's enabled, you
[`AutoNewLine`](xref:Yarhl.IO.TextDataReader.AutoNewLine*). If it's enabled, you
don't need to know the line ending in advance because we will stop at `\n` and
remove the last `\r` if present. This is also useful if a file mix both line
endings. And remember, by setting the `NewLine` property `AutoNewLine` is
Expand Down Expand Up @@ -148,13 +148,14 @@ stream that confirms the encoding of the file. For instance, when using UTF-16,
the file will begin with the bytes `0xFEFF`. It also specifies if the encoding
is little-ending or big-endian (needed for UTF-16).

Our `TextReader` will skip the BOM (_if it's present_) at the beginning of the
file. In the case of the `TextWriter`, the behavior is defined by the property
[`AutoPreamble`](xref:Yarhl.IO.TextWriter.AutoPreamble) which is set to `false`
by default (again, some games may see it as unexpected bytes). When enabled, the
first write call will also write the BOM. You can also write it manually by
calling [`WritePreamble()`](xref:Yarhl.IO.TextWriter.WritePreamble) (but
remember, only if you are at the beginning of the stream).
Our `TextDataReader` will skip the BOM (_if it's present_) at the beginning of
the file. In the case of the `TextDataWriter`, the behavior is defined by the
property [`AutoPreamble`](xref:Yarhl.IO.TextDataWriter.AutoPreamble) which is
set to `false` by default (again, some games may see it as unexpected bytes).
When enabled, the first write call will also write the BOM. You can also write
it manually by calling
[`WritePreamble()`](xref:Yarhl.IO.TextDataWriter.WritePreamble) (but remember,
only if you are at the beginning of the stream).

I know... I talk too much... Let's continue!

Expand Down Expand Up @@ -206,7 +207,7 @@ public void SaveFile(string path)
```csharp
public void LoadFile(DataStream stream)
{
var reader = new TextReader(stream, Encoding.Unicode);
var reader = new TextDataReader(stream, Encoding.Unicode);

string firstLine = reader.ReadLine();
char[] someChars = reader.Read(4);
Expand All @@ -219,7 +220,7 @@ public void LoadFile(DataStream stream)

public void SaveFile(DataStream stream)
{
var writer = new TextWriter(stream) {
var writer = new TextDataWriter(stream) {
AutoPreamble = true,
};

Expand Down
10 changes: 5 additions & 5 deletions src/Yarhl.Media/Text/Binary2Po.cs
Expand Up @@ -39,7 +39,7 @@ public Po Convert(IBinary source)
if (source == null)
throw new ArgumentNullException(nameof(source));

TextReader reader = new TextReader(source.Stream);
TextDataReader reader = new TextDataReader(source.Stream);
Po po = new Po();

// Read the header if any
Expand All @@ -60,7 +60,7 @@ public Po Convert(IBinary source)
return po;
}

static PoEntry ReadEntry(TextReader reader)
static PoEntry ReadEntry(TextDataReader reader)
{
// Skip all the blank lines before the block of text
string line = string.Empty;
Expand All @@ -86,7 +86,7 @@ static PoEntry ReadEntry(TextReader reader)
return entry;
}

static void ParseLine(TextReader reader, PoEntry entry, string line)
static void ParseLine(TextDataReader reader, PoEntry entry, string line)
{
string[] fields = line.Split(new[] { ' ' }, 2);
if (fields.Length != 2)
Expand Down Expand Up @@ -207,7 +207,7 @@ static void ParseHeaderLine(PoHeader header, string key, string value)
}
}

static string ReadMultiLineComment(TextReader reader, string line, string comment)
static string ReadMultiLineComment(TextDataReader reader, string line, string comment)
{
StringBuilder builder = new StringBuilder(line + "\n");
while (reader.PeekToToken(" ") == comment) {
Expand All @@ -222,7 +222,7 @@ static string ReadMultiLineComment(TextReader reader, string line, string commen
return result.Remove(result.Length - 1, 1).Replace("\n ", "\n");
}

static string ReadMultiLineContent(TextReader reader, string currentLine)
static string ReadMultiLineContent(TextDataReader reader, string currentLine)
{
StringBuilder content = new StringBuilder(ParseMultiLine(currentLine));

Expand Down
10 changes: 5 additions & 5 deletions src/Yarhl.Media/Text/Po2Binary.cs
Expand Up @@ -39,7 +39,7 @@ public BinaryFormat Convert(Po source)
throw new ArgumentNullException(nameof(source));

BinaryFormat binary = new BinaryFormat();
TextWriter writer = new TextWriter(binary.Stream);
TextDataWriter writer = new TextDataWriter(binary.Stream);

if (source.Header != null)
WriteHeader(source.Header, writer);
Expand All @@ -52,7 +52,7 @@ public BinaryFormat Convert(Po source)
return binary;
}

static void WriteHeader(PoHeader header, TextWriter writer)
static void WriteHeader(PoHeader header, TextDataWriter writer)
{
writer.WriteLine(@"msgid """"");
writer.WriteLine(@"msgstr """"");
Expand All @@ -72,7 +72,7 @@ static void WriteHeader(PoHeader header, TextWriter writer)
writer.WriteLine(@"""X-{0}: {1}\n""", entry.Key, entry.Value);
}

static void WriteEntry(PoEntry entry, TextWriter writer)
static void WriteEntry(PoEntry entry, TextDataWriter writer)
{
WriteIfNotEmpty(writer, "# {0}", entry.TranslatorComment);
WriteIfNotEmpty(writer, "#. {0}", entry.ExtractedComments);
Expand All @@ -93,7 +93,7 @@ static void WriteEntry(PoEntry entry, TextWriter writer)
WriteWrappedString(writer, entry.Translated);
}

static void WriteIfNotEmpty(TextWriter writer, string format, string content)
static void WriteIfNotEmpty(TextDataWriter writer, string format, string content)
{
if (!string.IsNullOrEmpty(content)) {
var lines = content.Split('\n');
Expand All @@ -103,7 +103,7 @@ static void WriteIfNotEmpty(TextWriter writer, string format, string content)
}
}

static void WriteWrappedString(TextWriter writer, string content)
static void WriteWrappedString(TextDataWriter writer, string content)
{
int idx = 0;
content = content.Replace("\n", "\\n");
Expand Down