using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using CodeLiturgy.Augeas.Test; using static CodeLiturgy.Augeas.AugeasExtern; using static CodeLiturgy.Augeas.AugFlags; namespace CodeLiturgy.Augeas { /// /// Augeas Core containing the settings of this instance. /// We can have multiple augeas cores during the execution. /// public unsafe partial class Augeas { private readonly IntPtr _augeas; /// /// Augeas Core Destructor /// ~Augeas() { // Effectively free the pointer close_aug(_augeas); } /// /// Augeas Core constructor /// /// Augeas core instance settings public Augeas(AugSettings augSettings) { _augeas = init_aug(augSettings, NO_STDINC | NO_LOAD); } /// /// Prints a preview of the desired segment in /// /// Augeas path to filter out the configuration /// The actual configuration file path public void PrintPreview(string matchPath) { print_preview(_augeas, matchPath); } /// /// Finalize the augeas instance. /// public void Close() { close_aug(_augeas); } /// /// Finalize the augeas instance. /// public void LoadFile(string configurationFilePath) { load_file(_augeas, configurationFilePath); } /// /// Prints the Aug Tree of a segment /// /// /// public void PrintAugTree(string matchPath) { print_tree(_augeas, matchPath); } /// /// Returns the tree given the augeas configuration path. /// /// configuration path. /// Dictionary with the Tree public Dictionary GetTree(string matchPath) { var result = new Dictionary(); var raw = get_tree(_augeas, matchPath); string sb = Marshal.PtrToStringAnsi((IntPtr)raw); if (sb == null) return result; free_str(raw); var lines = sb.Split(";ENDL;"); foreach (var line in lines) { var pair = line.Split(";VAL;"); if (pair.Length == 2) { result.Add(pair[0].Remove(0, 3), pair[1]); } } return result; } public static void Benchmark(Action act, int iterations) { GC.Collect(); act.Invoke(); // run once outside of loop to avoid initialization costs Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { act.Invoke(); } sw.Stop(); Console.WriteLine((sw.ElapsedMilliseconds / iterations).ToString()); } } }