diff --git a/Bash/header b/Bash/header index ef50a82..3f96a4d 100644 --- a/Bash/header +++ b/Bash/header @@ -8,6 +8,12 @@ function header () { function errorheader () { tput setaf 1 1>&2; tput bold 1>&2; echo "ERROR:" $@ 1>&2; tput sgr0 1>&2; return } +function infoheader() { + tput setaf 3; tput bold; echo $@; tput sgr0; return +} +function colorstrip() { + perl -e 'use strict; use warnings; while(<>) { s/\e\[[\d;]*m//g; print; }' +} ## Configuration reading ## function param() { diff --git a/CSharp/Configure.csharp b/CSharp/Configure.csharp index d3c67e2..e5addab 100644 --- a/CSharp/Configure.csharp +++ b/CSharp/Configure.csharp @@ -88,7 +88,8 @@ namespace AniNIX.Shared { int i = 0; for (i = 0; i < lines.Count; i++) { if (lines[i].Length > 5 && lines[i][0] == '[' && lines[i][lines[i].Length-1] == ']') { - foundHeaders.Add(lines[i].Substring(2,lines[i].Length-4)); + string foundHeader = lines[i].Substring(2,lines[i].Length-4); + if (!foundHeaders.Contains(foundHeader)) foundHeaders.Add(foundHeader); } } return foundHeaders; diff --git a/CSharp/DirHandle.csharp b/CSharp/DirHandle.csharp index 64bfde3..8b5ea0f 100644 --- a/CSharp/DirHandle.csharp +++ b/CSharp/DirHandle.csharp @@ -42,6 +42,42 @@ namespace AniNIX.Shared { return new List(Directory.GetFiles(@_path)); } + /// + /// Get the path of the newest file. + /// + /// A List of strings + public String GetNewest() { + try { + return this.SortedListOfFiles()[0].FullName.Trim(); + } catch (Exception e) { + e.ToString(); + return null; + } + } + + /// + /// Get the path of the oldest file + /// + /// A List of strings + public String GetOldest() { + try { + FileInfo[] files = this.SortedListOfFiles(); + return files[files.Length-1].FullName.Trim(); + } catch (Exception e) { + e.ToString(); + return null; + } + + } + + /// + /// Sorts the list of files from newest to oldest. + /// + /// A List of strings + private FileInfo[] SortedListOfFiles() { + return (new DirectoryInfo(@_path)).GetFiles().OrderByDescending(p => p.CreationTime).ToArray(); + } + /* IDisposable */ /// diff --git a/CSharp/FileHandle.csharp b/CSharp/FileHandle.csharp index 4e457c7..7def766 100644 --- a/CSharp/FileHandle.csharp +++ b/CSharp/FileHandle.csharp @@ -24,21 +24,26 @@ namespace AniNIX.Shared { } /// - /// Read the next line. If already reached EOF, block until line is added. + /// Read the next line. /// /// 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(); - } + return _fileHandle.ReadLine(); + } + + /// + /// Get the next new line added to the file. + /// + /// Next new line + public string NextNewLine() { + FileSystemWatcher fsw = new FileSystemWatcher(_directory); + fsw.Path = _directory; + fsw.Filter = _file; + fsw.NotifyFilter = NotifyFilters.LastWrite; + fsw.WaitForChanged(WatcherChangeTypes.Changed); + // Optionally, this could be accomplished with http://stackoverflow.com/questions/452902/how-to-read-a-text-file-reversely-with-iterator-in-c-sharp + // Instead, we are using a syscall for sake of codespace and testing. + return ExecuteCommand.Run(String.Format("tail -n 1 {0}",this._path)); } /// @@ -61,7 +66,7 @@ namespace AniNIX.Shared { } /* IDisposable */ - + /// /// Clean up this FileStream, implementing IDisposable ///