From 60de762c8b6c6d6dc53171a2d1c93056d8afd769 Mon Sep 17 00:00:00 2001 From: DarkFeather Date: Wed, 1 Mar 2017 08:31:47 -0600 Subject: [PATCH] Fixes from Cerberus and other development --- Bash/header | 2 +- CSharp/Configure.csharp | 1 + CSharp/DirHandle.csharp | 82 ++++++++++++++++++++++++++++ CSharp/ExecuteCommand.csharp | 5 +- CSharp/FileHandle.csharp | 103 +++++++++++++++++++++++++++++++++++ CSharp/ReportMessage.csharp | 10 +--- 6 files changed, 190 insertions(+), 13 deletions(-) create mode 100644 CSharp/DirHandle.csharp create mode 100644 CSharp/FileHandle.csharp diff --git a/Bash/header b/Bash/header index 35f8632..ef50a82 100644 --- a/Bash/header +++ b/Bash/header @@ -227,7 +227,7 @@ function getmagnetlink { searchstring="$(echo "$1" | sed "s/ /$searchunifier/g")" searchlink="$(printf "$torrentengine" "$searchstring")" searchresult="$(wget --timeout=5 -q -O - "$searchlink" | bash -c "$torrentterms")" - magnetlink="$(wget --timeout=5 -q -O - "$searchresult" | grep 'magnet:?' | cut -f $magnetposition -d \")" + magnetlink="$(wget --timeout=5 -q -O - "$searchresult" | grep 'magnet:?' | cut -f "$magnetposition" -d \"| head -n 1)" if [ $torrentdebugging == "true" ]; then echo '<'$searchstring'>' echo '<'$searchlink'>' diff --git a/CSharp/Configure.csharp b/CSharp/Configure.csharp index 0e95ef9..d3c67e2 100644 --- a/CSharp/Configure.csharp +++ b/CSharp/Configure.csharp @@ -15,6 +15,7 @@ namespace AniNIX.Shared { /// /// The Config file public Configure(String filename) { + if (!File.Exists(filename)) throw new Exception("File not found."); StreamReader fileReader = new StreamReader(filename); String line; while (true) { diff --git a/CSharp/DirHandle.csharp b/CSharp/DirHandle.csharp new file mode 100644 index 0000000..64bfde3 --- /dev/null +++ b/CSharp/DirHandle.csharp @@ -0,0 +1,82 @@ +using System; +using System.IO; +using System.Text; +using System.Linq; +using System.Collections.Generic; + +namespace AniNIX.Shared { + + public class DirHandle : IDisposable { + + // Internal representations of the directory. + private FileSystemWatcher _dirHandle = new FileSystemWatcher(); + private String _path; + + /// + /// Create a new DirHandle + /// + /// The path to watch + /// Optional filter to control files by -- accepts regex + public DirHandle(String path, String filter = "*.*") { + this._path=path; + _dirHandle.Path = path; + _dirHandle.NotifyFilter = NotifyFilters.LastWrite; + _dirHandle.Filter = filter; + _dirHandle.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.Size; + _dirHandle.EnableRaisingEvents = false; + } + + /// + /// Wait until the directory changes + /// + public void HoldForChange() { + _dirHandle.WaitForChanged(WatcherChangeTypes.Changed); + return; + } + + /// + /// Get a list of files in the path. + /// + /// A List of strings + public List ListContents() { + return new List(Directory.GetFiles(@_path)); + } + + /* IDisposable */ + + /// + /// Clean up this FileStream, implementing IDisposable + /// + public void Dispose() { + this.Dispose(true); + GC.SuppressFinalize(this); + } + /// + /// Force the GarbageCollector to Dispose if programmer does not + /// + ~DirHandle() { + Dispose(false); + ReportMessage.Log(Verbosity.Error,"Programmer forgot to dispose of DirHandle. Marking for Garbage Collector"); + } + + // This bool indicates whether we have disposed of this Raven + public bool _isDisposed = false; + + /// + /// Dispose of this FileHandle's resources responsibly. + /// + private void Dispose(bool disposing) { + if (!_isDisposed) { + if (disposing) { + _path = null; + } + if ( _dirHandle != null) { + _dirHandle.Dispose(); + } + } + this._isDisposed = true; + } + + } +} + diff --git a/CSharp/ExecuteCommand.csharp b/CSharp/ExecuteCommand.csharp index 2b474dc..4a9f7ad 100644 --- a/CSharp/ExecuteCommand.csharp +++ b/CSharp/ExecuteCommand.csharp @@ -20,9 +20,8 @@ namespace AniNIX.Shared { /// public static String Run(String command, String input) { //Sanitize inputs. - if (command.Contains("\'")) { - throw new Exception("Command strings cannot include \'."); - } + + command = command.Replace("\\'","'").Replace("'","\\'"); //Create process. Process proc = new Process(); diff --git a/CSharp/FileHandle.csharp b/CSharp/FileHandle.csharp new file mode 100644 index 0000000..4e457c7 --- /dev/null +++ b/CSharp/FileHandle.csharp @@ -0,0 +1,103 @@ +using System; +using System.IO; +using System.Text; +using System.Collections.Generic; + +namespace AniNIX.Shared { + + public class FileHandle : IDisposable { + + //Private variables + private StreamReader _fileHandle = null; + private String _path = null; + private String _directory = null; + private String _file = null; + + /// + /// Create an object to bundle my usual file operations. + /// + public FileHandle(String path) { + this._path = path; + this._directory = Path.GetDirectoryName(path); + this._file = Path.GetFileName(path); + this.Reset(); + } + + /// + /// Read the next line. If already reached EOF, block until line is added. + /// + /// Next line + public String NextLine() { + String nextLine = _fileHandle.ReadLine(); + if (nextLine != null) { + return nextLine; + } else { + FileSystemWatcher fsw = new FileSystemWatcher(_directory); + fsw.Path = _directory; + fsw.Filter = _file; + fsw.NotifyFilter = NotifyFilters.LastWrite; + fsw.WaitForChanged(WatcherChangeTypes.Changed); + return _fileHandle.ReadLine(); + } + } + + /// + /// Read all contents of the file. + /// + /// File contents + public String ReadAll() { + return _fileHandle.ReadToEnd(); + } + + /// + /// Close the old handle, if applicable, and open new one. + /// + public void Reset() { + if (this._fileHandle != null) { + this._fileHandle.Close(); + this._fileHandle.Dispose(); + } + this._fileHandle = new StreamReader(_path); + } + + /* IDisposable */ + + /// + /// Clean up this FileStream, implementing IDisposable + /// + public void Dispose() { + this.Dispose(true); + GC.SuppressFinalize(this); + } + /// + /// Force the GarbageCollector to Dispose if programmer does not + /// + ~FileHandle() { + Dispose(false); + ReportMessage.Log(Verbosity.Error,"Programmer forgot to dispose of FileHandle. Marking for Garbage Collector"); + } + + // This bool indicates whether we have disposed of this Raven + public bool _isDisposed = false; + + /// + /// Dispose of this FileHandle's resources responsibly. + /// + private void Dispose(bool disposing) { + if (!_isDisposed) { + if (disposing) { + _path = null; + _file = null; + _directory = null; + } + if ( _fileHandle != null) { + _fileHandle.Close(); + _fileHandle.Dispose(); + } + } + this._isDisposed = true; + } + + } +} + diff --git a/CSharp/ReportMessage.csharp b/CSharp/ReportMessage.csharp index a951af9..5812754 100644 --- a/CSharp/ReportMessage.csharp +++ b/CSharp/ReportMessage.csharp @@ -26,15 +26,7 @@ namespace AniNIX.Shared { return; } - if (ReportMessage.verbosity == Verbosity.Quiet) { - return; - } - - if (level == Verbosity.Always - || (ReportMessage.verbosity == Verbosity.Verbose && level == Verbosity.Verbose) - || (ReportMessage.verbosity == Verbosity.VeryVerbose && (level == Verbosity.Verbose || level == Verbosity.VeryVerbose)) - || (ReportMessage.verbosity == Verbosity.Explicit && (level == Verbosity.Verbose || level == Verbosity.VeryVerbose || level == Verbosity.Explicit)) - ) { + if ( level <= ReportMessage.verbosity) { Console.WriteLine(message); }