예제 #1
0
        bool searchData(string filename, SearchDataDelegate sdd)
        {
            // sdd 不能为 null
            // 但是 filename 可以随意
            if (sdd == null)
            {
                throw new ArgumentNullException();
            }

            if (fileEntries == null || fileEntries.Count == 0)
            {
                return false;
            }

            // 预先编译好正则表达式
            // 用得着用不着得后面判断
            Regex regexEngine = null;
            if (regex)
            {
                try
                {
                    string pattern = filename;
                    regexEngine = new Regex(pattern, regexOptions);
                }
                catch (System.Exception ex)
                {
                    // 出错,很可能是正则表达式有误
                    // 把异常向上传递
                    throw new Exception("正则表达式不正确:" + ex.Message);
                }
            }

            // 将文件名转为小写,方便快速匹配
            // 注意这一操作必须放在前一句——
            // 构造正则表达式引擎之后
            if (!string.IsNullOrEmpty(filename))
            {
                filename = filename.ToLower();
            }

            DateTime dt = DateTime.Now;
            bool success = false;
            int count = 0;
            for (int i = 0; i < fileEntries.Count; i++)
            {
                FileEntry fileEntry = fileEntries[i];
                // 根据用户的要求,采用不同的匹配规则进行匹配
                if (same) // 要求精确匹配(但是大小写不敏感)
                {
                    if (fileEntry.FilenameLC == filename)
                    {
                        ++count;
                        // sdd 返回 false 说明不需要再继续
                        // 向下寻找了,所以直接跳出
                        if (sdd(i, fileEntry, GroupEnum.Same) == false)
                        {
                            success = true;
                            break;
                        }
                    }
                }
                else
                {
                    // 要求正则匹配
                    if (regex)
                    {
                        Constrain.NotNull(regexEngine);
                        Match match = regexEngine.Match(fileEntry.Filename);
                        if (match.Success
                            && match.Index == 0
                            && match.Length == fileEntry.Filename.Length)
                        {
                            ++count;
                            if (sdd(i, fileEntry, GroupEnum.Regex) == false)
                            {
                                success = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        // 注意下面三种匹配不是互斥的
                        // 用户如果多选,则都会进行匹配检查
                        // 如果匹配,都会添加到最终结果中

                        // 要求开头匹配
                        if (startWith)
                        {
                            if (fileEntry.FilenameLC.StartsWith(filename))
                            {
                                ++count;
                                if (sdd(i, fileEntry, GroupEnum.StartWith) == false)
                                {
                                    success = true;
                                    break;
                                }
                            }
                        }
                        // 要求中间匹配
                        if (exist)
                        {
                            if (fileEntry.FilenameLC.IndexOf(filename) > -1)
                            {
                                ++count;
                                if (sdd(i, fileEntry, GroupEnum.Exist) == false)
                                {
                                    success = true;
                                    break;
                                }
                            }
                        }
                        // 要求结尾匹配
                        if (endWith)
                        {
                            if (fileEntry.FilenameLC.EndsWith(filename))
                            {
                                ++count;
                                if (sdd(i, fileEntry, GroupEnum.EndWith) == false)
                                {
                                    success = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            TimeSpan ts = DateTime.Now - dt;
            string tip = string.Format("搜索得{0}条记录 共耗时{1}s", count, ts.TotalSeconds);
            Trace.WriteLine(tip);
            targetForm.SearchInfo = tip;
            return success;
        }
예제 #2
0
        bool searchData(string filename, SearchDataDelegate sdd)
        {
            // sdd 不能为 null
            // 但是 filename 可以随意
            if (sdd == null)
            {
                throw new ArgumentNullException();
            }

            if (fileEntries == null || fileEntries.Count == 0)
            {
                return(false);
            }

            // 预先编译好正则表达式
            // 用得着用不着得后面判断
            Regex regexEngine = null;

            if (regex)
            {
                try
                {
                    string pattern = filename;
                    regexEngine = new Regex(pattern, regexOptions);
                }
                catch (System.Exception ex)
                {
                    // 出错,很可能是正则表达式有误
                    // 把异常向上传递
                    throw new Exception("正则表达式不正确:" + ex.Message);
                }
            }

            // 将文件名转为小写,方便快速匹配
            // 注意这一操作必须放在前一句——
            // 构造正则表达式引擎之后
            if (!string.IsNullOrEmpty(filename))
            {
                filename = filename.ToLower();
            }

            DateTime dt      = DateTime.Now;
            bool     success = false;
            int      count   = 0;

            for (int i = 0; i < fileEntries.Count; i++)
            {
                FileEntry fileEntry = fileEntries[i];
                // 根据用户的要求,采用不同的匹配规则进行匹配
                if (same) // 要求精确匹配(但是大小写不敏感)
                {
                    if (fileEntry.FilenameLC == filename)
                    {
                        ++count;
                        // sdd 返回 false 说明不需要再继续
                        // 向下寻找了,所以直接跳出
                        if (sdd(i, fileEntry, GroupEnum.Same) == false)
                        {
                            success = true;
                            break;
                        }
                    }
                }
                else
                {
                    // 要求正则匹配
                    if (regex)
                    {
                        Constrain.NotNull(regexEngine);
                        Match match = regexEngine.Match(fileEntry.Filename);
                        if (match.Success &&
                            match.Index == 0 &&
                            match.Length == fileEntry.Filename.Length)
                        {
                            ++count;
                            if (sdd(i, fileEntry, GroupEnum.Regex) == false)
                            {
                                success = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        // 注意下面三种匹配不是互斥的
                        // 用户如果多选,则都会进行匹配检查
                        // 如果匹配,都会添加到最终结果中

                        // 要求开头匹配
                        if (startWith)
                        {
                            if (fileEntry.FilenameLC.StartsWith(filename))
                            {
                                ++count;
                                if (sdd(i, fileEntry, GroupEnum.StartWith) == false)
                                {
                                    success = true;
                                    break;
                                }
                            }
                        }
                        // 要求中间匹配
                        if (exist)
                        {
                            if (fileEntry.FilenameLC.IndexOf(filename) > -1)
                            {
                                ++count;
                                if (sdd(i, fileEntry, GroupEnum.Exist) == false)
                                {
                                    success = true;
                                    break;
                                }
                            }
                        }
                        // 要求结尾匹配
                        if (endWith)
                        {
                            if (fileEntry.FilenameLC.EndsWith(filename))
                            {
                                ++count;
                                if (sdd(i, fileEntry, GroupEnum.EndWith) == false)
                                {
                                    success = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            TimeSpan ts  = DateTime.Now - dt;
            string   tip = string.Format("搜索得{0}条记录 共耗时{1}s", count, ts.TotalSeconds);

            Trace.WriteLine(tip);
            targetForm.SearchInfo = tip;
            return(success);
        }