Ejemplo n.º 1
0
        /// <summary>
        /// Gets the specified element without removing it.
        /// </summary>
        /// <param name="index"> The index of the specific element to peek. </param>
        /// <returns> The element at the specified position or default(T) when the queue was empty. </returns>
        /// <exception cref="LoggedException"> The index was out of bounds. </exception>
        /// <exception cref="LoggedException"> An unhandled exception occured whilst excecuting the method. </exception>
        public T Peek(int index)
        {
            EnterWriteLock();

            if (size == 0)
            {
                ExitWriteLock();
                return(default(T));
            }
            if (index >= size)
            {
                ExitWriteLock();
                LoggedException.Raise(nameof(ThreadSafeQueue <T>), "index must be smaller than the size of the queue", new IndexOutOfRangeException());
            }

            T result = data[(head + index) % data.Length];

            ExitWriteLock();
            return(result);
        }
Ejemplo n.º 2
0
 private static void RaiseLinqException(string method, Exception inner)
 {
     LoggedException.Raise(nameof(ArrayExtensions), $"{method} has encountered an unhandled exception", inner);
 }
Ejemplo n.º 3
0
        public static List <RETRoute> ReadRoutesFromFile(string fileName)
        {
            fileName = $"{Settings.Default.DataDirectory}{fileName}";
            List <RETRoute> result = new List <RETRoute>();

            curLine = 0;

            LoggedException.RaiseIf(!fileName.EndsWith(".PAS"), nameof(PASReader),
                                    $"Cannot open file with extension {Path.GetExtension(fileName)}, supply .pas file");

            try
            {
                using (StreamReader sr = new StreamReader(fileName))
                {
                    Log.Verbose(nameof(CSVReader), $"Started parsing file: '{Path.GetFileName(fileName)}'");
                    RETRoute cur = null;

                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        ++curLine;

                        try
                        {
                            switch (line[0])
                            {
                            case ('!'):
                                CheckFormat(line);
                                break;

                            case ('&'):
                                CheckValidity(line);
                                break;

                            case ('#'):
                                cur = ReadRouteLine(line);
                                break;

                            case ('-'):
                                Log.Debug(nameof(PASReader), "Skipped day code line (Useless)");
                                ReadDayCodeLine(line);
                                break;

                            case ('>'):
                                if (cur == null)
                                {
                                    Log.Error(nameof(PASReader), $"Floating stop at line: {curLine} (Ignored)");
                                }
                                else if (cur.Stops.Count > 0)
                                {
                                    Log.Error(nameof(PASReader), $"Second start stop at line: {curLine} (Ignored)");
                                }
                                else
                                {
                                    cur.Stops.Add(ReadStopStartLine(line));
                                }
                                break;

                            case ('.'):
                                if (cur == null)
                                {
                                    Log.Error(nameof(PASReader), $"Floating stop at line: {curLine} (Ignored)");
                                }
                                else if (cur.Stops.Count < 1)
                                {
                                    Log.Error(nameof(PASReader), $"Intermediate stop with no start at line: {curLine} (Ignored)");
                                }
                                else
                                {
                                    cur.Stops.Add(ReadStopLine(line));
                                }
                                break;

                            case ('+'):
                                if (cur == null)
                                {
                                    Log.Error(nameof(PASReader), $"Floating big stop at line: {curLine} (Ignored)");
                                }
                                else if (cur.Stops.Count < 1)
                                {
                                    Log.Error(nameof(PASReader), $"Big stop with no start at line: {curLine} (Ignored)");
                                }
                                else
                                {
                                    cur.Stops.Add(ReadBigStopLine(line));
                                }
                                break;

                            case ('<'):
                                if (cur == null)
                                {
                                    Log.Error(nameof(PASReader), $"Floating stop at line: {curLine} (Ignored)");
                                }
                                else if (cur.Stops.Count < 1)
                                {
                                    Log.Error(nameof(PASReader), $"End stop with no start at line: {curLine} (Ignored)");
                                }
                                else
                                {
                                    cur.Stops.Add(ReadStopEndLine(line));
                                }

                                result.Add(cur);
                                cur = null;
                                break;

                            default:
                                Log.Warning(nameof(PASReader), $"Unknown line type at line: {curLine}");
                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            LoggedException.Raise(nameof(PASReader), $"An unhandled excpetion was raised at line: {curLine}", e);
                        }
                    }

                    Log.Info(nameof(CSVReader), $"File contains {result.Count} readable entries");
                }
            }
            catch (Exception e)
            {
                Log.Fatal(nameof(CSVReader), new FileLoadException($"An unhandled exception was raised during the processing of file: '{Path.GetFullPath(fileName)}'", e));
            }

            Log.Verbose(nameof(CSVReader), $"Finished parsing file: '{Path.GetFileName(fileName)}'");
            return(result);
        }