I downloaded the capnproto for dlanguage and started tinkering the sample-addressbook.. My problem is, everytime I add a person to the addressbook, it still prints only one of the data in the file, instead of the whole file..
void writeAddressBook()
{
MessageBuilder message = new MessageBuilder();
auto addressbook = message.initRoot!AddressBook;
auto people = addressbook.initPeople(1);
auto person = people[0];
person.id = 123;
person.setName("Tomas");
person.email = "tomas@example.com";
auto personPhones = person.initPhones(1);
personPhones[0].number = "0917xxxxxxx";
personPhones[0].type = Person.PhoneNumber.Type.mobile;
person.employment.school = "School of Thoughts";
File f = File("1.dat","a+b");
SerializePacked.writeToUnbuffered(new FileDescriptor(f), message);
}
If I call the writeAddressBook() 4 times, then I have 4 People in the addressbook with the same name, The Problem is, everytime I print all of the data, it only prints the first one..
void printAddressBook()
{
File f = File("1.dat","r");
auto message = SerializePacked.readFromUnbuffered(new FileDescriptor(f));
auto addressbook = message.getRoot!AddressBook;
foreach(person; addressbook.people)
{
writefln("%s: %s", person.name, person.email);
foreach(phone; person.phones)
{
string typeName = "UNKNOWN";
switch(phone.type) with(Person.PhoneNumber.Type)
{
case mobile:
typeName = "mobile";
break;
case home:
typeName = "home";
break;
case work:
typeName = "work";
break;
default:
break;
}
writefln(" %s phone: %s", typeName, phone.number);
}
auto employment = person.employment;
switch(employment.which()) with(Person.Employment.Which)
{
case unemployed:
writefln(" unemployed");
break;
case employer:
writefln(" employer: %s", employment.employer);
break;
case school:
writefln(" student at: %s", employment.school);
break;
case selfEmployed:
writefln(" self-employed");
break;
default:
break;
}
}
}