コード例 #1
0
        /// <summary>
        /// Iterates though all people in the AddressBook and prints info about them.
        /// </summary>
        private static long Print(Xml.AddressBook addressBook)
        {
            Console.WriteLine("*****List AddressBookXml*****");
            watch.Restart();
            foreach (Xml.Person person in addressBook.people)
            {
                Console.WriteLine("Person ID: {0}", person.Id);
                Console.WriteLine("  Name: {0}", person.Name);
                if (person.Email != "")
                {
                    Console.WriteLine("  E-mail address: {0}", person.Email);
                }

                foreach (Xml.PersonPhoneNumber phoneNumber in person.Phones)
                {
                    switch (phoneNumber.Type)
                    {
                    case Xml.PersonPhoneNumberType.Mobile:
                        Console.Write("  Mobile phone #: ");
                        break;

                    case Xml.PersonPhoneNumberType.Home:
                        Console.Write("  Home phone #: ");
                        break;

                    case Xml.PersonPhoneNumberType.Work:
                        Console.Write("  Work phone #: ");
                        break;
                    }
                    Console.WriteLine(phoneNumber.Number);
                }
            }
            watch.Stop();
            return(watch.ElapsedMilliseconds);
        }
コード例 #2
0
        /// <summary>
        /// Entry point - loads an existing addressbook or creates a new one,
        /// then writes it back to the file.
        /// </summary>
        public static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.Error.WriteLine("Usage:  AddPerson ADDRESS_BOOK_FILE");
                return(-1);
            }

            AddressBook addressBook;

            Xml.AddressBook  xmlAddressBook;
            Json.AddressBook jsonAddressBook;

            if (File.Exists(args[0]))
            {
                using (Stream file = File.OpenRead(args[0]))
                {
                    addressBook = AddressBook.Parser.ParseFrom(file);
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
                addressBook = new AddressBook();
            }

            if (File.Exists(args[1]))
            {
                using (Stream file = File.OpenRead(args[1]))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                    xmlAddressBook = xmlSerializer.Deserialize(file) as Xml.AddressBook;
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[1]);
                xmlAddressBook = new Xml.AddressBook();
            }

            if (File.Exists(args[2]))
            {
                using (StreamReader stream = new StreamReader(args[2]))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    JsonTextReader reader     = new JsonTextReader(stream);
                    jsonAddressBook = serializer.Deserialize(reader, typeof(Json.AddressBook)) as Json.AddressBook;
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[2]);
                jsonAddressBook = new Json.AddressBook();
            }

            // Add an address.
            var persons = PromptForAddress(Console.In, Console.Out);

            addressBook.People.Add(persons[0] as Person);

            Xml.Person[] newValue;
            if (xmlAddressBook.people != null)
            {
                newValue = new Xml.Person[xmlAddressBook.people.Length + 1];
                Array.Copy(xmlAddressBook.people, newValue, xmlAddressBook.people.Length);
                newValue[xmlAddressBook.people.Length] = persons[1] as Xml.Person;
            }
            else
            {
                newValue    = new Xml.Person[1];
                newValue[0] = persons[1] as Xml.Person;
            }

            xmlAddressBook.people = newValue;

            if (jsonAddressBook.People == null)
            {
                jsonAddressBook.People = new System.Collections.ObjectModel.ObservableCollection <Json.Person>();
            }

            jsonAddressBook.People.Add(persons[2] as Json.Person);

            // Write the new proto address book back to disk.
            using (Stream output = File.OpenWrite(args[0]))
            {
                addressBook.WriteTo(output);
            }

            // Write the new xml address book back to disk.
            using (Stream output = File.OpenWrite(args[1]))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                xmlSerializer.Serialize(output, xmlAddressBook);
            }

            // Write the new json address book back to disk
            using (StreamWriter stream = new StreamWriter(args[2]))
            {
                JsonSerializer serializer = new JsonSerializer();
                JsonTextWriter writer     = new JsonTextWriter(stream);
                serializer.Serialize(writer, jsonAddressBook, typeof(Json.AddressBook));
            }
            return(0);
        }
