• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using System;
11 using System.IO;
12 
13 namespace Google.Protobuf.Examples.AddressBook
14 {
15     internal class AddPerson
16     {
17         /// <summary>
18         /// Builds a person based on user input
19         /// </summary>
PromptForAddress(TextReader input, TextWriter output)20         private static Person PromptForAddress(TextReader input, TextWriter output)
21         {
22             Person person = new Person();
23 
24             output.Write("Enter person ID: ");
25             person.Id = int.Parse(input.ReadLine());
26 
27             output.Write("Enter name: ");
28             person.Name = input.ReadLine();
29 
30             output.Write("Enter email address (blank for none): ");
31             string email = input.ReadLine();
32             if (email.Length > 0)
33             {
34                 person.Email = email;
35             }
36 
37             while (true)
38             {
39                 output.Write("Enter a phone number (or leave blank to finish): ");
40                 string number = input.ReadLine();
41                 if (number.Length == 0)
42                 {
43                     break;
44                 }
45 
46                 Person.Types.PhoneNumber phoneNumber = new Person.Types.PhoneNumber { Number = number };
47 
48                 output.Write("Is this a mobile, home, or work phone? ");
49                 String type = input.ReadLine();
50                 switch (type)
51                 {
52                     case "mobile":
53                         phoneNumber.Type = Person.Types.PhoneType.Mobile;
54                         break;
55                     case "home":
56                         phoneNumber.Type = Person.Types.PhoneType.Home;
57                         break;
58                     case "work":
59                         phoneNumber.Type = Person.Types.PhoneType.Work;
60                         break;
61                     default:
62                         output.Write("Unknown phone type. Using default.");
63                         break;
64                 }
65 
66                 person.Phones.Add(phoneNumber);
67             }
68             return person;
69         }
70 
71         /// <summary>
72         /// Entry point - loads an existing addressbook or creates a new one,
73         /// then writes it back to the file.
74         /// </summary>
Main(string[] args)75         public static int Main(string[] args)
76         {
77             if (args.Length != 1)
78             {
79                 Console.Error.WriteLine("Usage:  AddPerson ADDRESS_BOOK_FILE");
80                 return -1;
81             }
82 
83             AddressBook addressBook;
84 
85             if (File.Exists(args[0]))
86             {
87                 using Stream file = File.OpenRead(args[0]);
88                 addressBook = AddressBook.Parser.ParseFrom(file);
89             }
90             else
91             {
92                 Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
93                 addressBook = new AddressBook();
94             }
95 
96             // Add an address.
97             addressBook.People.Add(PromptForAddress(Console.In, Console.Out));
98 
99             // Write the new address book back to disk.
100             using (Stream output = File.OpenWrite(args[0]))
101             {
102                 addressBook.WriteTo(output);
103             }
104             return 0;
105         }
106     }
107 }