Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            do
            {
                Uri    path    = new Uri(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
                string newPath = "file:/" + String.Join("", path.Segments.TakeWhile((arg) => arg != "Deserialize/")
                                                        .Append("EmeraldCity/").Append("bin/").Append("Debug/").Append("out.ser"));
                StreetContainer container = StreetContainer.Deserialize((new Uri(newPath)).AbsolutePath);
                Console.WriteLine("Magic streets are \n");
                container?.Streets.Where((arg) => ~arg % 2 == 1 && !arg)
                .ToList().ForEach((arg) => Console.WriteLine(arg.ToString()));

                Console.WriteLine("Press esc to exit");
            } while (Console.ReadKey().Key != ConsoleKey.Escape);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            do
            {
                int           n     = Reader.Read <int>("Enter number of streets", "smth wrong, reenter pls", (arg) => arg > 0 && arg <= 100);
                List <string> lines = new List <string>();
                try
                {
                    //Read file content
                    using (StreamReader reader = new StreamReader("data.txt", Encoding.GetEncoding("Windows-1251")))
                    {
                        while (!reader.EndOfStream)
                        {
                            string line = reader.ReadLine();

                            lines.Add(line);
                        }
                    }
                }
                catch (IOException ex)
                {
                    Console.WriteLine("Error in reading from file");
                    Console.WriteLine(ex.Message);
                }

                List <Street> streetArray = new List <Street>();

                if (lines.Where((arg) => arg.Count() > 0).Any((arg) => !FileContentChecker.CheckLineContent(arg).Item1))
                {
                    Console.WriteLine("Warning, invalid file format!!");
                    for (int i = 0; i < n; ++i)
                    {
                        streetArray.Add(Street.MakeStreet());
                    }
                }
                else
                {
                    int nonzeroLine = lines.Count((arg) => arg.Count() >= 0);

                    int toTake = Math.Min(nonzeroLine, n);

                    streetArray.AddRange(lines.ConvertAll <Street>((arg) => FileContentChecker.CheckLineContent(arg).Item2).Take(toTake));
                }

                streetArray.ForEach((arg) => Console.WriteLine(arg + "\n"));

                //writing to file
                StreetContainer container = new StreetContainer(streetArray);
                try
                {
                    container.SerializeSelf(GetFilePath("out.ser"));
                }
                catch (InvalidOperationException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (System.Xml.XmlException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            } while (Console.ReadKey().Key != ConsoleKey.Escape);
        }