Some fixes -- DirHandle isn't behaving as expected.

This commit is contained in:
DarkFeather 2017-03-30 19:14:29 -05:00
parent 60de762c8b
commit 83d4717be2
4 changed files with 62 additions and 14 deletions

View File

@ -8,6 +8,12 @@ function header () {
function errorheader () { function errorheader () {
tput setaf 1 1>&2; tput bold 1>&2; echo "ERROR:" $@ 1>&2; tput sgr0 1>&2; return 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 ## ## Configuration reading ##
function param() { function param() {

View File

@ -88,7 +88,8 @@ namespace AniNIX.Shared {
int i = 0; int i = 0;
for (i = 0; i < lines.Count; i++) { for (i = 0; i < lines.Count; i++) {
if (lines[i].Length > 5 && lines[i][0] == '[' && lines[i][lines[i].Length-1] == ']') { 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; return foundHeaders;

View File

@ -42,6 +42,42 @@ namespace AniNIX.Shared {
return new List<String>(Directory.GetFiles(@_path)); return new List<String>(Directory.GetFiles(@_path));
} }
/// <summary>
/// Get the path of the newest file.
/// </summary>
/// <returns>A List of strings</returns>
public String GetNewest() {
try {
return this.SortedListOfFiles()[0].FullName.Trim();
} catch (Exception e) {
e.ToString();
return null;
}
}
/// <summary>
/// Get the path of the oldest file
/// </summary>
/// <returns>A List of strings</returns>
public String GetOldest() {
try {
FileInfo[] files = this.SortedListOfFiles();
return files[files.Length-1].FullName.Trim();
} catch (Exception e) {
e.ToString();
return null;
}
}
/// <summary>
/// Sorts the list of files from newest to oldest.
/// </summary>
/// <returns>A List of strings</returns>
private FileInfo[] SortedListOfFiles() {
return (new DirectoryInfo(@_path)).GetFiles().OrderByDescending(p => p.CreationTime).ToArray();
}
/* IDisposable */ /* IDisposable */
/// <summary> /// <summary>

View File

@ -24,21 +24,26 @@ namespace AniNIX.Shared {
} }
/// <summary> /// <summary>
/// Read the next line. If already reached EOF, block until line is added. /// Read the next line.
/// </summary> /// </summary>
/// <returns>Next line</returns> /// <returns>Next line</returns>
public String NextLine() { public String NextLine() {
String nextLine = _fileHandle.ReadLine(); return _fileHandle.ReadLine();
if (nextLine != null) { }
return nextLine;
} else { /// <summary>
FileSystemWatcher fsw = new FileSystemWatcher(_directory); /// Get the next new line added to the file.
fsw.Path = _directory; /// </summary>
fsw.Filter = _file; /// <returns>Next new line</returns>
fsw.NotifyFilter = NotifyFilters.LastWrite; public string NextNewLine() {
fsw.WaitForChanged(WatcherChangeTypes.Changed); FileSystemWatcher fsw = new FileSystemWatcher(_directory);
return _fileHandle.ReadLine(); 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));
} }
/// <summary> /// <summary>