-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12432a0
commit 90a1648
Showing
3 changed files
with
112 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using DicomGrepCore.Extensions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DicomGrepCore.Utils | ||
{ | ||
public static class Util | ||
{ | ||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="refString"></param> | ||
/// <param name="testString"></param> | ||
/// <param name="caseSensitive"></param> | ||
/// <param name="wholeWord"></param> | ||
/// <returns></returns> | ||
public static bool CompareString(string refString, string testString, bool caseSensitive, bool wholeWord) | ||
{ | ||
if (wholeWord) | ||
{ | ||
if (caseSensitive) | ||
return refString.Equals(testString, StringComparison.InvariantCulture); | ||
else | ||
return refString.Equals(testString, StringComparison.InvariantCultureIgnoreCase); | ||
} | ||
else | ||
{ | ||
if (caseSensitive) | ||
return refString.CaseInsensitiveContains(testString, StringComparison.InvariantCulture); | ||
else | ||
return refString.CaseInsensitiveContains(testString, StringComparison.InvariantCultureIgnoreCase); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="directoryPath"></param> | ||
/// <param name="pattern"></param> | ||
/// <param name="depth"></param> | ||
/// <param name="tokenSource"></param> | ||
/// <returns></returns> | ||
public static List<string> LookupDirectory(string directoryPath, string pattern, int depth, CancellationTokenSource tokenSource) | ||
{ | ||
List<string> filenames = new List<string>(); | ||
try | ||
{ | ||
if (tokenSource.IsCancellationRequested) | ||
return filenames; | ||
else | ||
filenames.AddRange(Directory.GetFiles(directoryPath, pattern)); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.Error(e, $"Unable to list files in: '{directoryPath}'"); | ||
} | ||
|
||
if (depth > 0) | ||
{ | ||
try | ||
{ | ||
foreach (string subdirectory in Directory.GetDirectories(directoryPath)) | ||
{ | ||
if (tokenSource.IsCancellationRequested) | ||
return filenames; | ||
else | ||
filenames.AddRange(LookupDirectory(subdirectory, pattern, depth - 1, tokenSource)); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.Error(e, $"Unable to access directory: '{directoryPath}'"); | ||
} | ||
} | ||
return filenames; | ||
} | ||
} | ||
} |