toFile - multiple declarations
Function toFile
Writes structs to file.
void toFile(S)
(
S s,
File f
)
if (is(S == struct));
It looks at the members of the struct and selects the appropriate method:
- If the type of the member has a
toFile
member, it callstoFile(f)
on it. - If the member is itself a struct, it recurses by calling
toFile(f)
on it. - If the member is a string, it checks that its format and capacity are defined
with either an
@EPString(n)
UDA or@EPShortString(n)
UDA, and then callswriteAsEPString
orwriteAsEPShortString
respectively. A missing UDA is an error and reported at compile time. - In all other cases the member is written using
std
..stdio .File .rawWrite
Parameters
Name | Description |
---|---|
s | The struct variable to write. |
f | The File to write to. |
See Also
fromFile.
Function toFile
Where s is an alias to a string annotated with either an @EPString(n)
UDA or @EPShortString(n)
UDA,
writes s to file f in the appropriate Prospero formats.
void toFile(alias s)
(
File f
);
Example
import std .stdio;
struct MyType
{
ubyte b = 0xF;
@EPShortString(5) string str = "Hello";
// string error; // Must produce a compile time error. OK.
}
struct Record
{
int i;
@EPString(10) string str = "World!";
MyType n;
}
Record r;
r .str = "Mars!";
r .n .str = "Bye";
File tmp = File .tmpfile();
r .toFile(tmp); // <===
tmp .flush;
assert(tmp .size == 4 // int i
+ 10 // String
+ 2 // String unused length
+ 1 // ubyte
+ 1 // ShortString length
+ 5 // ShortString
);
tmp .rewind;
auto buf = tmp .rawRead(new ubyte[cast(uint)(tmp .size)]);
foreach (i, b; buf)
{
final switch (i)
{
case 0, 1, 2, 3, 9, 10, 11, 12, 13, 15, 21, 22:
assert(b == 0x0);
break;
case 4:
assert(b == 'M');
break;
case 5:
assert(b == 'a');
break;
case 6:
assert(b == 'r');
break;
case 7:
assert(b == 's');
break;
case 8:
assert(b == '!');
break;
case 14:
assert(b == 5); // unused length EP string
break;
case 16:
assert(b == 0xF);
break;
case 17:
assert(b == 3); // length of EP short string
break;
case 18:
assert(b == 'B');
break;
case 19:
assert(b == 'y');
break;
case 20:
assert(b == 'e');
break;
}
}
Example
@EPShortString(5) string str1 = "Hello";
@EPString(10) string str2 = "World!";
File tmp = File .tmpfile();
toFile!str1(tmp);
toFile!str2(tmp);
tmp .flush;
assert(tmp .size == 1 // ShortString length
+ 5 // ShortString
+ 10 // String
+ 2 // String unused length
);
tmp .rewind;
auto buf = tmp .rawRead(new ubyte[cast(uint)(tmp .size)]);
foreach (i, b; buf)
{
final switch (i)
{
case 12, 13, 14, 15, 17:
assert(b == 0x0);
break;
case 0:
assert(b == 5); // length of EP short string
break;
case 1:
assert(b == 'H');
break;
case 2:
assert(b == 'e');
break;
case 3, 4, 9:
assert(b == 'l');
break;
case 5:
assert(b == 'o');
break;
case 6:
assert(b == 'W');
break;
case 7:
assert(b == 'o');
break;
case 8:
assert(b == 'r');
break;
case 10:
assert(b == 'd');
break;
case 11:
assert(b == '!');
break;
case 16:
assert(b == 4); // unused length EP string
break;
}
}