Example #1
0
        public override void Present <T>()
        {
            var       ps = this.presenters;
            Presenter presenter;

            lock (this.locker)
            {
                presenter = EH.FirstOrDefault(
                    ps,
                    p => p is T);
            }

            if (presenter == null)
            {
                return;
            }

            lock (this.locker)
            {
                foreach (var p in ps)
                {
                    p.Stop();
                }
            }

            this.startPresenter?.Invoke(presenter);
        }
Example #2
0
        public virtual void PresentFluidly <T>()
            where T : Presenter
        {
            var presenter = EH.FirstOrDefault(
                this.presenters,
                p => p is T);

            if (presenter == null)
            {
                return;
            }

            this.startPresenter?.Invoke(presenter);
        }
Example #3
0
        public override void PresentFluidly <T>()
        {
            Presenter presenter;

            lock (this.locker)
            {
                presenter = EH.FirstOrDefault(
                    this.presenters,
                    p => p is T);
            }

            if (presenter == null)
            {
                return;
            }

            this.startPresenter?.Invoke(presenter);
        }
Example #4
0
        public virtual void Present <T>()
            where T : Presenter
        {
            var ps        = this.presenters;
            var presenter = EH.FirstOrDefault(
                ps,
                p => p is T);

            if (presenter == null)
            {
                return;
            }

            foreach (var p in ps)
            {
                p?.Stop();
            }

            this.startPresenter?.Invoke(presenter);
        }
Example #5
0
        public virtual ICollection <string> ReadKeysInSection(
            string sectionName)
        {
            string[] lines;
            try
            {
                lines = this.readLines();
            }
            catch
            {
                return(new LinkedList <string>());
            }

            var headers = this.readSectionHeaders(
                lines);
            var targetHeader = EH.FirstOrDefault(
                headers,
                h => h.Name == sectionName);

            return(this.readKeysInSectionProtected(
                       lines,
                       targetHeader));
        }
Example #6
0
        public virtual void ChangeValue(
            string sectionName,
            string key,
            string newValue)
        {
            string[] lines;
            try
            {
                lines = this.readLines();
            }
            catch
            {
                return;
            }

            var           sectionHeaders    = this.readSectionHeaders(lines);
            SectionHeader targetHeader      = default;
            var           nextHeaderCounter = 1;

            foreach (var header in sectionHeaders)
            {
                if (header.Name == sectionName)
                {
                    targetHeader = header;
                    break;
                }

                ++nextHeaderCounter;
            }
            if (targetHeader == default)
            {
                return;
            }

            if (!this.readKeysInSectionProtected(lines, targetHeader)
                .Contains(key))
            {
                return;
            }

            var nextHeader = EH.FirstOrDefault(
                EH.Skip(
                    sectionHeaders,
                    nextHeaderCounter));
            var startOfSection = targetHeader.LineNumber;
            var endOfSection   = lines.Length;

            if (nextHeader != default)
            {
                endOfSection = nextHeader.LineNumber - 1;
            }

            for (var i = startOfSection; i < endOfSection; ++i)
            {
                var line = lines[i];
                if (line == null)
                {
                    continue;
                }

                if (!line.StartsWith(key))
                {
                    continue;
                }

                var sb = new StringBuilder(
                    line);
                var valueIndex     = line.IndexOf('=') + 1;
                var indexOfComment = line.IndexOf(';');
                if (indexOfComment < 0)
                {
                    sb.Replace(
                        line.Substring(
                            valueIndex),
                        newValue);
                }
                else
                {
                    sb.Replace(
                        line.Substring(
                            valueIndex,
                            indexOfComment - valueIndex),
                        newValue);
                }

                lines[i] = sb.ToString();
                try
                {
                    this.writeLines(lines);
                }
                catch
                {
                    return;
                }

                return;
            }
        }
Example #7
0
        protected virtual string readValueProtected(
            string sectionName,
            string key,
            bool readEntireValue)
        {
            var lines   = this.readLines();
            var headers = this.readSectionHeaders(
                lines);
            var targetHeader = EH.FirstOrDefault(
                headers,
                header => header.Name == sectionName);

            if (targetHeader == default)
            {
                return(null);
            }

            if (!this
                .readKeysInSectionProtected(
                    lines,
                    targetHeader)
                .Contains(key))
            {
                return(null);
            }

            string targetLine = default;
            int    endOfKey;

            foreach (var line in EH.Skip(
                         lines,
                         targetHeader.LineNumber))
            {
                endOfKey = line.IndexOf('=');
                if (line.Substring(0, endOfKey) == key)
                {
                    targetLine = line;
                    break;
                }
            }

            if (targetLine == default)
            {
                return(null);
            }

            var startIndexOfValue = targetLine.IndexOf('=') + 1;
            int valueLength;
            var indexOfSemicolon = targetLine.IndexOf(';');

            if (indexOfSemicolon > -1 && !readEntireValue)
            {
                valueLength = indexOfSemicolon - startIndexOfValue;
                goto finish;
            }

            valueLength = targetLine.Length - startIndexOfValue;

finish:
            return(targetLine
                   .Substring(
                       startIndexOfValue,
                       valueLength)
                   .TrimEnd());
        }
Example #8
0
        protected virtual ICollection <string> readKeysInSectionProtected(
            string[] lines,
            SectionHeader targetHeader)
        {
            ICollection <string> keys = new LinkedList <string>();

            if (targetHeader == default)
            {
                return(keys);
            }

            var headers = this.readSectionHeaders(lines);

            if (!EH.Contains(
                    EH.Select(
                        headers,
                        header => header.Name),
                    targetHeader.Name))
            {
                return(keys);
            }

            var lineNumber = targetHeader.LineNumber;
            int lastLineIndex;

            if (EH.Last(
                    EH.Select(
                        headers,
                        header => header.Name)) == targetHeader.Name)
            {
                lastLineIndex = lines.Length - 1;
                goto readKeys;
            }

            var headerCounter = 1;

            foreach (var header in headers)
            {
                if (header.Name == targetHeader.Name)
                {
                    break;
                }

                ++headerCounter;
            }

            var nextHeader = EH.FirstOrDefault(
                EH.Skip(
                    headers, headerCounter));

            lastLineIndex = nextHeader?.LineNumber - 1
                            ?? lines.Length - 1;


readKeys:
            for (var i = lineNumber; i <= lastLineIndex; ++i)
            {
                var indexOfSemicolon = lines[i].IndexOf(';');
                if (indexOfSemicolon == 0)
                {
                    continue;
                }

                var indexOfEquals = lines[i].IndexOf('=');
                if (indexOfEquals < 0)
                {
                    continue;
                }

                if (indexOfSemicolon > -1 &&
                    indexOfSemicolon < indexOfEquals)
                {
                    continue;
                }

                var keyName = lines[i]
                              .Substring(0, indexOfEquals);
                keys.Add(keyName);
            }

            return(keys);
        }