blob: b309c596804739007510f06ff62c12898275fec7 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// See README.txt for information and build instructions.
2
Colin Cross11fb7ae2018-11-04 17:34:26 -08003#include <fstream>
Colin Cross86b21282019-08-13 18:26:41 -07004#include <google/protobuf/util/time_util.h>
5#include <iostream>
temporal40ee5512008-07-10 02:12:20 +00006#include <string>
Colin Cross86b21282019-08-13 18:26:41 -07007
temporal40ee5512008-07-10 02:12:20 +00008#include "addressbook.pb.h"
Colin Cross86b21282019-08-13 18:26:41 -07009
temporal40ee5512008-07-10 02:12:20 +000010using namespace std;
11
Colin Cross86b21282019-08-13 18:26:41 -070012using google::protobuf::util::TimeUtil;
13
temporal40ee5512008-07-10 02:12:20 +000014// Iterates though all people in the AddressBook and prints info about them.
15void ListPeople(const tutorial::AddressBook& address_book) {
Jan Tattermuschb0e5ba62015-07-20 15:24:08 -070016 for (int i = 0; i < address_book.people_size(); i++) {
17 const tutorial::Person& person = address_book.people(i);
temporal40ee5512008-07-10 02:12:20 +000018
19 cout << "Person ID: " << person.id() << endl;
20 cout << " Name: " << person.name() << endl;
Jan Tattermusch78709f22015-07-20 14:33:36 -070021 if (person.email() != "") {
temporal40ee5512008-07-10 02:12:20 +000022 cout << " E-mail address: " << person.email() << endl;
23 }
24
Jan Tattermusch78709f22015-07-20 14:33:36 -070025 for (int j = 0; j < person.phones_size(); j++) {
26 const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
temporal40ee5512008-07-10 02:12:20 +000027
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 Cross86b21282019-08-13 18:26:41 -070038 default:
39 cout << " Unknown phone #: ";
40 break;
temporal40ee5512008-07-10 02:12:20 +000041 }
42 cout << phone_number.number() << endl;
43 }
Colin Cross86b21282019-08-13 18:26:41 -070044 if (person.has_last_updated()) {
45 cout << " Updated: " << TimeUtil::ToString(person.last_updated()) << endl;
46 }
temporal40ee5512008-07-10 02:12:20 +000047 }
48}
49
50// Main function: Reads the entire address book from a file and prints all
51// the information inside.
52int 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.comd2fd0632009-07-24 01:00:35 +000075 // Optional: Delete all global objects allocated by libprotobuf.
76 google::protobuf::ShutdownProtobufLibrary();
77
temporal40ee5512008-07-10 02:12:20 +000078 return 0;
79}