private LIST_FILE_RAW_TYPE_INFO CreatRawLink()
        {
            if (_listRawTypeInfos == null)
            {
                _listRawTypeInfos = new List <LIST_FILE_RAW_TYPE_INFO>();

                try
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("select * from FileClass where 1 <> 1 ");
                    RAW_TYPES.ForEach(s => sb.AppendFormat(" or FileType = {0} ", s));
                    SqliteContext sql = new SqliteContext(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileRawTypeLib.db"));
                    DataTable     dt  = sql.FindDataTable(sb.ToString());

                    IntPtr lastPtr = IntPtr.Zero;
                    foreach (DataRow dr in dt.Rows)
                    {
                        FILE_RAW_TYPE_INFO raw = new FILE_RAW_TYPE_INFO();

                        raw.FileType        = dr["FileExName"].ToSafeString().Replace(";", "_"); //dr["FileTypeName"].ToSafeString();
                        raw.Size            = dr["FileDefaultSize"].ToSafeString().ToSafeInt();
                        raw.ExtensionName   = dr["FileExName"].ToSafeString();
                        raw.HeadMagic       = dr["FileHeadFlag"].ToSafeString();
                        raw.HeadOffsetBytes = (ushort)dr["FileHeadBeginPos"].ToSafeString().ToSafeInt();
                        raw.FootMagic       = dr["FileEndFlag"].ToSafeString();
                        raw.FootOffsetBytes = dr["FileEndPOS"].ToSafeString().ToSafeInt();
                        raw.HeadLen         = dr["FileHeadLength"].ToSafeString().ToSafeInt();
                        raw.FootLen         = 0;

                        LIST_FILE_RAW_TYPE_INFO rawlist = new LIST_FILE_RAW_TYPE_INFO();
                        rawlist.Check = 1;          //0 - 不检查, 1 - 检查
                        rawlist.Num   = 0;
                        rawlist.rt    = raw;
                        rawlist.next  = lastPtr;

                        int    size   = Marshal.SizeOf(rawlist);
                        IntPtr rawptr = Marshal.AllocHGlobal(size);
                        Marshal.StructureToPtr(rawlist, rawptr, true);

                        _listRawTypeInfos.Add(rawlist);
                        lastPtr = rawptr;
                    }
                }
                catch (Exception ex)
                {
                    LoggerManagerSingle.Instance.Error("获取Raw文件类型失败!", ex);
                }
            }

            return(_listRawTypeInfos.Count > 0 ? _listRawTypeInfos[_listRawTypeInfos.Count - 1] : new LIST_FILE_RAW_TYPE_INFO());
        }
Exemple #2
0
        /// <summary>
        /// 查询数据库,同时通过正则表达式过滤数据
        /// </summary>
        /// <param name="file">db文件</param>
        /// <param name="sql">SQL</param>
        /// <param name="colNames">过滤的列,和regex一一对应</param>
        /// <param name="regex">过滤的列对应的正则表达式,和regex一一对应</param>
        /// <returns></returns>
        public string FindByRegex(string file, string sql, string[] colNames, string[] regex)
        {
            if (colNames != null || regex != null)
            {
                return(Find(file, sql));
            }

            try
            {
                if (colNames.Length != regex.Length)
                {
                    throw new Exception("the length of columns is not equal the length of regex");
                }

                //var list = this._Find(file, sql);
                if (!System.IO.File.Exists(file))
                {
                    Console.WriteLine(string.Format("文件不存在", file));
                    return(null);
                }
                SqliteContext context = new SqliteContext(file);
                var           dt      = context.FindDataTable(sql);

                Dictionary <string, Regex> dicRegexes = new Dictionary <string, Regex>();
                for (int j = 0; j < colNames.Length; j++)
                {
                    dicRegexes[colNames[j]] = new Regex(regex[j]);
                }

                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    if (dicRegexes.Any(r => !r.Value.IsMatch(dt.Rows[i][r.Key].ToString())))        //如果某列值不满足正则匹配,则删除该行数据
                    {
                        dt.Rows.RemoveAt(i);
                    }
                }

                var list = ToDynamicCollection(dt);
                this.Encode(list, string.Empty, string.Empty);
                return(JsonConvert.SerializeObject(list));
            }
            catch (Exception ex)
            {
                var str = string.Format("SQL查询失败", file, ex.Message);
                Console.WriteLine(str);
                return(string.Empty);
            }
        }