protobuf/examples/list_people.cc
Go to the documentation of this file.
1 // See README.txt for information and build instructions.
2 
3 #include <fstream>
4 #include <google/protobuf/util/time_util.h>
5 #include <iostream>
6 #include <string>
7 
8 #include "addressbook.pb.h"
9 
10 using namespace std;
11 
13 
14 // Iterates though all people in the AddressBook and prints info about them.
15 void ListPeople(const tutorial::AddressBook& address_book) {
16  for (int i = 0; i < address_book.people_size(); i++) {
17  const tutorial::Person& person = address_book.people(i);
18 
19  cout << "Person ID: " << person.id() << endl;
20  cout << " Name: " << person.name() << endl;
21  if (person.email() != "") {
22  cout << " E-mail address: " << person.email() << endl;
23  }
24 
25  for (int j = 0; j < person.phones_size(); j++) {
26  const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
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;
38  default:
39  cout << " Unknown phone #: ";
40  break;
41  }
42  cout << phone_number.number() << endl;
43  }
44  if (person.has_last_updated()) {
45  cout << " Updated: " << TimeUtil::ToString(person.last_updated()) << endl;
46  }
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.
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 
74 
75  // Optional: Delete all global objects allocated by libprotobuf.
77 
78  return 0;
79 }
ListPeople
void ListPeople(const tutorial::AddressBook &address_book)
Definition: protobuf/examples/list_people.cc:15
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
ToString
std::string ToString(const grpc::string_ref &r)
Definition: string_ref_helper.cc:24
GOOGLE_PROTOBUF_VERIFY_VERSION
#define GOOGLE_PROTOBUF_VERIFY_VERSION
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/common.h:116
google::protobuf::util::TimeUtil
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/util/time_util.h:62
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
main
int main(int argc, char *argv[])
Definition: protobuf/examples/list_people.cc:52
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
google::protobuf::ShutdownProtobufLibrary
void ShutdownProtobufLibrary()
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/common.cc:356
add_person.address_book
address_book
Definition: bloaty/third_party/protobuf/examples/add_person.py:49
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:15