1ffe3c632Sopenharmony_ci// See README.txt for information and build instructions.
2ffe3c632Sopenharmony_ci
3ffe3c632Sopenharmony_ci#include <ctime>
4ffe3c632Sopenharmony_ci#include <fstream>
5ffe3c632Sopenharmony_ci#include <google/protobuf/util/time_util.h>
6ffe3c632Sopenharmony_ci#include <iostream>
7ffe3c632Sopenharmony_ci#include <string>
8ffe3c632Sopenharmony_ci
9ffe3c632Sopenharmony_ci#include "addressbook.pb.h"
10ffe3c632Sopenharmony_ci
11ffe3c632Sopenharmony_ciusing namespace std;
12ffe3c632Sopenharmony_ci
13ffe3c632Sopenharmony_ciusing google::protobuf::util::TimeUtil;
14ffe3c632Sopenharmony_ci
15ffe3c632Sopenharmony_ci// This function fills in a Person message based on user input.
16ffe3c632Sopenharmony_civoid PromptForAddress(tutorial::Person* person) {
17ffe3c632Sopenharmony_ci  cout << "Enter person ID number: ";
18ffe3c632Sopenharmony_ci  int id;
19ffe3c632Sopenharmony_ci  cin >> id;
20ffe3c632Sopenharmony_ci  person->set_id(id);
21ffe3c632Sopenharmony_ci  cin.ignore(256, '\n');
22ffe3c632Sopenharmony_ci
23ffe3c632Sopenharmony_ci  cout << "Enter name: ";
24ffe3c632Sopenharmony_ci  getline(cin, *person->mutable_name());
25ffe3c632Sopenharmony_ci
26ffe3c632Sopenharmony_ci  cout << "Enter email address (blank for none): ";
27ffe3c632Sopenharmony_ci  string email;
28ffe3c632Sopenharmony_ci  getline(cin, email);
29ffe3c632Sopenharmony_ci  if (!email.empty()) {
30ffe3c632Sopenharmony_ci    person->set_email(email);
31ffe3c632Sopenharmony_ci  }
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ci  while (true) {
34ffe3c632Sopenharmony_ci    cout << "Enter a phone number (or leave blank to finish): ";
35ffe3c632Sopenharmony_ci    string number;
36ffe3c632Sopenharmony_ci    getline(cin, number);
37ffe3c632Sopenharmony_ci    if (number.empty()) {
38ffe3c632Sopenharmony_ci      break;
39ffe3c632Sopenharmony_ci    }
40ffe3c632Sopenharmony_ci
41ffe3c632Sopenharmony_ci    tutorial::Person::PhoneNumber* phone_number = person->add_phones();
42ffe3c632Sopenharmony_ci    phone_number->set_number(number);
43ffe3c632Sopenharmony_ci
44ffe3c632Sopenharmony_ci    cout << "Is this a mobile, home, or work phone? ";
45ffe3c632Sopenharmony_ci    string type;
46ffe3c632Sopenharmony_ci    getline(cin, type);
47ffe3c632Sopenharmony_ci    if (type == "mobile") {
48ffe3c632Sopenharmony_ci      phone_number->set_type(tutorial::Person::MOBILE);
49ffe3c632Sopenharmony_ci    } else if (type == "home") {
50ffe3c632Sopenharmony_ci      phone_number->set_type(tutorial::Person::HOME);
51ffe3c632Sopenharmony_ci    } else if (type == "work") {
52ffe3c632Sopenharmony_ci      phone_number->set_type(tutorial::Person::WORK);
53ffe3c632Sopenharmony_ci    } else {
54ffe3c632Sopenharmony_ci      cout << "Unknown phone type.  Using default." << endl;
55ffe3c632Sopenharmony_ci    }
56ffe3c632Sopenharmony_ci  }
57ffe3c632Sopenharmony_ci  *person->mutable_last_updated() = TimeUtil::SecondsToTimestamp(time(NULL));
58ffe3c632Sopenharmony_ci}
59ffe3c632Sopenharmony_ci
60ffe3c632Sopenharmony_ci// Main function:  Reads the entire address book from a file,
61ffe3c632Sopenharmony_ci//   adds one person based on user input, then writes it back out to the same
62ffe3c632Sopenharmony_ci//   file.
63ffe3c632Sopenharmony_ciint main(int argc, char* argv[]) {
64ffe3c632Sopenharmony_ci  // Verify that the version of the library that we linked against is
65ffe3c632Sopenharmony_ci  // compatible with the version of the headers we compiled against.
66ffe3c632Sopenharmony_ci  GOOGLE_PROTOBUF_VERIFY_VERSION;
67ffe3c632Sopenharmony_ci
68ffe3c632Sopenharmony_ci  if (argc != 2) {
69ffe3c632Sopenharmony_ci    cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
70ffe3c632Sopenharmony_ci    return -1;
71ffe3c632Sopenharmony_ci  }
72ffe3c632Sopenharmony_ci
73ffe3c632Sopenharmony_ci  tutorial::AddressBook address_book;
74ffe3c632Sopenharmony_ci
75ffe3c632Sopenharmony_ci  {
76ffe3c632Sopenharmony_ci    // Read the existing address book.
77ffe3c632Sopenharmony_ci    fstream input(argv[1], ios::in | ios::binary);
78ffe3c632Sopenharmony_ci    if (!input) {
79ffe3c632Sopenharmony_ci      cout << argv[1] << ": File not found.  Creating a new file." << endl;
80ffe3c632Sopenharmony_ci    } else if (!address_book.ParseFromIstream(&input)) {
81ffe3c632Sopenharmony_ci      cerr << "Failed to parse address book." << endl;
82ffe3c632Sopenharmony_ci      return -1;
83ffe3c632Sopenharmony_ci    }
84ffe3c632Sopenharmony_ci  }
85ffe3c632Sopenharmony_ci
86ffe3c632Sopenharmony_ci  // Add an address.
87ffe3c632Sopenharmony_ci  PromptForAddress(address_book.add_people());
88ffe3c632Sopenharmony_ci
89ffe3c632Sopenharmony_ci  {
90ffe3c632Sopenharmony_ci    // Write the new address book back to disk.
91ffe3c632Sopenharmony_ci    fstream output(argv[1], ios::out | ios::trunc | ios::binary);
92ffe3c632Sopenharmony_ci    if (!address_book.SerializeToOstream(&output)) {
93ffe3c632Sopenharmony_ci      cerr << "Failed to write address book." << endl;
94ffe3c632Sopenharmony_ci      return -1;
95ffe3c632Sopenharmony_ci    }
96ffe3c632Sopenharmony_ci  }
97ffe3c632Sopenharmony_ci
98ffe3c632Sopenharmony_ci  // Optional:  Delete all global objects allocated by libprotobuf.
99ffe3c632Sopenharmony_ci  google::protobuf::ShutdownProtobufLibrary();
100ffe3c632Sopenharmony_ci
101ffe3c632Sopenharmony_ci  return 0;
102ffe3c632Sopenharmony_ci}
103