add_person.cc 2.7 KB

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