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