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