1+ using System . IO ;
2+ using Microsoft . VisualStudio . Settings ;
3+
4+ namespace GitHub . Helpers
5+ {
6+ internal class SettingsStore
7+ {
8+ readonly WritableSettingsStore store ;
9+ readonly string root ;
10+ public SettingsStore ( WritableSettingsStore store , string root )
11+ {
12+ this . store = store ;
13+ this . root = root ;
14+ }
15+
16+ public object Read ( string property , object defaultValue )
17+ {
18+ return Read ( null , property , defaultValue ) ;
19+ }
20+
21+ /// <summary>
22+ /// Read from a settings store
23+ /// </summary>
24+ /// <typeparam name="T"></typeparam>
25+ /// <param name="subpath">The subcollection path (appended to the path passed to the constructor)</param>
26+ /// <param name="property">The property name to read</param>
27+ /// <param name="defaultValue">The default value to use in case the property doesn't exist.
28+ /// The type of the default value will be used to figure out the proper way to read the property, so if pass null,
29+ /// the property will be read as a string (which may or may not be what you want)</param>
30+ /// <returns></returns>
31+ public object Read ( string subpath , string property , object defaultValue )
32+ {
33+ var collection = subpath != null ? Path . Combine ( root , subpath ) : root ;
34+ store . CreateCollection ( collection ) ;
35+
36+ if ( defaultValue is bool )
37+ return store . GetBoolean ( collection , property , ( bool ) defaultValue ) ;
38+ else if ( defaultValue is int )
39+ return store . GetInt32 ( collection , property , ( int ) defaultValue ) ;
40+ else if ( defaultValue is uint )
41+ return store . GetUInt32 ( collection , property , ( uint ) defaultValue ) ;
42+ else if ( defaultValue is long )
43+ return store . GetInt64 ( collection , property , ( long ) defaultValue ) ;
44+ else if ( defaultValue is ulong )
45+ return store . GetUInt64 ( collection , property , ( ulong ) defaultValue ) ;
46+ return store . GetString ( collection , property , defaultValue ? . ToString ( ) ?? "" ) ;
47+ }
48+
49+ public void Write ( string property , object value )
50+ {
51+ Write ( null , property , value ) ;
52+ }
53+
54+ public void Write ( string subpath , string property , object value )
55+ {
56+ var collection = subpath != null ? Path . Combine ( root , subpath ) : root ;
57+ store . CreateCollection ( collection ) ;
58+
59+ if ( value is bool )
60+ store . SetBoolean ( collection , property , ( bool ) value ) ;
61+ else if ( value is int )
62+ store . SetInt32 ( collection , property , ( int ) value ) ;
63+ else if ( value is uint )
64+ store . SetUInt32 ( collection , property , ( uint ) value ) ;
65+ else if ( value is long )
66+ store . SetInt64 ( collection , property , ( long ) value ) ;
67+ else if ( value is ulong )
68+ store . SetUInt64 ( collection , property , ( ulong ) value ) ;
69+ else
70+ store . SetString ( collection , property , value ? . ToString ( ) ?? "" ) ;
71+ }
72+ }
73+ }
0 commit comments