fromFile - multiple declarations

Function fromFile

Reads structs from file.

void fromFile(S) (
  ref 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 fromFile member, it calls fromFile(f) on it.
  • If the member is itself a struct, it recurses by calling fromFile(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 calls readEPString or readEPShortString respectively. A missing UDA is an error and reported at compile time.
  • In all other cases the member is read using std.stdio.File.rawRead.

Parameters

NameDescription
s The struct variable to read.
f The File to read from.

See Also

toFile.

Function fromFile

Where s is an alias to a string annotated with either an @EPString(n) UDA or @EPShortString(n) UDA, reads s from file f in the appropriate Prospero formats.

void fromFile(alias s) (
  File f
);

Example

import std.stdio;

struct MyType
{
    ubyte b = 0xF;
    @EPShortString(5) string str = "Hello";
}
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;
tmp.rewind;

Record rec;
rec.fromFile(tmp);
assert(rec == r);

Example

import std.stdio;

enum soort_subcompartiment_type {rechthoek, extern_subcompartiment}

struct subcompartiment_type
{
    int start_opgebouwde_tank;
    struct {
        soort_subcompartiment_type soort_subcompartiment;
        union {
            struct {int one, two, three, four;};
            struct {double d_one, d_two;}
        }
    }
}

subcompartiment_type sc;
sc.soort_subcompartiment = soort_subcompartiment_type.rechthoek;
sc.one = 1;
sc.three = 3;

File tmp = File.tmpfile();
sc.toFile(tmp);  // <===
tmp.flush;
tmp.rewind;

subcompartiment_type sc2;
sc2.fromFile(tmp);
assert(sc2 == sc);

Example

import std.stdio;

enum soort_subcompartiment_type {rechthoek, extern_subcompartiment}

struct subcompartiment_type
{
    int start_opgebouwde_tank;
    struct {
        soort_subcompartiment_type soort_subcompartiment;
        union {
            struct {int one, two, three, four;};
            struct {double d_one, d_two;
                    @EPString(80) string name;
                   }
        }
    }
}

subcompartiment_type sc;
sc.soort_subcompartiment = soort_subcompartiment_type.rechthoek;
sc.one = 1;
sc.three = 3;
sc.name = "John Doe";

File tmp = File.tmpfile();
sc.toFile(tmp);  // <===
tmp.flush;
tmp.rewind;

subcompartiment_type sc2;
sc2.fromFile(tmp);
assert(sc2 == sc);