blob: 6eeb9f6e849db8aeb896f4db5b15231b84923b53 [file] [log] [blame]
csharptest71f662c2011-05-20 15:15:34 -05001#region Copyright notice and license
csharptest71f662c2011-05-20 15:15:34 -05002// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
Jon Skeetee835a32015-06-30 17:22:26 +01004// https://developers.google.com/protocol-buffers/
csharptest71f662c2011-05-20 15:15:34 -05005//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
csharptest71f662c2011-05-20 15:15:34 -050031#endregion
32
33using System;
34using System.IO;
35
Jon Skeetfb77cc92015-06-26 11:23:33 +010036namespace Google.Protobuf.Examples.AddressBook
csharptest71f662c2011-05-20 15:15:34 -050037{
38 internal class AddPerson
39 {
40 /// <summary>
41 /// Builds a person based on user input
42 /// </summary>
43 private static Person PromptForAddress(TextReader input, TextWriter output)
44 {
Jon Skeeteb70bd02015-06-12 09:53:44 +010045 Person person = new Person();
csharptest71f662c2011-05-20 15:15:34 -050046
47 output.Write("Enter person ID: ");
48 person.Id = int.Parse(input.ReadLine());
49
50 output.Write("Enter name: ");
51 person.Name = input.ReadLine();
52
53 output.Write("Enter email address (blank for none): ");
54 string email = input.ReadLine();
55 if (email.Length > 0)
56 {
57 person.Email = email;
58 }
59
60 while (true)
61 {
62 output.Write("Enter a phone number (or leave blank to finish): ");
63 string number = input.ReadLine();
64 if (number.Length == 0)
65 {
66 break;
67 }
68
Jon Skeeteb70bd02015-06-12 09:53:44 +010069 Person.Types.PhoneNumber phoneNumber = new Person.Types.PhoneNumber { Number = number };
csharptest71f662c2011-05-20 15:15:34 -050070
71 output.Write("Is this a mobile, home, or work phone? ");
72 String type = input.ReadLine();
73 switch (type)
74 {
75 case "mobile":
76 phoneNumber.Type = Person.Types.PhoneType.MOBILE;
77 break;
78 case "home":
79 phoneNumber.Type = Person.Types.PhoneType.HOME;
80 break;
81 case "work":
82 phoneNumber.Type = Person.Types.PhoneType.WORK;
83 break;
84 default:
85 output.Write("Unknown phone type. Using default.");
86 break;
87 }
88
Jan Tattermusch43b17372015-07-20 16:00:26 -070089 person.Phones.Add(phoneNumber);
csharptest71f662c2011-05-20 15:15:34 -050090 }
Jon Skeeteb70bd02015-06-12 09:53:44 +010091 return person;
csharptest71f662c2011-05-20 15:15:34 -050092 }
93
94 /// <summary>
95 /// Entry point - loads an existing addressbook or creates a new one,
96 /// then writes it back to the file.
97 /// </summary>
98 public static int Main(string[] args)
99 {
100 if (args.Length != 1)
101 {
102 Console.Error.WriteLine("Usage: AddPerson ADDRESS_BOOK_FILE");
103 return -1;
104 }
105
Jon Skeeteb70bd02015-06-12 09:53:44 +0100106 AddressBook addressBook;
csharptest71f662c2011-05-20 15:15:34 -0500107
108 if (File.Exists(args[0]))
109 {
110 using (Stream file = File.OpenRead(args[0]))
111 {
Jon Skeeteb70bd02015-06-12 09:53:44 +0100112 addressBook = AddressBook.Parser.ParseFrom(file);
csharptest71f662c2011-05-20 15:15:34 -0500113 }
114 }
115 else
116 {
117 Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
Jon Skeeteb70bd02015-06-12 09:53:44 +0100118 addressBook = new AddressBook();
csharptest71f662c2011-05-20 15:15:34 -0500119 }
120
121 // Add an address.
Jan Tattermusch43b17372015-07-20 16:00:26 -0700122 addressBook.People.Add(PromptForAddress(Console.In, Console.Out));
csharptest71f662c2011-05-20 15:15:34 -0500123
124 // Write the new address book back to disk.
125 using (Stream output = File.OpenWrite(args[0]))
126 {
Jon Skeeteb70bd02015-06-12 09:53:44 +0100127 addressBook.WriteTo(output);
csharptest71f662c2011-05-20 15:15:34 -0500128 }
129 return 0;
130 }
131 }
Jon Skeet3f225112008-11-24 16:09:39 +0000132}