1ffe3c632Sopenharmony_ci// See README.txt for information and build instructions.
2ffe3c632Sopenharmony_ci
3ffe3c632Sopenharmony_ci#include <fstream>
4ffe3c632Sopenharmony_ci#include <google/protobuf/util/time_util.h>
5ffe3c632Sopenharmony_ci#include <iostream>
6ffe3c632Sopenharmony_ci#include <string>
7ffe3c632Sopenharmony_ci
8ffe3c632Sopenharmony_ci#include "addressbook.pb.h"
9ffe3c632Sopenharmony_ci
10ffe3c632Sopenharmony_ciusing namespace std;
11ffe3c632Sopenharmony_ci
12ffe3c632Sopenharmony_ciusing google::protobuf::util::TimeUtil;
13ffe3c632Sopenharmony_ci
14ffe3c632Sopenharmony_ci// Iterates though all people in the AddressBook and prints info about them.
15ffe3c632Sopenharmony_civoid ListPeople(const tutorial::AddressBook& address_book) {
16ffe3c632Sopenharmony_ci  for (int i = 0; i < address_book.people_size(); i++) {
17ffe3c632Sopenharmony_ci    const tutorial::Person& person = address_book.people(i);
18ffe3c632Sopenharmony_ci
19ffe3c632Sopenharmony_ci    cout << "Person ID: " << person.id() << endl;
20ffe3c632Sopenharmony_ci    cout << "  Name: " << person.name() << endl;
21ffe3c632Sopenharmony_ci    if (person.email() != "") {
22ffe3c632Sopenharmony_ci      cout << "  E-mail address: " << person.email() << endl;
23ffe3c632Sopenharmony_ci    }
24ffe3c632Sopenharmony_ci
25ffe3c632Sopenharmony_ci    for (int j = 0; j < person.phones_size(); j++) {
26ffe3c632Sopenharmony_ci      const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
27ffe3c632Sopenharmony_ci
28ffe3c632Sopenharmony_ci      switch (phone_number.type()) {
29ffe3c632Sopenharmony_ci        case tutorial::Person::MOBILE:
30ffe3c632Sopenharmony_ci          cout << "  Mobile phone #: ";
31ffe3c632Sopenharmony_ci          break;
32ffe3c632Sopenharmony_ci        case tutorial::Person::HOME:
33ffe3c632Sopenharmony_ci          cout << "  Home phone #: ";
34ffe3c632Sopenharmony_ci          break;
35ffe3c632Sopenharmony_ci        case tutorial::Person::WORK:
36ffe3c632Sopenharmony_ci          cout << "  Work phone #: ";
37ffe3c632Sopenharmony_ci          break;
38ffe3c632Sopenharmony_ci        default:
39ffe3c632Sopenharmony_ci          cout << "  Unknown phone #: ";
40ffe3c632Sopenharmony_ci          break;
41ffe3c632Sopenharmony_ci      }
42ffe3c632Sopenharmony_ci      cout << phone_number.number() << endl;
43ffe3c632Sopenharmony_ci    }
44ffe3c632Sopenharmony_ci    if (person.has_last_updated()) {
45ffe3c632Sopenharmony_ci      cout << "  Updated: " << TimeUtil::ToString(person.last_updated()) << endl;
46ffe3c632Sopenharmony_ci    }
47ffe3c632Sopenharmony_ci  }
48ffe3c632Sopenharmony_ci}
49ffe3c632Sopenharmony_ci
50ffe3c632Sopenharmony_ci// Main function:  Reads the entire address book from a file and prints all
51ffe3c632Sopenharmony_ci//   the information inside.
52ffe3c632Sopenharmony_ciint main(int argc, char* argv[]) {
53ffe3c632Sopenharmony_ci  // Verify that the version of the library that we linked against is
54ffe3c632Sopenharmony_ci  // compatible with the version of the headers we compiled against.
55ffe3c632Sopenharmony_ci  GOOGLE_PROTOBUF_VERIFY_VERSION;
56ffe3c632Sopenharmony_ci
57ffe3c632Sopenharmony_ci  if (argc != 2) {
58ffe3c632Sopenharmony_ci    cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
59ffe3c632Sopenharmony_ci    return -1;
60ffe3c632Sopenharmony_ci  }
61ffe3c632Sopenharmony_ci
62ffe3c632Sopenharmony_ci  tutorial::AddressBook address_book;
63ffe3c632Sopenharmony_ci
64ffe3c632Sopenharmony_ci  {
65ffe3c632Sopenharmony_ci    // Read the existing address book.
66ffe3c632Sopenharmony_ci    fstream input(argv[1], ios::in | ios::binary);
67ffe3c632Sopenharmony_ci    if (!address_book.ParseFromIstream(&input)) {
68ffe3c632Sopenharmony_ci      cerr << "Failed to parse address book." << endl;
69ffe3c632Sopenharmony_ci      return -1;
70ffe3c632Sopenharmony_ci    }
71ffe3c632Sopenharmony_ci  }
72ffe3c632Sopenharmony_ci
73ffe3c632Sopenharmony_ci  ListPeople(address_book);
74ffe3c632Sopenharmony_ci
75ffe3c632Sopenharmony_ci  // Optional:  Delete all global objects allocated by libprotobuf.
76ffe3c632Sopenharmony_ci  google::protobuf::ShutdownProtobufLibrary();
77ffe3c632Sopenharmony_ci
78ffe3c632Sopenharmony_ci  return 0;
79ffe3c632Sopenharmony_ci}
80