<commit_before>﻿using Microsoft.Extensions.Options;
using Microsoft.Win32;
using System;
using System.Collections.Generic;

namespace TIKSN.Settings
{
    public class WindowsRegistrySettingsService : ISettingsService
    {
        private readonly IOptions<WindowsRegistrySettingsServiceOptions> _options;

        public WindowsRegistrySettingsService(IOptions<WindowsRegistrySettingsServiceOptions> options)
        {
            _options = options;
            ValidateOptions();
        }

        public T GetLocalSetting<T>(string name, T defaultValue)
        {
            return Process(RegistryHive.CurrentUser, name, defaultValue, GetSetting);
        }

        public T GetRoamingSetting<T>(string name, T defaultValue)
        {
            return Process(RegistryHive.LocalMachine, name, defaultValue, GetSetting);
        }

        public IReadOnlyCollection<string> ListLocalSetting()
        {
            return ListNames(RegistryHive.CurrentUser);
        }

        public IReadOnlyCollection<string> ListRoamingSetting()
        {
            return ListNames(RegistryHive.LocalMachine);
        }

        public void RemoveLocalSetting(string name)
        {
            Process<object>(RegistryHive.CurrentUser, name, null, RemoveSetting);
        }

        public void RemoveRoamingSetting(string name)
        {
            Process<object>(RegistryHive.LocalMachine, name, null, RemoveSetting);
        }

        public void SetLocalSetting<T>(string name, T value)
        {
            Process(RegistryHive.CurrentUser, name, value, SetSetting);
        }

        public void SetRoamingSetting<T>(string name, T value)
        {
            Process(RegistryHive.LocalMachine, name, value, SetSetting);
        }

        private T GetSetting<T>(RegistryKey subKey, string name, T defaultValue)
        {
            return (T)subKey.GetValue(name, defaultValue);
        }

        private IReadOnlyCollection<string> ListNames(RegistryHive hiveKey)
        {
            ValidateOptions();

            using (var rootKey = RegistryKey.OpenBaseKey(hiveKey, _options.Value.RegistryView))
            {
                using (var registrySubKey = rootKey.OpenSubKey(_options.Value.SubKey))
                {
                    return registrySubKey.GetValueNames();
                }
            }
        }

        private T Process<T>(RegistryHive hiveKey, string name, T value, Func<RegistryKey, string, T, T> processor)
        {
            ValidateOptions();

            using (var rootKey = RegistryKey.OpenBaseKey(hiveKey, _options.Value.RegistryView))<commit_msg>unified name listing<commit_after>﻿using Microsoft.Extensions.Options;
using Microsoft.Win32;
using System;
using System.Collections.Generic;

namespace TIKSN.Settings
{
    public class WindowsRegistrySettingsService : ISettingsService
    {
        private readonly IOptions<WindowsRegistrySettingsServiceOptions> _options;

        public WindowsRegistrySettingsService(IOptions<WindowsRegistrySettingsServiceOptions> options)
        {
            _options = options;
            ValidateOptions();
        }

        public T GetLocalSetting<T>(string name, T defaultValue)
        {
            return Process(RegistryHive.CurrentUser, name, defaultValue, GetSetting);
        }

        public T GetRoamingSetting<T>(string name, T defaultValue)
        {
            return Process(RegistryHive.LocalMachine, name, defaultValue, GetSetting);
        }

        public IReadOnlyCollection<string> ListLocalSetting()
        {
            return Process<IReadOnlyCollection<string>>(RegistryHive.CurrentUser, null, null, ListNames);
        }

        public IReadOnlyCollection<string> ListRoamingSetting()
        {
            return Process<IReadOnlyCollection<string>>(RegistryHive.LocalMachine, null, null, ListNames);
        }

        public void RemoveLocalSetting(string name)
        {
            Process<object>(RegistryHive.CurrentUser, name, null, RemoveSetting);
        }

        public void RemoveRoamingSetting(string name)
        {
            Process<object>(RegistryHive.LocalMachine, name, null, RemoveSetting);
        }

        public void SetLocalSetting<T>(string name, T value)
        {
            Process(RegistryHive.CurrentUser, name, value, SetSetting);
        }

        public void SetRoamingSetting<T>(string name, T value)
        {
            Process(RegistryHive.LocalMachine, name, value, SetSetting);
        }

        private T GetSetting<T>(RegistryKey subKey, string name, T defaultValue)
        {
            return (T)subKey.GetValue(name, defaultValue);
        }

        private IReadOnlyCollection<string> ListNames(RegistryKey subKey, string name, IReadOnlyCollection<string> value)
        {
            return subKey.GetValueNames();
        }

        private T Process<T>(RegistryHive hiveKey, string name, T value, Func<RegistryKey, string, T, T> processor)
        {
            ValidateOptions();

            using (var rootKey = RegistryKey.OpenBaseKey(hiveKey, _options.Value.RegistryView))
{'fraction_non_alphanumeric': 0.08040993299172251, 'fraction_numerical': 0.0007883326763894363, 'mean_word_length': 2.5145429362880884, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1462198', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1260, 'n_words': 277}