AddPerson.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://github.com/jskeet/dotnet-protobufs/
  4. // Original C++/Java/Python code:
  5. // http://code.google.com/p/protobuf/
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived from
  19. // this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. using System;
  33. using System.IO;
  34. namespace Google.ProtocolBuffers.Examples.AddressBook
  35. {
  36. class AddPerson {
  37. /// <summary>
  38. /// Builds a person based on user input
  39. /// </summary>
  40. static Person PromptForAddress(TextReader input, TextWriter output) {
  41. Person.Builder person = Person.CreateBuilder();
  42. output.Write("Enter person ID: ");
  43. person.Id = int.Parse(input.ReadLine());
  44. output.Write("Enter name: ");
  45. person.Name = input.ReadLine();
  46. output.Write("Enter email address (blank for none): ");
  47. string email = input.ReadLine();
  48. if (email.Length > 0) {
  49. person.Email = email;
  50. }
  51. while (true) {
  52. output.Write("Enter a phone number (or leave blank to finish): ");
  53. string number = input.ReadLine();
  54. if (number.Length == 0) {
  55. break;
  56. }
  57. Person.Types.PhoneNumber.Builder phoneNumber =
  58. Person.Types.PhoneNumber.CreateBuilder().SetNumber(number);
  59. output.Write("Is this a mobile, home, or work phone? ");
  60. String type = input.ReadLine();
  61. switch (type) {
  62. case "mobile":
  63. phoneNumber.Type = Person.Types.PhoneType.MOBILE;
  64. break;
  65. case "home":
  66. phoneNumber.Type = Person.Types.PhoneType.HOME;
  67. break;
  68. case "work":
  69. phoneNumber.Type = Person.Types.PhoneType.WORK;
  70. break;
  71. default:
  72. output.Write("Unknown phone type. Using default.");
  73. break;
  74. }
  75. person.AddPhone(phoneNumber);
  76. }
  77. return person.Build();
  78. }
  79. /// <summary>
  80. /// Entry point - loads an existing addressbook or creates a new one,
  81. /// then writes it back to the file.
  82. /// </summary>
  83. public static int Main(string[] args) {
  84. if (args.Length != 1) {
  85. Console.Error.WriteLine("Usage: AddPerson ADDRESS_BOOK_FILE");
  86. return -1;
  87. }
  88. AddressBook.Builder addressBook = AddressBook.CreateBuilder();
  89. if (File.Exists(args[0])) {
  90. using (Stream file = File.OpenRead(args[0])) {
  91. addressBook.MergeFrom(file);
  92. }
  93. } else {
  94. Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
  95. }
  96. // Add an address.
  97. addressBook.AddPerson(PromptForAddress(Console.In, Console.Out));
  98. // Write the new address book back to disk.
  99. using (Stream output = File.OpenWrite(args[0])) {
  100. addressBook.Build().WriteTo(output);
  101. }
  102. return 0;
  103. }
  104. }
  105. }