Skip to content
This repository has been archived by the owner on Feb 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #134 from jjw24/everythingFixes
Browse files Browse the repository at this point in the history
Everythingfixes
  • Loading branch information
jjw24 authored Jan 26, 2020
2 parents 8bb855e + fec7912 commit 9b970ae
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 194 deletions.
249 changes: 111 additions & 138 deletions Plugins/Wox.Plugin.Everything/Everything/EverythingAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,36 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Everything.Everything.Exceptions;

namespace Wox.Plugin.Everything.Everything
{
public sealed class EverythingAPI
public interface IEverythingApi
{
#region DllImport
[DllImport(Main.DLL, CharSet = CharSet.Unicode)]
private static extern int Everything_SetSearchW(string lpSearchString);
[DllImport(Main.DLL)]
private static extern void Everything_SetMatchPath(bool bEnable);
[DllImport(Main.DLL)]
private static extern void Everything_SetMatchCase(bool bEnable);
[DllImport(Main.DLL)]
private static extern void Everything_SetMatchWholeWord(bool bEnable);
[DllImport(Main.DLL)]
private static extern void Everything_SetRegex(bool bEnable);
[DllImport(Main.DLL)]
private static extern void Everything_SetMax(int dwMax);
[DllImport(Main.DLL)]
private static extern void Everything_SetOffset(int dwOffset);

[DllImport(Main.DLL)]
private static extern bool Everything_GetMatchPath();
[DllImport(Main.DLL)]
private static extern bool Everything_GetMatchCase();
[DllImport(Main.DLL)]
private static extern bool Everything_GetMatchWholeWord();
[DllImport(Main.DLL)]
private static extern bool Everything_GetRegex();
[DllImport(Main.DLL)]
private static extern UInt32 Everything_GetMax();
[DllImport(Main.DLL)]
private static extern UInt32 Everything_GetOffset();
[DllImport(Main.DLL, CharSet = CharSet.Unicode)]
private static extern string Everything_GetSearchW();
[DllImport(Main.DLL)]
private static extern StateCode Everything_GetLastError();

[DllImport(Main.DLL, CharSet = CharSet.Unicode)]
private static extern bool Everything_QueryW(bool bWait);

[DllImport(Main.DLL)]
private static extern void Everything_SortResultsByPath();

[DllImport(Main.DLL)]
private static extern int Everything_GetNumFileResults();
[DllImport(Main.DLL)]
private static extern int Everything_GetNumFolderResults();
[DllImport(Main.DLL)]
private static extern int Everything_GetNumResults();
[DllImport(Main.DLL)]
private static extern int Everything_GetTotFileResults();
[DllImport(Main.DLL)]
private static extern int Everything_GetTotFolderResults();
[DllImport(Main.DLL)]
private static extern int Everything_GetTotResults();
[DllImport(Main.DLL)]
private static extern bool Everything_IsVolumeResult(int nIndex);
[DllImport(Main.DLL)]
private static extern bool Everything_IsFolderResult(int nIndex);
[DllImport(Main.DLL)]
private static extern bool Everything_IsFileResult(int nIndex);
[DllImport(Main.DLL, CharSet = CharSet.Unicode)]
private static extern void Everything_GetResultFullPathNameW(int nIndex, StringBuilder lpString, int nMaxCount);
[DllImport(Main.DLL)]
private static extern void Everything_Reset();
#endregion

enum StateCode
/// <summary>
/// Searches the specified key word.
/// </summary>
/// <param name="keyWord">The key word.</param>
/// <param name="token">token that allow cancellation</param>
/// <param name="offset">The offset.</param>
/// <param name="maxCount">The max count.</param>
/// <returns></returns>
List<SearchResult> Search(string keyWord, CancellationToken token, int offset = 0, int maxCount = 100);

void Load(string sdkPath);
}

public sealed class EverythingApi : IEverythingApi
{
private const int BufferSize = 4096;

private readonly object _syncObject = new object();
// cached buffer to remove redundant allocations.
private readonly StringBuilder _buffer = new StringBuilder(BufferSize);

public enum StateCode
{
OK,
MemoryError,
Expand All @@ -87,63 +47,63 @@ enum StateCode
/// Gets or sets a value indicating whether [match path].
/// </summary>
/// <value><c>true</c> if [match path]; otherwise, <c>false</c>.</value>
public Boolean MatchPath
public bool MatchPath
{
get
{
return Everything_GetMatchPath();
return EverythingApiDllImport.Everything_GetMatchPath();
}
set
{
Everything_SetMatchPath(value);
EverythingApiDllImport.Everything_SetMatchPath(value);
}
}

/// <summary>
/// Gets or sets a value indicating whether [match case].
/// </summary>
/// <value><c>true</c> if [match case]; otherwise, <c>false</c>.</value>
public Boolean MatchCase
public bool MatchCase
{
get
{
return Everything_GetMatchCase();
return EverythingApiDllImport.Everything_GetMatchCase();
}
set
{
Everything_SetMatchCase(value);
EverythingApiDllImport.Everything_SetMatchCase(value);
}
}

/// <summary>
/// Gets or sets a value indicating whether [match whole word].
/// </summary>
/// <value><c>true</c> if [match whole word]; otherwise, <c>false</c>.</value>
public Boolean MatchWholeWord
public bool MatchWholeWord
{
get
{
return Everything_GetMatchWholeWord();
return EverythingApiDllImport.Everything_GetMatchWholeWord();
}
set
{
Everything_SetMatchWholeWord(value);
EverythingApiDllImport.Everything_SetMatchWholeWord(value);
}
}

/// <summary>
/// Gets or sets a value indicating whether [enable regex].
/// </summary>
/// <value><c>true</c> if [enable regex]; otherwise, <c>false</c>.</value>
public Boolean EnableRegex
public bool EnableRegex
{
get
{
return Everything_GetRegex();
return EverythingApiDllImport.Everything_GetRegex();
}
set
{
Everything_SetRegex(value);
EverythingApiDllImport.Everything_SetRegex(value);
}
}

Expand All @@ -152,93 +112,106 @@ public Boolean EnableRegex
/// </summary>
public void Reset()
{
Everything_Reset();
}

private void no()
{
switch (Everything_GetLastError())
lock (_syncObject)
{
case StateCode.CreateThreadError:
throw new CreateThreadException();
case StateCode.CreateWindowError:
throw new CreateWindowException();
case StateCode.InvalidCallError:
throw new InvalidCallException();
case StateCode.InvalidIndexError:
throw new InvalidIndexException();
case StateCode.IPCError:
throw new IPCErrorException();
case StateCode.MemoryError:
throw new MemoryErrorException();
case StateCode.RegisterClassExError:
throw new RegisterClassExException();
EverythingApiDllImport.Everything_Reset();
}
}

/// <summary>
/// Searches the specified key word.
/// Searches the specified key word and reset the everything API afterwards
/// </summary>
/// <param name="keyWord">The key word.</param>
/// <param name="token">when cancelled the current search will stop and exit (and would not reset)</param>
/// <param name="offset">The offset.</param>
/// <param name="maxCount">The max count.</param>
/// <returns></returns>
public IEnumerable<SearchResult> Search(string keyWord, int offset = 0, int maxCount = 100)
public List<SearchResult> Search(string keyWord, CancellationToken token, int offset = 0, int maxCount = 100)
{
if (string.IsNullOrEmpty(keyWord))
throw new ArgumentNullException("keyWord");
throw new ArgumentNullException(nameof(keyWord));

if (offset < 0)
throw new ArgumentOutOfRangeException("offset");
throw new ArgumentOutOfRangeException(nameof(offset));

if (maxCount < 0)
throw new ArgumentOutOfRangeException("maxCount");
throw new ArgumentOutOfRangeException(nameof(maxCount));

if (keyWord.StartsWith("@"))
lock (_syncObject)
{
Everything_SetRegex(true);
keyWord = keyWord.Substring(1);
}
Everything_SetSearchW(keyWord);
Everything_SetOffset(offset);
Everything_SetMax(maxCount);
if (keyWord.StartsWith("@"))
{
EverythingApiDllImport.Everything_SetRegex(true);
keyWord = keyWord.Substring(1);
}

EverythingApiDllImport.Everything_SetSearchW(keyWord);
EverythingApiDllImport.Everything_SetOffset(offset);
EverythingApiDllImport.Everything_SetMax(maxCount);

if (!Everything_QueryW(true))
{
switch (Everything_GetLastError())
if (token.IsCancellationRequested)
{
return null;
}


if (!EverythingApiDllImport.Everything_QueryW(true))
{
case StateCode.CreateThreadError:
throw new CreateThreadException();
case StateCode.CreateWindowError:
throw new CreateWindowException();
case StateCode.InvalidCallError:
throw new InvalidCallException();
case StateCode.InvalidIndexError:
throw new InvalidIndexException();
case StateCode.IPCError:
throw new IPCErrorException();
case StateCode.MemoryError:
throw new MemoryErrorException();
case StateCode.RegisterClassExError:
throw new RegisterClassExException();
CheckAndThrowExceptionOnError();
return null;
}
yield break;

var results = new List<SearchResult>();
for (int idx = 0; idx < EverythingApiDllImport.Everything_GetNumResults(); ++idx)
{
if (token.IsCancellationRequested)
{
return null;
}

EverythingApiDllImport.Everything_GetResultFullPathNameW(idx, _buffer, BufferSize);

var result = new SearchResult { FullPath = _buffer.ToString() };
if (EverythingApiDllImport.Everything_IsFolderResult(idx))
result.Type = ResultType.Folder;
else if (EverythingApiDllImport.Everything_IsFileResult(idx))
result.Type = ResultType.File;

results.Add(result);
}

Reset();

return results;
}
}

const int bufferSize = 4096;
StringBuilder buffer = new StringBuilder(bufferSize);
for (int idx = 0; idx < Everything_GetNumResults(); ++idx)
{
Everything_GetResultFullPathNameW(idx, buffer, bufferSize);
[DllImport("kernel32.dll")]
private static extern int LoadLibrary(string name);

var result = new SearchResult { FullPath = buffer.ToString() };
if (Everything_IsFolderResult(idx))
result.Type = ResultType.Folder;
else if (Everything_IsFileResult(idx))
result.Type = ResultType.File;
public void Load(string sdkPath)
{
LoadLibrary(sdkPath);
}

yield return result;
private static void CheckAndThrowExceptionOnError()
{
switch (EverythingApiDllImport.Everything_GetLastError())
{
case StateCode.CreateThreadError:
throw new CreateThreadException();
case StateCode.CreateWindowError:
throw new CreateWindowException();
case StateCode.InvalidCallError:
throw new InvalidCallException();
case StateCode.InvalidIndexError:
throw new InvalidIndexException();
case StateCode.IPCError:
throw new IPCErrorException();
case StateCode.MemoryError:
throw new MemoryErrorException();
case StateCode.RegisterClassExError:
throw new RegisterClassExException();
}
}
}
Expand Down
Loading

0 comments on commit 9b970ae

Please sign in to comment.