Example #1
0
        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();
            }
        }
Example #2
0
 /// <summary>
 /// Resets this instance.
 /// </summary>
 public void Reset()
 {
     lock (_syncObject)
     {
         EverythingApiDllImport.Everything_Reset();
     }
 }
Example #3
0
        /// <summary>
        /// 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 List <SearchResult> Search(string keyWord, CancellationToken token, int maxCount, int offset = 0)
        {
            if (string.IsNullOrEmpty(keyWord))
            {
                throw new ArgumentNullException(nameof(keyWord));
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (maxCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxCount));
            }

            lock (_syncObject)
            {
                if (keyWord.StartsWith("@"))
                {
                    EverythingApiDllImport.Everything_SetRegex(true);
                    keyWord = keyWord.Substring(1);
                }

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

                if (token.IsCancellationRequested)
                {
                    return(null);
                }


                if (!EverythingApiDllImport.Everything_QueryW(true))
                {
                    CheckAndThrowExceptionOnError();
                    return(null);
                }

                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);
            }
        }
Example #4
0
        /// <summary>
        /// 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 List <SearchResult> Search(string keyWord, CancellationToken token, int maxCount)
        {
            var results = new List <SearchResult>();

            if (string.IsNullOrEmpty(keyWord))
            {
                throw new ArgumentNullException(nameof(keyWord));
            }
            if (maxCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxCount));
            }

            if (token.IsCancellationRequested)
            {
                return(results);
            }
            if (keyWord.StartsWith("@"))
            {
                EverythingApiDllImport.Everything_SetRegex(true);
                keyWord = keyWord.Substring(1);
            }
            else
            {
                EverythingApiDllImport.Everything_SetRegex(false);
            }

            if (token.IsCancellationRequested)
            {
                return(results);
            }
            EverythingApiDllImport.Everything_SetRequestFlags(RequestFlag.HighlightedFileName | RequestFlag.HighlightedFullPathAndFileName);
            if (token.IsCancellationRequested)
            {
                return(results);
            }
            EverythingApiDllImport.Everything_SetOffset(0);
            if (token.IsCancellationRequested)
            {
                return(results);
            }
            EverythingApiDllImport.Everything_SetMax(maxCount);
            if (token.IsCancellationRequested)
            {
                return(results);
            }
            EverythingApiDllImport.Everything_SetSearchW(keyWord);

            if (token.IsCancellationRequested)
            {
                return(results);
            }
            if (!EverythingApiDllImport.Everything_QueryW(true))
            {
                CheckAndThrowExceptionOnError();
                return(results);
            }

            if (token.IsCancellationRequested)
            {
                return(results);
            }
            int count = EverythingApiDllImport.Everything_GetNumResults();

            for (int idx = 0; idx < count; ++idx)
            {
                if (token.IsCancellationRequested)
                {
                    return(results);
                }
                // https://www.voidtools.com/forum/viewtopic.php?t=8169
                string fileNameHighted = Marshal.PtrToStringUni(EverythingApiDllImport.Everything_GetResultHighlightedFileNameW(idx));
                string fullPathHighted = Marshal.PtrToStringUni(EverythingApiDllImport.Everything_GetResultHighlightedFullPathAndFileNameW(idx));
                if (fileNameHighted == null | fullPathHighted == null)
                {
                    CheckAndThrowExceptionOnError();
                }
                if (token.IsCancellationRequested)
                {
                    return(results);
                }
                ConvertHightlightFormat(fileNameHighted, out List <int> fileNameHightlightData, out string fileName);
                if (token.IsCancellationRequested)
                {
                    return(results);
                }
                ConvertHightlightFormat(fullPathHighted, out List <int> fullPathHightlightData, out string fullPath);

                var result = new SearchResult
                {
                    FileName          = fileName,
                    FileNameHightData = fileNameHightlightData,
                    FullPath          = fullPath,
                    FullPathHightData = fullPathHightlightData,
                };

                if (token.IsCancellationRequested)
                {
                    return(results);
                }
                if (EverythingApiDllImport.Everything_IsFolderResult(idx))
                {
                    result.Type = ResultType.Folder;
                }
                else
                {
                    result.Type = ResultType.File;
                }

                results.Add(result);
            }

            return(results);
        }