Skip to content

Commit

Permalink
Search in Regex
Browse files Browse the repository at this point in the history
  • Loading branch information
celeron533 committed Jan 25, 2024
1 parent 6764972 commit 899516e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 26 deletions.
11 changes: 11 additions & 0 deletions DicomGrep/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
xmlns:vm="clr-namespace:DicomGrep.ViewModels"
xmlns:enums="clr-namespace:DicomGrep.Enums"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:coreEnum="clr-namespace:DicomGrepCore.Enums;assembly=DicomGrepCore"
mc:Ignorable="d"
Title="Dicom Grep" Height="640" Width="960" MinWidth="300" MinHeight="200">
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<Window.Resources>
<ObjectDataProvider x:Key="MatchPatternEnumSource"
ObjectType="{x:Type coreEnum:MatchPatternEnum}"
MethodName="GetValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="coreEnum:MatchPatternEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="16"/>
Expand Down Expand Up @@ -95,6 +105,7 @@
<StackPanel Margin="8,0,0,0">
<CheckBox Content="Case sensitive" IsChecked="{Binding CaseSensitive}"/>
<CheckBox Content="Whole word" IsChecked="{Binding WholeWord}"/>
<ComboBox ItemsSource="{Binding Source={StaticResource MatchPatternEnumSource}}" SelectedItem="{Binding MatchPattern}"/>
<CheckBox Content="Include subfolders" IsChecked="{Binding IncludeSubfolders}"/>
<StackPanel Orientation="Horizontal">
<Label Content="Treads: "/>
Expand Down
2 changes: 2 additions & 0 deletions DicomGrep/Services/ConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Text.Json;
using DicomGrepCore.Entities;
using DicomGrepCore.Enums;

namespace DicomGrep.Services
{
Expand Down Expand Up @@ -84,6 +85,7 @@ private static Configuration DefaultConfiguration()
SearchTag = "",
SearchText = "TREATMENT",
IncludeSubfolders = true,
MatchPattern = MatchPatternEnum.Normal,
SearchInResults = false,
SearchThreads = 1
};
Expand Down
18 changes: 14 additions & 4 deletions DicomGrep/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using DicomGrepCore.Enums;

namespace DicomGrep.ViewModels
{
Expand Down Expand Up @@ -104,6 +105,14 @@ public bool WholeWord
set { SetProperty(ref _wholeWord, value); }
}

private MatchPatternEnum _matchPattern;

public MatchPatternEnum MatchPattern
{
get { return _matchPattern; }
set { SetProperty(ref _matchPattern, value); }
}

