Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1 | #region Copyright notice and license
|
| 2 | // Protocol Buffers - Google's data interchange format
|
| 3 | // Copyright 2008 Google Inc. All rights reserved.
|
| 4 | // https://developers.google.com/protocol-buffers/
|
| 5 | //
|
| 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.
|
| 31 | #endregion
|
| 32 |
|
| 33 | using System;
|
| 34 | using System.IO;
|
| 35 |
|
| 36 | namespace Google.Protobuf.Examples.AddressBook
|
| 37 | {
|
| 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 | {
|
| 45 | Person person = new Person();
|
| 46 |
|
| 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 |
|
| 69 | Person.Types.PhoneNumber phoneNumber = new Person.Types.PhoneNumber { Number = number };
|
| 70 |
|
| 71 | output.Write("Is this a mobile, home, or work phone? ");
|
| 72 | String type = input.ReadLine();
|
| 73 | switch (type)
|
| 74 | {
|
| 75 | case "mobile":
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 76 | phoneNumber.Type = Person.Types.PhoneType.Mobile;
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 77 | break;
|
| 78 | case "home":
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 79 | phoneNumber.Type = Person.Types.PhoneType.Home;
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 80 | break;
|
| 81 | case "work":
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 82 | phoneNumber.Type = Person.Types.PhoneType.Work;
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 83 | break;
|
| 84 | default:
|
| 85 | output.Write("Unknown phone type. Using default.");
|
| 86 | break;
|
| 87 | }
|
| 88 |
|
| 89 | person.Phones.Add(phoneNumber);
|
| 90 | }
|
| 91 | return person;
|
| 92 | }
|
| 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 |
|
| 106 | AddressBook addressBook;
|
| 107 |
|
| 108 | if (File.Exists(args[0]))
|
| 109 | {
|
| 110 | using (Stream file = File.OpenRead(args[0]))
|
| 111 | {
|
| 112 | addressBook = AddressBook.Parser.ParseFrom(file);
|
| 113 | }
|
| 114 | }
|
| 115 | else
|
| 116 | {
|
| 117 | Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
|
| 118 | addressBook = new AddressBook();
|
| 119 | }
|
| 120 |
|
| 121 | // Add an address.
|
| 122 | addressBook.People.Add(PromptForAddress(Console.In, Console.Out));
|
| 123 |
|
| 124 | // Write the new address book back to disk.
|
| 125 | using (Stream output = File.OpenWrite(args[0]))
|
| 126 | {
|
| 127 | addressBook.WriteTo(output);
|
| 128 | }
|
| 129 | return 0;
|
| 130 | }
|
| 131 | }
|
| 132 | } |