tizen 2.4 release
[external/protobuf.git] / examples / list_people.cc
1 // See README.txt for information and build instructions.
2
3 #include <iostream>
4 #include <fstream>
5 #include <string>
6 #include "addressbook.pb.h"
7 using namespace std;
8
9 // Iterates though all people in the AddressBook and prints info about them.
10 void ListPeople(const tutorial::AddressBook& address_book) {
11   for (int i = 0; i < address_book.person_size(); i++) {
12     const tutorial::Person& person = address_book.person(i);
13
14     cout << "Person ID: " << person.id() << endl;
15     cout << "  Name: " << person.name() << endl;
16     if (person.has_email()) {
17       cout << "  E-mail address: " << person.email() << endl;
18     }
19
20     for (int j = 0; j < person.phone_size(); j++) {
21       const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
22
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.
41 int 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
64   // Optional:  Delete all global objects allocated by libprotobuf.
65   google::protobuf::ShutdownProtobufLibrary();
66
67   return 0;
68 }