blob: 68e5666d84e6f729db7cbab9ff3e598671539420 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// See README.txt for information and build instructions.
2
3#include <iostream>
4#include <fstream>
5#include <string>
6#include "addressbook.pb.h"
7using namespace std;
8
9// Iterates though all people in the AddressBook and prints info about them.
10void ListPeople(const tutorial::AddressBook& address_book) {
Jan Tattermuschb0e5ba62015-07-20 15:24:08 -070011 for (int i = 0; i < address_book.people_size(); i++) {
12 const tutorial::Person& person = address_book.people(i);
temporal40ee5512008-07-10 02:12:20 +000013
14 cout << "Person ID: " << person.id() << endl;
15 cout << " Name: " << person.name() << endl;
Jan Tattermusch78709f22015-07-20 14:33:36 -070016 if (person.email() != "") {
temporal40ee5512008-07-10 02:12:20 +000017 cout << " E-mail address: " << person.email() << endl;
18 }
19
Jan Tattermusch78709f22015-07-20 14:33:36 -070020 for (int j = 0; j < person.phones_size(); j++) {
21 const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
temporal40ee5512008-07-10 02:12:20 +000022
23 switch (phone_number.type()) {
24 case tutorial::Person::MOBILE:
25 cout << " Mobile phone #: ";
26 break;
27 case tutorial::Person::HOME:
28 cout << " Home phone #: ";
29 break;
30 case tutorial::Person::WORK:
31 cout << " Work phone #: ";
32 break;
33 }
34 cout << phone_number.number() << endl;
35 }
36 }
37}
38
39// Main function: Reads the entire address book from a file and prints all
40// the information inside.
41int main(int argc, char* argv[]) {
42 // Verify that the version of the library that we linked against is
43 // compatible with the version of the headers we compiled against.
44 GOOGLE_PROTOBUF_VERIFY_VERSION;
45
46 if (argc != 2) {
47 cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
48 return -1;
49 }
50
51 tutorial::AddressBook address_book;
52
53 {
54 // Read the existing address book.
55 fstream input(argv[1], ios::in | ios::binary);
56 if (!address_book.ParseFromIstream(&input)) {
57 cerr << "Failed to parse address book." << endl;
58 return -1;
59 }
60 }
61
62 ListPeople(address_book);
63
kenton@google.comd2fd0632009-07-24 01:00:35 +000064 // Optional: Delete all global objects allocated by libprotobuf.
65 google::protobuf::ShutdownProtobufLibrary();
66
temporal40ee5512008-07-10 02:12:20 +000067 return 0;
68}