Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6 KiB

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