aboutsummaryrefslogtreecommitdiff
path: root/src/AddressBook/ListPeople.cs
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2008-11-24 16:09:39 +0000
committerJon Skeet <skeet@pobox.com>2008-11-24 16:09:39 +0000
commit3f225111007e39c5d34c76d015c23596bf934658 (patch)
tree79c63014adc56306b46db1956dadd2e382978e87 /src/AddressBook/ListPeople.cs
parent828510cdbda47eff58682fd62c9166c39194810c (diff)
downloadprotobuf-3f225111007e39c5d34c76d015c23596bf934658.tar.gz
protobuf-3f225111007e39c5d34c76d015c23596bf934658.tar.bz2
protobuf-3f225111007e39c5d34c76d015c23596bf934658.zip
Added address book example
Diffstat (limited to 'src/AddressBook/ListPeople.cs')
-rw-r--r--src/AddressBook/ListPeople.cs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/AddressBook/ListPeople.cs b/src/AddressBook/ListPeople.cs
new file mode 100644
index 00000000..b2a9a964
--- /dev/null
+++ b/src/AddressBook/ListPeople.cs
@@ -0,0 +1,57 @@
+
+using System;
+using System.IO;
+
+namespace Google.ProtocolBuffers.Examples.AddressBook {
+ class ListPeople {
+ /// <summary>
+ /// Iterates though all people in the AddressBook and prints info about them.
+ /// </summary>
+ static void Print(AddressBook addressBook) {
+ foreach (Person person in addressBook.PersonList) {
+ Console.WriteLine("Person ID: {0}", person.Id);
+ Console.WriteLine(" Name: {0}", person.Name);
+ if (person.HasEmail) {
+ Console.WriteLine(" E-mail address: {0}", person.Email);
+ }
+
+ foreach (Person.Types.PhoneNumber phoneNumber in person.PhoneList) {
+ switch (phoneNumber.Type) {
+ case Person.Types.PhoneType.MOBILE:
+ Console.Write(" Mobile phone #: ");
+ break;
+ case Person.Types.PhoneType.HOME:
+ Console.Write(" Home phone #: ");
+ break;
+ case Person.Types.PhoneType.WORK:
+ Console.Write(" Work phone #: ");
+ break;
+ }
+ Console.WriteLine(phoneNumber.Number);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Entry point - loads the addressbook and then displays it.
+ /// </summary>
+ public static int Main(string[] args) {
+ if (args.Length != 1) {
+ Console.Error.WriteLine("Usage: ListPeople ADDRESS_BOOK_FILE");
+ return 1;
+ }
+
+ if (!File.Exists(args[0])) {
+ Console.WriteLine("{0} doesn't exist. Add a person to create the file first.", args[0]);
+ return 0;
+ }
+
+ // Read the existing address book.
+ using (Stream stream = File.OpenRead(args[0])) {
+ AddressBook addressBook = AddressBook.ParseFrom(stream);
+ Print(addressBook);
+ }
+ return 0;
+ }
+ }
+}