1 /** 2 Copyright 2018 Mark Fisher 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy of 5 this software and associated documentation files (the "Software"), to deal in 6 the Software without restriction, including without limitation the rights to 7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 of the Software, and to permit persons to whom the Software is furnished to do 9 so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in all 12 copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 SOFTWARE. 21 **/ 22 module dxx.util.ini; 23 24 private import ctini.ctini; 25 26 private import std.typecons; 27 private import std.variant; 28 29 void iterateTupleTree(alias Tuples,alias Func,string fqn=null)() { 30 static foreach (fieldName ; Tuples.fieldNames) { 31 static if(isTuple!(typeof(mixin("Tuples."~fieldName)))) { 32 Func!(true,fieldName,mixin("Tuples."~fieldName),fqn); 33 static if(fqn !is null) { 34 iterateTupleTree!(mixin("Tuples."~fieldName),Func,fqn ~ "." ~ fieldName); 35 } else { 36 iterateTupleTree!(mixin("Tuples."~fieldName),Func,fieldName); 37 } 38 } else { 39 Func!(false,fieldName,mixin("Tuples."~fieldName),fqn); 40 } 41 } 42 } 43 void iterateTupleTreeF(alias Tuples,Func,string fqn=null)(Func f) { 44 static foreach (fieldName ; Tuples.fieldNames) { 45 static if(isTuple!(typeof(mixin("Tuples."~fieldName)))) { 46 //f(true,fieldName,mixin("Tuples."~fieldName),fqn); 47 static if(fqn !is null) { 48 iterateTupleTreeF!(mixin("Tuples."~fieldName),Func,fqn ~ "." ~ fieldName)(f); 49 } else { 50 iterateTupleTreeF!(mixin("Tuples."~fieldName),Func,fieldName)(f); 51 } 52 } else { 53 //f(false,fieldName,mixin("Tuples."~fieldName),fqn); 54 f(fieldName,mixin("Tuples."~fieldName),fqn); 55 } 56 } 57 } 58 void iterateTupleTreeV(alias Tuples,alias Func,string fqn=null)() { 59 static void __f(bool isSection,string k,alias v,string fqn)() { 60 Func!(isSection,k,typeof(Variant(v)),fqn)(Variant(v)); 61 } 62 iterateTupleTree!(Tuples,__f,fqn)(); 63 } 64 auto iterateSections(alias fields,alias Func)() { 65 static void __f(bool isSection,string k,alias v,string fqn)() { 66 static if (isSection) { 67 Func!(fqn,k,v)(); 68 } 69 } 70 iterateTupleTree!(fields,__f)(); 71 } 72 auto iterateValues(alias fields,alias Func)() { 73 static void __f(bool isSection,string k,alias v,string fqn)() { 74 static if (!isSection) { 75 Func!(fqn,k,v)(); 76 } 77 } 78 iterateTupleTree!(fields,__f)(); 79 } 80 auto iterateValuesF(alias fields,Func)(Func f) { 81 void __f(string k,string v,string fqn) { 82 f(fqn,k,v); 83 } 84 iterateTupleTreeF!(fields)(&__f); 85 } 86 87 template ctConfig(string fname) { 88 enum ctConfig = IniConfig!fname; 89 } 90 91 unittest { 92 import std.stdio; 93 import std.conv; 94 95 void writeFields(alias fields)() { 96 static void __f(bool isSection,string k,alias v,string fqn)() { 97 static if (isSection) { 98 if(fqn !is null) { 99 writeln(" ** [" ~ fqn ~ "." ~ k ~ "]"); 100 } else { 101 writeln(" -- [" ~ k ~ "]"); 102 } 103 } else { 104 writeln(typeid(v).to!string ~ " " ~ k ~ " = " ~ v.to!string~";"); 105 } 106 } 107 iterateTupleTree!(fields,__f)(); 108 } 109 void writeFieldsV(alias fields,string fqn=null)() { 110 static void __f(bool isSection,string k,V,string fqn)(V v) { 111 writeln(" v@ " ~ v.type.toString ~ " " ~ fqn ~ "." ~ k ~ " -- " ~ v.toString); 112 } 113 iterateTupleTreeV!(fields,__f,fqn)(); 114 } 115 void writeSections(alias fields)() { 116 static void __f(string fqn,string k,alias v)() { 117 writeln("[" ~ k ~ "]"); 118 } 119 iterateSections!(fields,__f)(); 120 } 121 void writeValues(alias fields)() { 122 static void __f(string fqn,string k,alias v)() { 123 writeln(k ~ " == " ~ v.to!string); 124 } 125 iterateValues!(fields,__f)(); 126 } 127 128 enum config = IniConfig!"test-config.ini"; 129 130 //Four data types 131 //Everything available at compile time 132 static assert(config.Section.intValue == 3); 133 static assert(config.Section.stringValue == "string"); 134 static assert(config.Section.floatValue == 123.45f); 135 static assert(config.Section.Subsection.boolValue == false); 136 137 writeFields!config(); 138 writeFieldsV!config(); 139 writeSections!config(); 140 writeValues!config(); 141 142 //writeln(getSectionNames!config); 143 } 144 145