コード例 #3
0
        /// <summary>
        /// Entry point - loads an existing addressbook or creates a new one,
        /// then writes it back to the file.
        /// </summary>
        public static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.Error.WriteLine("Usage:  GeneratePersons ADDRESS_BOOK_FILE");
                return(-1);
            }

            AddressBook addressBook;

            Xml.AddressBook  xmlAddressBook;
            Json.AddressBook jsonAddressBook;
            long             protoSave = 0, protoGen = 0, protoCount = 0,
                             xmlSave = 0, xmlGen = 0, xmlCount = 0,
                             jsonSave = 0, jsonGen = 0, jsonCount = 0;

            if (File.Exists(args[0]))
            {
                using (Stream file = File.OpenRead(args[0]))
                {
                    addressBook = AddressBook.Parser.ParseFrom(file);
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
                addressBook = new AddressBook();
            }

            if (File.Exists(args[1]))
            {
                using (Stream file = File.OpenRead(args[1]))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                    xmlAddressBook = xmlSerializer.Deserialize(file) as Xml.AddressBook;
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[1]);
                xmlAddressBook = new Xml.AddressBook();
            }

            if (File.Exists(args[2]))
            {
                using (StreamReader stream = new StreamReader(args[2]))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    JsonTextReader reader     = new JsonTextReader(stream);
                    jsonAddressBook = serializer.Deserialize(reader, typeof(Json.AddressBook)) as Json.AddressBook;
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[2]);
                jsonAddressBook = new Json.AddressBook();
            }

            Console.WriteLine("Enter number of persons: ");
            string persons  = Console.ReadLine();
            int    nPersons = 0;

            while (!Int32.TryParse(persons, out nPersons))
            {
                Console.WriteLine("Enter Numeric Value");
                persons = Console.ReadLine();
            }

            // Add addresses proto.
            watch.Restart();
            for (int i = 0; i < nPersons; ++i)
            {
                addressBook.People.Add(GeneratePerson());
            }
            watch.Stop();
            protoGen   = watch.ElapsedMilliseconds;
            protoCount = addressBook.People.Count;

            // Add addresses Xml.
            watch.Restart();
            Xml.Person[] newValue;
            int          startIndex;

            if (xmlAddressBook.people != null)
            {
                newValue = new Xml.Person[xmlAddressBook.people.Length + nPersons];
                Array.Copy(xmlAddressBook.people, newValue, xmlAddressBook.people.Length);
                startIndex = xmlAddressBook.people.Length;
            }
            else
            {
                newValue   = new Xml.Person[nPersons];
                startIndex = 0;
            }

            for (int i = 0; i < nPersons; ++i)
            {
                newValue[i + startIndex] = GenerateXmlPerson();
            }

            xmlAddressBook.people = newValue;
            watch.Stop();
            xmlGen   = watch.ElapsedMilliseconds;
            xmlCount = xmlAddressBook.people.LongLength;

            // Add addresses json.
            watch.Restart();
            if (jsonAddressBook.People == null)
            {
                jsonAddressBook.People = new System.Collections.ObjectModel.ObservableCollection <Json.Person>();
            }

            for (int i = 0; i < nPersons; ++i)
            {
                jsonAddressBook.People.Add(GenerateJsonPerson());
            }
            watch.Stop();
            jsonGen   = watch.ElapsedMilliseconds;
            jsonCount = addressBook.People.Count;

            // Write the new proto address book back to disk.
            watch.Restart();
            using (Stream output = File.OpenWrite(args[0]))
            {
                addressBook.WriteTo(output);
            }
            watch.Stop();
            protoSave = watch.ElapsedMilliseconds;

            // Write the new xml address book back to disk.
            watch.Restart();
            using (Stream output = File.OpenWrite(args[1]))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                xmlSerializer.Serialize(output, xmlAddressBook);
            }
            watch.Stop();
            xmlSave = watch.ElapsedMilliseconds;

            // Write the new json address book back to disk.
            watch.Restart();
            using (StreamWriter stream = new StreamWriter(args[2]))
            {
                JsonSerializer serializer = new JsonSerializer();
                JsonTextWriter writer     = new JsonTextWriter(stream);
                serializer.Serialize(writer, jsonAddressBook, typeof(Json.AddressBook));
            }
            watch.Stop();
            jsonSave = watch.ElapsedMilliseconds;

            Console.WriteLine("*****Protocole Buffer*****");
            Console.WriteLine($"Time to generate {nPersons} records (ms): {protoGen}");
            Console.WriteLine($"Total records: {protoCount}");
            Console.WriteLine($"Time to save(ms): {protoSave}");

            Console.WriteLine("*****Xml*****");
            Console.WriteLine($"Time to generate {nPersons} records (ms): {xmlGen}");
            Console.WriteLine($"Total records: {xmlCount}");
            Console.WriteLine($"Time to save(ms): {xmlSave}");

            Console.WriteLine("*****Json*****");
            Console.WriteLine($"Time to generate {nPersons} records (ms): {jsonGen}");
            Console.WriteLine($"Total records: {jsonCount}");
            Console.WriteLine($"Time to save(ms): {jsonSave}");

            return(0);
        }
