ListPeople.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. class ListPeople {
  36. /// <summary>
  37. /// Iterates though all people in the AddressBook and prints info about them.
  38. /// </summary>
  39. static void Print(AddressBook addressBook) {
  40. foreach (Person person in addressBook.PersonList) {
  41. Console.WriteLine("Person ID: {0}", person.Id);
  42. Console.WriteLine(" Name: {0}", person.Name);
  43. if (person.HasEmail) {
  44. Console.WriteLine(" E-mail address: {0}", person.Email);
  45. }
  46. foreach (Person.Types.PhoneNumber phoneNumber in person.PhoneList) {
  47. switch (phoneNumber.Type) {
  48. case Person.Types.PhoneType.MOBILE:
  49. Console.Write(" Mobile phone #: ");
  50. break;
  51. case Person.Types.PhoneType.HOME:
  52. Console.Write(" Home phone #: ");
  53. break;
  54. case Person.Types.PhoneType.WORK:
  55. Console.Write(" Work phone #: ");
  56. break;
  57. }
  58. Console.WriteLine(phoneNumber.Number);
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// Entry point - loads the addressbook and then displays it.
  64. /// </summary>
  65. public static int Main(string[] args) {
  66. if (args.Length != 1) {
  67. Console.Error.WriteLine("Usage: ListPeople ADDRESS_BOOK_FILE");
  68. return 1;
  69. }
  70. if (!File.Exists(args[0])) {
  71. Console.WriteLine("{0} doesn't exist. Add a person to create the file first.", args[0]);
  72. return 0;
  73. }
  74. // Read the existing address book.
  75. using (Stream stream = File.OpenRead(args[0])) {
  76. AddressBook addressBook = AddressBook.ParseFrom(stream);
  77. Print(addressBook);
  78. }
  79. return 0;
  80. }
  81. }
  82. }