1 module hunt.system.Environment; 2 3 import hunt.Exceptions; 4 import hunt.logging; 5 import hunt.system.Locale; 6 import hunt.util.Configuration; 7 8 import core.stdc.locale; 9 import core.stdc.string; 10 11 import std.concurrency : initOnce; 12 import std.file; 13 import std.path; 14 import std.string; 15 16 struct Environment { 17 18 __gshared string defaultConfigFile = "hunt.config"; 19 __gshared string defaultSettingSection = ""; 20 21 private __gshared ConfigBuilder _props; 22 23 static ConfigBuilder getProperties() { 24 if (_props is null) { 25 return initOnce!(_props)({ 26 ConfigBuilder props; 27 string rootPath = dirName(thisExePath()); 28 string fileName = buildPath(rootPath, defaultConfigFile); 29 if (exists(fileName)) { 30 if(isDir(fileName)) { 31 throw new Exception("You can't load config from a directory: " ~ fileName); 32 } else { 33 props = new ConfigBuilder(fileName, defaultSettingSection); 34 } 35 } else { 36 props = new ConfigBuilder(); 37 } 38 initializeProperties(props); 39 return props; 40 }()); 41 } else { 42 return _props; 43 } 44 } 45 46 static void setProperties(ConfigBuilder props) { 47 if (props is null) { 48 throw new NullPointerException(); 49 } else { 50 this._props = props; 51 } 52 } 53 54 private static void initializeProperties(ConfigBuilder props) { 55 /* Determine the language, country, variant, and encoding from the host, 56 * and store these in the user.language, user.country, user.variant and 57 * file.encoding system properties. */ 58 setupI18nBaseProperties(props, Locale.getUserDefault()); 59 } 60 61 // dfmt off 62 private static void setupI18nBaseProperties(ConfigBuilder props, Locale locale) { 63 if (locale !is null) { 64 if (!props.hasProperty("user.language") && locale.language !is null) { 65 props.setProperty("user.language", locale.language); 66 } 67 if (!props.hasProperty("user.country") && locale.country !is null) { 68 props.setProperty("user.country", locale.country); 69 } 70 if (!props.hasProperty("user.variant") && locale.variant !is null) { 71 props.setProperty("user.variant", locale.variant); 72 } 73 if (!props.hasProperty("user.script") && locale.script !is null) { 74 props.setProperty("user.script", locale.script); 75 } 76 if (!props.hasProperty("user.encoding") && locale.encoding !is null) { 77 props.setProperty("user.encoding", locale.encoding); 78 } 79 } else { 80 if (!props.hasProperty("user.language")) 81 props.setProperty("user.language", "en"); 82 if (!props.hasProperty("user.encoding")) 83 props.setProperty("user.encoding", "ISO8859-1"); 84 } 85 } 86 87 // dfmt on 88 89 }