コード例 #4
0
        /// <summary>
        /// Entry point - loads the addressbook and then displays it.
        /// </summary>
        public static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                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.
            long protoRead, protoList, protoLength, protoCount,
                 xmlRead, xmlList, xmlLength, xmlCount,
                 jsonRead, jsonList, jsonLength, jsonCount;

            watch.Restart();
            using (Stream stream = File.OpenRead(args[0]))
            {
                AddressBook addressBook = AddressBook.Parser.ParseFrom(stream);
                watch.Stop();
                protoLength = stream.Length;
                protoRead   = watch.ElapsedMilliseconds;
                protoCount  = addressBook.People.Count;
                protoList   = Print(addressBook);
            }

            // Read the existing Xml address book.
            watch.Restart();
            using (Stream stream = File.OpenRead(args[1]))
            {
                XmlSerializer   xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                Xml.AddressBook addressBook   = xmlSerializer.Deserialize(stream) as Xml.AddressBook;
                watch.Stop();
                xmlLength = stream.Length;
                xmlRead   = watch.ElapsedMilliseconds;
                xmlCount  = addressBook.people.Length;
                xmlList   = Print(addressBook);
            }

            // Read the existing Json address book.
            watch.Restart();
            using (StreamReader stream = File.OpenText(args[2]))
            {
                JsonSerializer   serializer  = new JsonSerializer();
                JsonTextReader   reader      = new JsonTextReader(stream);
                Json.AddressBook addressBook = serializer.Deserialize(reader, typeof(Json.AddressBook)) as Json.AddressBook;
                watch.Stop();
                jsonLength = stream.BaseStream.Length;
                jsonRead   = watch.ElapsedMilliseconds;
                jsonCount  = addressBook.People.Count;
                jsonList   = Print(addressBook);
            }

            Console.WriteLine("*****Protocole Buffer*****");
            Console.WriteLine($"File size (bytes): {protoLength}");
            Console.WriteLine($"Time to read(ms): {protoRead}");
            Console.WriteLine($"Total records: {protoCount}");
            Console.WriteLine($"Time to display(ms): {protoList}");

            Console.WriteLine("*****Xml*****");
            Console.WriteLine($"File size (bytes): {xmlLength}");
            Console.WriteLine($"Time to read(ms): {xmlRead}");
            Console.WriteLine($"Total records: {xmlCount}");
            Console.WriteLine($"Time to display(ms): {xmlList}");

            Console.WriteLine("*****Json*****");
            Console.WriteLine($"File size (bytes): {jsonLength}");
            Console.WriteLine($"Time to read(ms): {jsonRead}");
            Console.WriteLine($"Total records: {jsonCount}");
            Console.WriteLine($"Time to display(ms): {jsonList}");

            return(0);
        }