private bool _includeSubfolders;
public bool IncludeSubfolders
{
Expand Down Expand Up @@ -224,7 +233,7 @@ public ICommand SopClassLookupCommand
{
get
{
return _sopClassLookupCommand ?? (_sopClassLookupCommand = new RelayCommand<object>(_=> DoSopClassLookup()));
return _sopClassLookupCommand ?? (_sopClassLookupCommand = new RelayCommand<object>(_ => DoSopClassLookup()));
}
}

Expand All @@ -233,7 +242,7 @@ public ICommand DicomDictionaryLookupCommand
{
get
{
return _dicomDictionaryLookupCommand ?? (_dicomDictionaryLookupCommand = new RelayCommand<object>(_=> DoDicomDictionaryLookup()));
return _dicomDictionaryLookupCommand ?? (_dicomDictionaryLookupCommand = new RelayCommand<object>(_ => DoDicomDictionaryLookup()));
}
}

Expand All @@ -251,7 +260,7 @@ public ICommand ExportCommand
{
get
{
return _exportCommand ?? (_exportCommand = new RelayCommand<object>(_=> DoExport()));
return _exportCommand ?? (_exportCommand = new RelayCommand<object>(_ => DoExport()));
}
}

Expand Down Expand Up @@ -282,7 +291,7 @@ public MainViewModel()
// dummy data for designer preview
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
{
for (int i=1; i<=5; i++)
for (int i = 1; i <= 5; i++)
{
ResultDicomItem resultDicomItem1 = new ResultDicomItem(new DicomTag(0x0010, 0x0020), "John Doe", new byte[0]);
ResultDicomItem resultDicomItem2 = new ResultDicomItem(new DicomTag(0x0123, 0x99, "DicomGrep Creator"), "C533", new byte[0]);
Expand Down Expand Up @@ -385,6 +394,7 @@ private void DoSearch(bool searchInResults = false)
SearchText = SearchText,
CaseSensitive = CaseSensitive,
WholeWord = WholeWord,
MatchPattern = MatchPattern,
IncludeSubfolders = IncludeSubfolders,
SearchInResults = searchInResults, // from parameter
SearchThreads = SearchThreads
Expand Down
2 changes: 2 additions & 0 deletions DicomGrepCore/Entities/SearchCriteria.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
using DicomGrepCore.Enums;

namespace DicomGrepCore.Entities
{
Expand Down Expand Up @@ -41,6 +42,7 @@ public string SearchTag
public bool CaseSensitive { get; set; }
public bool WholeWord { get; set; }
public bool IncludeSubfolders { get; set; }
public MatchPatternEnum MatchPattern { get; set; }

[JsonIgnore]
public bool SearchInResults { get; set; }
Expand Down
14 changes: 14 additions & 0 deletions DicomGrepCore/Enums/MatchPatternEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DicomGrepCore.Enums
{
public enum MatchPatternEnum
{
Normal,
Regex
}
}
56 changes: 34 additions & 22 deletions DicomGrepCore/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using DicomGrepCore.Enums;
using FellowOakDicom;

namespace DicomGrepCore.Services
Expand Down Expand Up @@ -48,7 +50,7 @@ public void Search(SearchCriteria criteria, CancellationTokenSource tokenSource)
this.token = tokenSource.Token;

searchedFileCount = 0;


if (criteria.SearchInResults)
{
Expand All @@ -67,7 +69,7 @@ public void Search(SearchCriteria criteria, CancellationTokenSource tokenSource)
matchFilenameList.Clear();
matchFileCount = 0;

ParallelOptions options = new ParallelOptions
ParallelOptions options = new ParallelOptions
{
MaxDegreeOfParallelism = criteria.SearchThreads,
CancellationToken = this.token
Expand Down Expand Up @@ -147,7 +149,7 @@ private void SearchInDicomFile(string filePath)
OnLoadDicomFile?.Invoke(this, new OnLoadDicomFileEventArgs(filePath));

DicomFile dicomFile = DicomFile.Open(filePath, FileReadOption.ReadLargeOnDemand, 16 * 1024);

IList<ResultDicomItem> resultDicomItems = null;

Check warning on line 153 in DicomGrepCore/Services/SearchService.cs

View workflow job for this annotation

GitHub Actions / build (Debug, 6.0.x)

Converting null literal or possible null value to non-nullable type.
//new DicomDatasetWalker(dicomFile.Dataset).Walk(new DatasetWalker());

Expand Down Expand Up @@ -216,7 +218,7 @@ private IList<ResultDicomItem> CompareDicomTagAndValue(DicomDataset dataset, ref
else
{
// check the tag first
if ( string.IsNullOrWhiteSpace(criteria.SearchTag) ||
if (string.IsNullOrWhiteSpace(criteria.SearchTag) ||
CompareDicomTag(dicomItem.Tag, criteria.DicomSearchTag))
{
// skip binary VRs
Expand Down Expand Up @@ -245,8 +247,8 @@ private IList<ResultDicomItem> CompareDicomTagAndValue(DicomDataset dataset, ref
valueString = Encoding.ASCII.GetString(bytes);
}
}
if ( string.IsNullOrWhiteSpace(criteria.SearchText) || CompareString(valueString, criteria, false))

if (string.IsNullOrWhiteSpace(criteria.SearchText) || CompareString(valueString, criteria, false))
{
//handle match
if (resultDicomItems == null)
Expand All @@ -257,7 +259,7 @@ private IList<ResultDicomItem> CompareDicomTagAndValue(DicomDataset dataset, ref
resultDicomItems.Add(new ResultDicomItem(element.Tag, valueString, rawValue));

//Console.WriteLine($"match value: {dicomItem.ToString()}, {valueString}");

}
}

Expand Down Expand Up @@ -286,35 +288,45 @@ private bool CompareDicomTag(DicomTag dicomTag, DicomTag criteriaDicomTag)
return criteriaDicomTag.Equals(dicomTag);
}

private bool CompareString(string refString, SearchCriteria criteria, bool isDicomTag)
private bool CompareString(string refString, SearchCriteria _criteria, bool isDicomTag)
{
if (string.IsNullOrEmpty(refString))
{
return false;
}

return CompareString(refString, criteria.SearchText, criteria.CaseSensitive, criteria.WholeWord);
return CompareString(refString, _criteria.SearchText, _criteria.CaseSensitive, _criteria.WholeWord);

}

private bool CompareString(string refString, string testString, bool caseSensitive, bool wholeWord)
{
if (wholeWord)
bool result = false;
switch (criteria.MatchPattern)
{
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);
default:
case MatchPatternEnum.Normal:
if (wholeWord)
{
result = refString.Equals(testString, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase);
}
else
{
result = refString.CaseInsensitiveContains(testString, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase);
}
break;
case MatchPatternEnum.Regex:
RegexOptions options = RegexOptions.Compiled;
if (!caseSensitive)
{
options |= RegexOptions.IgnoreCase;
}

result = Regex.IsMatch(refString, testString, options);
break;
}
return result;
}


}
}

0 comments on commit 899516e

Please sign in to comment.