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