Пример #1
0
        private static void LoadListsFrom <TType>(
            string basePath,
            string filter,
            Func <string, TType> hasher,
            Func <string, string> modifier,
            HashList <TType> list)
        {
            if (Directory.Exists(basePath) == false)
            {
                return;
            }

            foreach (var listPath in Directory.GetFiles(basePath, filter, SearchOption.AllDirectories))
            {
                using (var input = File.Open(listPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var reader = new StreamReader(input);

                    while (true)
                    {
                        var line = reader.ReadLine();
                        if (line == null)
                        {
                            break;
                        }

                        if (line.StartsWith(";"))
                        {
                            continue;
                        }

                        line = line.Trim();
                        if (line.Length <= 0)
                        {
                            continue;
                        }

                        if (modifier != null)
                        {
                            line = modifier(line);
                        }

                        var hash = hasher(line);

                        if (list.Lookup.ContainsKey(hash) &&
                            list.Lookup[hash] != line)
                        {
                            var otherLine = list.Lookup[hash];
                            throw new InvalidOperationException(
                                      string.Format(
                                          "hash collision ('{0}' vs '{1}')",
                                          line,
                                          otherLine));
                        }

                        list.Lookup[hash] = line;
                    }
                }
            }
        }
Пример #2
0
        public HashList <TType> LoadLists <TType>(
            string filter,
            Func <string, TType> hasher,
            Func <string, string> modifier)
        {
            var list = new HashList <TType>();

            foreach (var name in Dependencies)
            {
                var dependency = Manager[name];
                if (dependency != null)
                {
                    LoadListsFrom(
                        dependency.ListsPath,
                        filter,
                        hasher,
                        modifier,
                        list);
                }
            }

            LoadListsFrom(
                ListsPath,
                filter,
                hasher,
                modifier,
                list);

            return(list);
        }