Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(775)

Side by Side Diff: third_party/protobuf/examples/list_people.cc

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.people_size(); i++) {
12 const tutorial::Person& person = address_book.people(i);
13
14 cout << "Person ID: " << person.id() << endl;
15 cout << " Name: " << person.name() << endl;
16 if (person.email() != "") {
17 cout << " E-mail address: " << person.email() << endl;
18 }
19
20 for (int j = 0; j < person.phones_size(); j++) {
21 const tutorial::Person::PhoneNumber& phone_number = person.phones(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 }
OLDNEW
« no previous file with comments | « third_party/protobuf/examples/addressbook.proto ('k') | third_party/protobuf/examples/list_people.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698