commit e87188e460eaf172c11806bc85417facb9e4ca29 Author: BlubbFish Date: Sun Nov 15 23:38:57 2015 +0000 Utils hinzugefügt diff --git a/CmdArgs.cs b/CmdArgs.cs new file mode 100644 index 0000000..0c52719 --- /dev/null +++ b/CmdArgs.cs @@ -0,0 +1,201 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BlubbFish.Utils +{ + public class CmdArgs + { + public enum ArgLength + { + Single, + Touple + } + #region Classes + public class VaildArguments + { + public VaildArguments(ArgLength length, bool required) + { + this.Required = required; + this.Length = length; + } + public VaildArguments(ArgLength length) + { + this.Required = false; + this.Length = length; + } + + public ArgLength Length { get; private set; } + public bool Required { get; private set; } + } + private class ArgTouple + { + public ArgTouple(string type, string data) + { + this.type = type; + this.data = data; + } + public ArgTouple(string type) + { + this.type = type; + } + public string type { get; private set; } + public string data { get; private set; } + + internal void setData(string data) + { + if (data != "") + this.data = data; + } + } + #endregion + private string[] args; + private List argList; + private Dictionary argsPosible = new Dictionary(); + private static CmdArgs instances = null; + private bool isSetArguments = false; + + private CmdArgs() + { + } + + /// + /// Gibt eine Instanz der Klasse zurück + /// + /// Klasse + public static CmdArgs getInstance() + { + if (instances == null) + { + instances = new CmdArgs(); + } + return instances; + } + + /// + /// Übernimmt die Argumente für die Klasse + /// + /// Mögliche Komandozeilenargumente + /// Tatsächliche Komandozeilenargumente + public void setArguments(Dictionary arguments, string[] args) + { + this.args = args; + if (!this.isSetArguments) + { + this.isSetArguments = true; + this.argsPosible = arguments; + this.Init(); + } + } + + private void Init() + { + this.argList = new List(); + for (int i = 0; i < this.args.Length; i++) + { + if (this.argsPosible.Keys.Contains(args[i])) + { + ArgTouple arg = new ArgTouple(args[i]); + if (argsPosible[args[i]].Length == ArgLength.Touple) + { + if (args.Length > i + 1) + { + arg.setData(args[++i]); + } + else + { + throw new ArgumentException(); + } + } + this.argList.Add(arg); + } + } + } + + /// + /// Menge der angegebenen Komandozeilen-Argumente + /// + /// Menge + public int GetArgsLength() + { + return this.argList.Count; + } + + /// + /// Gibt zurück ob ein Argument angegeben wurde + /// + /// Name des Arguments + /// true wenn angegeben + public bool HasArgumentType(string name) + { + foreach (ArgTouple t in this.argList) + { + if (t.type == name) + return true; + } + return false; + } + + /// + /// Gibt den Inhalt des angegeben Arguments zurück, nur bei zweiteiligen Argumenten möglich + /// + /// Name des Arguments + /// Inhalt des Arguments oder ArgumentNullException + public string GetArgumentData(string name) + { + foreach (ArgTouple t in this.argList) + { + if (t.type == name && t.data != null) + return t.data; + else + { + throw new ArgumentNullException(); + } + } + return null; + } + + public bool HasAllRequiredArguments() + { + foreach (KeyValuePair item in this.argsPosible) + { + if (item.Value.Required && !this.HasArgumentType(item.Key)) + { + return false; + } + } + return true; + } + + public string getUsageList(string name) + { + string ret = "Usage: "+name+" Parameter\nParameter:\n"; + string req =""; + string opt = ""; + foreach (KeyValuePair item in this.argsPosible) + { + if (item.Value.Required) + { + req += item.Key+" "+((item.Value.Length == ArgLength.Touple)?" [data]\n":"\n"); + } + } + if (req != "") + { + ret += "Benötigte Parameter:\n" + req; + } + foreach (KeyValuePair item in this.argsPosible) + { + if (!item.Value.Required) + { + opt += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? " [data]\n" : "\n"); + } + } + if (opt != "") + { + ret += "Optionale Parameter:\n" + opt; + } + return ret; + } + } +} diff --git a/FileLogger.cs b/FileLogger.cs new file mode 100644 index 0000000..6157d7d --- /dev/null +++ b/FileLogger.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +namespace BlubbFish.Utils +{ + public class FileLogger + { + private static Dictionary instances = new Dictionary(); + private StreamWriter file; + private FileLogger(string filename, bool append) + { + if (!File.Exists(filename)) + { + string folder = Path.GetDirectoryName(Path.GetFullPath(filename)); + if (!Directory.Exists(folder)) + { + Directory.CreateDirectory(folder); + } + } + this.file = new StreamWriter(filename, append, Encoding.UTF8); + this.file.AutoFlush = true; + } + public static FileLogger getInstance(string filename, bool append) + { + if (!instances.Keys.Contains(filename)) + { + instances.Add(filename, new FileLogger(filename, append)); + } + return instances[filename]; + } + + public void setArray(string[] text) + { + this.file.Write(String.Join(file.NewLine, text) + file.NewLine); + this.file.Flush(); + } + + public void setLine(string text) + { + this.file.WriteLine(text); + this.file.Flush(); + } + } +} diff --git a/InIReader.cs b/InIReader.cs new file mode 100644 index 0000000..fc87deb --- /dev/null +++ b/InIReader.cs @@ -0,0 +1,198 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace BlubbFish.Utils +{ + public class InIReader + { + private Dictionary> cont; + private FileSystemWatcher k = new FileSystemWatcher(Directory.GetCurrentDirectory(), "*.ini"); + private string filename; + + private static Dictionary instances = new Dictionary(); + + private InIReader(string filename) + { + this.filename = filename; + k.Changed += new FileSystemEventHandler(this.readAgain); + loadFile(); + } + + public static InIReader getInstance(string filename) + { + if (!instances.Keys.Contains(filename)) + { + instances.Add(filename, new InIReader(filename)); + } + return instances[filename]; + } + + private void readAgain(object sender, EventArgs e) + { + this.loadFile(); + } + + private void loadFile() + { + this.cont = new Dictionary>(); + StreamReader file = new StreamReader(this.filename); + List buf = new List(); + string fline = ""; + while (fline != null) + { + fline = file.ReadLine(); + if (fline != null && fline.Length > 0 && fline.Substring(0,1) != ";") + buf.Add(fline); + } + file.Close(); + Dictionary sub = new Dictionary(); + string cap = ""; + foreach (string line in buf) + { + Match match = Regex.Match(line, @"^\[[a-zA-Z0-9\-_ ]+\]\w*$", RegexOptions.IgnoreCase); + if (match.Success) + { + if (sub.Count != 0 && cap != "") + { + this.cont.Add(cap, sub); + } + cap = line; + sub = new Dictionary(); + } + else + { + if (line != "" && cap != "") + { + string key = line.Substring(0, line.IndexOf('=')); + string value = line.Substring(line.IndexOf('=') + 1); + sub.Add(key, value); + } + } + } + if (sub.Count != 0 && cap != "") + { + this.cont.Add(cap, sub); + } + } + + public List getSections() + { + return cont.Keys.ToList(); + } + + public String getValue(String section, String key) + { + if (!section.StartsWith("[")) + { + section = "[" + section + "]"; + } + if (cont.Keys.Contains(section)) + { + if (cont[section].Keys.Contains(key)) + { + return cont[section][key]; + } + } + return null; + } + + + public void SetValue(string section, string key, string value) + { + if (!section.StartsWith("[")) + { + section = "[" + section + "]"; + } + if (cont.Keys.Contains(section)) + { + if (cont[section].Keys.Contains(key)) + { + cont[section][key] = value; + } + else + { + cont[section].Add(key, value); + } + } + else + { + Dictionary sub = new Dictionary(); + sub.Add(key, value); + cont.Add(section, sub); + } + this.changed(); + } + + private void changed() + { + k.Changed -= null; + saveSettings(); + loadFile(); + k.Changed += new FileSystemEventHandler(this.readAgain); + } + + private void saveSettings() + { + StreamWriter file = new StreamWriter(this.filename); + file.BaseStream.SetLength(0); + file.BaseStream.Flush(); + file.BaseStream.Seek(0, SeekOrigin.Begin); + foreach (KeyValuePair> cap in this.cont) + { + file.WriteLine(cap.Key); + foreach (KeyValuePair sub in cap.Value) + { + file.WriteLine(sub.Key + "=" + sub.Value); + } + file.WriteLine(); + } + file.Flush(); + file.Close(); + } + + /// + /// Fügt eine neue Sektion in der Ini-Datei ein. + /// + /// Sektionsname + /// true if added, false if error + public bool addSection(string name) + { + if (!name.StartsWith("[")) + { + name = "[" + name + "]"; + } + if (this.cont.Keys.Contains(name)) + { + return false; + } + this.cont.Add(name, new Dictionary()); + this.changed(); + return true; + } + + /// + /// Löscht eine Sektion inklusive Unterpunkte aus der Ini-Datei. + /// + /// Sektionsname + /// true if removed, false if error + public bool removeSection(string name) + { + if (!name.StartsWith("[")) + { + name = "[" + name + "]"; + } + if (!this.cont.Keys.Contains(name)) + { + return false; + } + this.cont.Remove(name); + this.changed(); + return false; + } + } +} diff --git a/OwnController.cs b/OwnController.cs new file mode 100644 index 0000000..9a5ac35 --- /dev/null +++ b/OwnController.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace BlubbFish.Utils +{ + public abstract class OwnController + { + /// + /// Führt den Controller aus. + /// + public void execute() + { + this.init(); + } + abstract protected void init(); + } +} diff --git a/OwnModel.cs b/OwnModel.cs new file mode 100644 index 0000000..42c2273 --- /dev/null +++ b/OwnModel.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlubbFish.Utils +{ + public abstract class OwnModel where T : class + { + private static readonly Lazy _instance = new Lazy(() => CreateInstanceOfT()); + private List observer = new List(); + public static T Instance + { + get + { + return _instance.Value; + } + } + private static T CreateInstanceOfT() + { + return Activator.CreateInstance(typeof(T), true) as T; + } + + public void setObserver(OwnView tray) + { + this.observer.Add(tray); + tray.update(); + } + protected void update() + { + this.observer.ForEach(delegate(OwnView tray) { tray.update(); }); + } + abstract protected void init(); + } +} diff --git a/OwnObject.cs b/OwnObject.cs new file mode 100644 index 0000000..fc884b5 --- /dev/null +++ b/OwnObject.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BlubbFish.Utils { + abstract public class OwnObject { + private List> loglist = new List>(); + + public delegate void LogEvent(string location, string message, LogLevel level, DateTime date); + public enum LogLevel : int { + Debug = 1, + Notice = 2, + Info = 4, + Warn = 8, + Error = 16 + } + + public event LogEvent eventDebug; + public event LogEvent eventNotice; + public event LogEvent eventInfo; + public event LogEvent eventWarn; + public event LogEvent eventError; + public event LogEvent EventLog; + + /// + /// Get the Complete Log + /// + public List getLog(LogLevel level, bool classNames, bool timeStamps) { + List ret = new List(); + foreach(Tuple t in this.loglist) { + if(t.Item4 >= level) { + ret.Add(logToString(t.Item2, t.Item3, t.Item4, t.Item1, classNames, timeStamps)); + } + } + return ret; + } + + /// + /// Formates a LogMessage to a String + /// + public static string logToString(string location, string message, LogLevel level, DateTime date, bool classNames, bool timeStamps) { + return (timeStamps ? "[" + date.ToString("R") + "]: "+level.ToString()+" " : "") + (classNames ? location + ", " : "") + message; + } + + protected void addLog(string location, string message, LogLevel level) { + this.addLog(location, message, level, DateTime.Now); + } + + protected void addLog(string location, string message, LogLevel level, DateTime date) { + if(eventDebug != null && level >= LogLevel.Debug) { + eventDebug(location, message, level, date); + } + if(eventNotice != null && level >= LogLevel.Notice) { + eventNotice(location, message, level, date); + } + if(eventInfo != null && level >= LogLevel.Info) { + eventInfo(location, message, level, date); + } + if(eventWarn != null && level >= LogLevel.Warn) { + eventWarn(location, message, level, date); + } + if(eventError != null && level >= LogLevel.Error) { + eventError(location, message, level, date); + } + if(EventLog != null) { + EventLog(location, message, level, date); + } + this.loglist.Add(new Tuple(date, location, message, level)); + } + } +} diff --git a/OwnView.cs b/OwnView.cs new file mode 100644 index 0000000..4b39e48 --- /dev/null +++ b/OwnView.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlubbFish.Utils +{ + public abstract class OwnView + { + /// + /// Called if the Oberver (Model) updates its View + /// + abstract public void update(); + /// + /// Called if view is viewed + /// + abstract protected void init(); + /// + /// Called if Form is Disposed + /// + abstract public void Dispose(); + } +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1ed8460 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die mit einer Assembly verknüpft sind. +[assembly: AssemblyTitle("Utils")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Utils")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("6f20376a-5c71-4979-9932-13c105d1c6e6")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Utils.csproj b/Utils.csproj new file mode 100644 index 0000000..22e3a26 --- /dev/null +++ b/Utils.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098} + Library + Properties + BlubbFish.Utils + Utils + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bin/Release/Utils.dll b/bin/Release/Utils.dll new file mode 100644 index 0000000..d11ce7c Binary files /dev/null and b/bin/Release/Utils.dll differ