list_people.cc 2.21 KB
Newer Older
temporal's avatar
temporal committed
1 2 3
// See README.txt for information and build instructions.

#include <fstream>
4 5
#include <google/protobuf/util/time_util.h>
#include <iostream>
temporal's avatar
temporal committed
6
#include <string>
7

temporal's avatar
temporal committed
8
#include "addressbook.pb.h"
9

temporal's avatar
temporal committed
10 11
using namespace std;

12 13
using google::protobuf::util::TimeUtil;

temporal's avatar
temporal committed
14 15
// Iterates though all people in the AddressBook and prints info about them.
void ListPeople(const tutorial::AddressBook& address_book) {
16 17
  for (int i = 0; i < address_book.people_size(); i++) {
    const tutorial::Person& person = address_book.people(i);
temporal's avatar
temporal committed
18 19 20

    cout << "Person ID: " << person.id() << endl;
    cout << "  Name: " << person.name() << endl;
Jan Tattermusch's avatar
Jan Tattermusch committed
21
    if (person.email() != "") {
temporal's avatar
temporal committed
22 23 24
      cout << "  E-mail address: " << person.email() << endl;
    }

Jan Tattermusch's avatar
Jan Tattermusch committed
25 26
    for (int j = 0; j < person.phones_size(); j++) {
      const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
temporal's avatar
temporal committed
27 28 29 30 31 32 33 34 35 36 37

      switch (phone_number.type()) {
        case tutorial::Person::MOBILE:
          cout << "  Mobile phone #: ";
          break;
        case tutorial::Person::HOME:
          cout << "  Home phone #: ";
          break;
        case tutorial::Person::WORK:
          cout << "  Work phone #: ";
          break;
38 39 40
        default:
          cout << "  Unknown phone #: ";
          break;
temporal's avatar
temporal committed
41 42 43
      }
      cout << phone_number.number() << endl;
    }
44 45 46
    if (person.has_last_updated()) {
      cout << "  Updated: " << TimeUtil::ToString(person.last_updated()) << endl;
    }
temporal's avatar
temporal committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  }
}

// Main function:  Reads the entire address book from a file and prints all
//   the information inside.
int main(int argc, char* argv[]) {
  // Verify that the version of the library that we linked against is
  // compatible with the version of the headers we compiled against.
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  if (argc != 2) {
    cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
    return -1;
  }

  tutorial::AddressBook address_book;

  {
    // Read the existing address book.
    fstream input(argv[1], ios::in | ios::binary);
    if (!address_book.ParseFromIstream(&input)) {
      cerr << "Failed to parse address book." << endl;
      return -1;
    }
  }

  ListPeople(address_book);

75 76 77
  // Optional:  Delete all global objects allocated by libprotobuf.
  google::protobuf::ShutdownProtobufLibrary();

temporal's avatar
temporal committed
78 79
  return 0;
}