当前位置:   article > 正文

简易快速搜索文件工具 ———C++_文件内容检索 c++

文件内容检索 c++

做这个项目是由于linux下面有find指令可以查找文档非常快速,windows下面的搜索文档却非常的慢,是通过暴力搜索的,但是有个叫everything的神器可以解决这个问题,可以将文档同步到数据库,直接查找数据库进行搜索,速度快了很多,我也想做一个类似于everthing的东西。

我需要我的程序

1.支持文档普通搜索

2.支持拼音全拼搜索

3.支持拼音首字母搜索

4.支持搜索关键字高亮

这个是项目的框架,确定好框架就开始一步步实现。

代码块分为五个部分, 

  1. 公共模块 : Common.h
  2. 系统工具模块 : Sysutil.h Sysutil.cpp
  3. 数据管理模块 : DataManager.h DataManager.cpp
  4. 扫描管理模块 : ScanManager.h ScanManager.cpp
  5. 系统驱动模块 : DocFastSearchTool.cpp

第一部分包含了我们所需要的头文件

  1. #pragma once
  2. #include<iostream>
  3. #include<string>
  4. #include<vector>
  5. #include<set>
  6. using namespace std;

再配置一下数据库

  1. #pragma once
  2. //数据库配置
  3. #define DOC_DB "doc.db"
  4. //数据库表配置
  5. #define DOC_TB "doc_info"
  6. //日志配置
  7. //#define __TRACE__
  8. #define __ERROR__

文件系统监控实现:

everything在实现这个模块时,使用了扫描+监控的实现方式,这两种方式是一种互补的方式。

1. 文件系统监控是利用系统文件系统的接口可以监控某个目录下的文档变化,优点是效率高,实时性强,缺点是监控是实时的,如果在监控程序没有启动期间的,文档的变化无法获取。

2. 文件系统扫描是通过系统接口,遍历获取目录下的所有文档跟数据库中的文档进行对比,获取文档变化,优点是监控程序启动前,变化的文档也能对比出来,缺点是性能低实时性不强。我们这里呢,为了简单一点,我们使用了简单粗暴的扫描。如有需要大家可以下去扩展实现可以加上监控。

数据持久化

数据持久化我们使用了轻量级的一个数据库sqlite管理,使用sqlite需要去下载sqlite的源码或者源 码。我们的数据库的表很简单,只需要一张表

  1. create table if not exists tb_doc (id INTEGER PRIMARY KEY autoincrement,
  2. doc_path text, doc_name text, doc_name_pinyin text, doc_name_initials
  3. text);

sysutil.cpp配置(高亮,模糊匹配)

  1. #include"sysutil.h"
  2. #include"log.h"
  3. #include<io.h>
  4. #include<windows.h>
  5. //系统工具 -- 体现为函数
  6. void DirectionList(const string &path, vector<string> &sub_dir, vector<string> &sub_file)
  7. {
  8. struct _finddata_t file;
  9. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc";
  10. string _path = path;
  11. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc\\*.*";
  12. _path += "\\*.*";
  13. long handle = _findfirst(_path.c_str(), &file);
  14. if(handle == -1)
  15. {
  16. //printf("扫描目录失败.\n");
  17. ERROR_LOG("扫描目录失败.");
  18. return;
  19. }
  20. do
  21. {
  22. if(file.name[0] == '.')
  23. continue;
  24. //cout<<file.name<<endl;
  25. if(file.attrib & _A_SUBDIR)
  26. sub_dir.push_back(file.name);
  27. else
  28. sub_file.push_back(file.name);
  29. if(file.attrib & _A_SUBDIR)
  30. {
  31. //文件为目录(文件夹)
  32. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc"
  33. string tmp_path = path;
  34. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc\\"
  35. tmp_path += "\\";
  36. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc\\dir_A"
  37. tmp_path += file.name;
  38. //目录递归遍历
  39. DirectionList(tmp_path, sub_dir, sub_file);
  40. }
  41. }while(_findnext(handle,&file) == 0);
  42. _findclose(handle);
  43. }
  44. /*
  45. void DirectionList(const string &path)
  46. {
  47. struct _finddata_t file;
  48. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc";
  49. string _path = path;
  50. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc\\*.*";
  51. _path += "\\*.*";
  52. long handle = _findfirst(_path.c_str(), &file);
  53. if(handle == -1)
  54. {
  55. printf("扫描目录失败.\n");
  56. return;
  57. }
  58. do
  59. {
  60. if(file.name[0] == '.')
  61. continue;
  62. cout<<file.name<<endl;
  63. if(file.attrib & _A_SUBDIR)
  64. {
  65. //文件为目录(文件夹)
  66. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc"
  67. string tmp_path = path;
  68. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc\\"
  69. tmp_path += "\\";
  70. //"C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc\\dir_A"
  71. tmp_path += file.name;
  72. //目录递归遍历
  73. DirectionList(tmp_path);
  74. }
  75. }while(_findnext(handle,&file) == 0);
  76. _findclose(handle);
  77. }
  78. */
  79. string ChineseConvertPinYinAllSpell(const string &dest_chinese)
  80. {
  81. static const int spell_value[] = { -20319, -20317, -20304, -20295,
  82. -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036,
  83. -20032, -20026,
  84. -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775,
  85. -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728,
  86. -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484,
  87. -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263,
  88. -19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224,
  89. -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996,
  90. -18977, -18961, -18952, -18783, -18774, -18773, -18763, -18756,
  91. -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526,
  92. -18518, -18501, -18490, -18478, -18463, -18448, -18447, -18446,
  93. -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183,
  94. -18181, -18012, -17997, -17988, -17970, -17964, -17961, -17950,
  95. -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730,
  96. -17721, -17703, -17701, -17697, -17692, -17683, -17676, -17496,
  97. -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202,
  98. -17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706,
  99. -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459,
  100. -16452, -16448, -16433, -16429, -16427, -16423, -16419, -16412,
  101. -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205,
  102. -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959,
  103. -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878,
  104. -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640,
  105. -15631, -15625, -15454, -15448, -15436, -15435, -15419, -15416,
  106. -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362,
  107. -15183, -15180, -15165, -15158, -15153, -15150, -15149, -15144,
  108. -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117,
  109. -15110, -15109, -14941, -14937, -14933, -14930, -14929, -14928,
  110. -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889,
  111. -14882, -14873, -14871, -14857, -14678, -14674, -14670, -14668,
  112. -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399,
  113. -14384, -14379, -14368, -14355, -14353, -14345, -14170, -14159,
  114. -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123,
  115. -14122, -14112, -14109, -14099, -14097, -14094, -14092, -14090,
  116. -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905,
  117. -13896, -13894, -13878, -13870, -13859, -13847, -13831, -13658,
  118. -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391,
  119. -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,
  120. -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095,
  121. -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871,
  122. -12860, -12858, -12852, -12849, -12838, -12831, -12829, -12812,
  123. -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346,
  124. -12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058,
  125. -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604,
  126. -11589, -11536, -11358, -11340, -11339, -11324, -11303, -11097,
  127. -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024,
  128. -11020, -11019, -11018, -11014, -10838, -10832, -10815, -10800,
  129. -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331,
  130. -10329, -10328, -10322, -10315, -10309, -10307, -10296, -10281,
  131. -10274, -10270, -10262, -10260, -10256, -10254 };
  132. // 395个字符串,每个字符串长度不超过6
  133. static const char spell_dict[396][7] = { "a", "ai", "an", "ang",
  134. "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi",
  135. "bian", "biao",
  136. "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao",
  137. "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che", "chen",
  138. "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang",
  139. "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan", "cui",
  140. "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di",
  141. "dian", "diao", "die", "ding", "diu", "dong", "dou", "du", "duan",
  142. "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei",
  143. "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang", "gao",
  144. "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai",
  145. "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang",
  146. "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua",
  147. "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian",
  148. "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan",
  149. "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken",
  150. "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui",
  151. "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei",
  152. "leng", "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling",
  153. "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo",
  154. "ma", "mai", "man", "mang", "mao", "me", "mei", "men", "meng", "mi",
  155. "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu",
  156. "na", "nai", "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni",
  157. "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong",
  158. "nu", "nv", "nuan", "nue", "nuo", "o", "ou", "pa", "pai", "pan",
  159. "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie",
  160. "pin", "ping", "po", "pu", "qi", "qia", "qian", "qiang", "qiao",
  161. "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun",
  162. "ran", "rang", "rao", "re", "ren", "reng", "ri", "rong", "rou",
  163. "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang",
  164. "sao", "se", "sen", "seng", "sha", "shai", "shan", "shang", "shao",
  165. "she", "shen", "sheng", "shi", "shou", "shu", "shua",
  166. "shuai", "shuan", "shuang", "shui", "shun", "shuo", "si", "song",
  167. "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai",
  168. "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", "tie",
  169. "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo",
  170. "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi",
  171. "xia", "xian", "xiang", "xiao", "xie", "xin", "xing",
  172. "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang",
  173. "yao", "ye", "yi", "yin", "ying", "yo", "yong", "you",
  174. "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze",
  175. "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang",
  176. "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu",
  177. "zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo",
  178. "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo" };
  179. std::string pinyin;
  180. // 循环处理字节数组
  181. const int length = dest_chinese.length();
  182. for (int j = 0, chrasc = 0; j < length;)
  183. {
  184. // 非汉字处理
  185. if (dest_chinese.at(j) >= 0 && dest_chinese.at(j) < 128)
  186. {
  187. pinyin += dest_chinese[j];
  188. // 偏移下标
  189. j++;
  190. continue;
  191. }
  192. // 汉字处理
  193. chrasc = dest_chinese[j] * 256 + dest_chinese[j + 1] + 256;
  194. if (chrasc > 0 && chrasc < 160)
  195. {
  196. // 非汉字
  197. pinyin += dest_chinese.at(j);
  198. // 偏移下标
  199. j++;
  200. }
  201. else
  202. {
  203. // 汉字
  204. for (int i = (sizeof(spell_value) / sizeof(spell_value[0]) - 1); i >= 0; --i)
  205. {
  206. // 查找字典
  207. if (spell_value[i] <= chrasc)
  208. {
  209. pinyin += spell_dict[i];
  210. break;
  211. }
  212. }
  213. // 偏移下标 (汉字双字节)
  214. j += 2;
  215. }
  216. } // for end
  217. return pinyin;
  218. }
  219. // 汉字转拼音首字母
  220. string ChineseConvertPinYinInitials(const string &name)
  221. {
  222. // 仅生成拼音首字母内容
  223. static int secPosValue[] =
  224. {
  225. 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106,
  226. 3212,
  227. 3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684,
  228. 4925, 5249
  229. };
  230. static const char* initials[] =
  231. {
  232. "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m",
  233. "n", "o",
  234. "p", "q", "r", "s", "t", "w", "x", "y", "z"
  235. };
  236. static const char* secondSecTable =
  237. "CJWGNSPGCGNE[Y[BTYYZDXYKYGT[JNNJQMBSGZSCYJSYY[PGKBZGY[YWJKGKLJYWKPJ"
  238. "QHY[W[DZLSGMRYPYWWCCKZNKYYGTTNJJNYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYX"
  239. "JWZLTBNCXJJJJTXDTTSQZYCDXXHGCK[PHFFSS[YBGXLPPBYLL[HLXS[ZM[JHSOJNGHDZ"
  240. "QYKLGJHSGQZHXQGKEZZWYSCSCJXYEYXADZPMDSSMZJZQJYZC[J[WQJBYZPXGZNZCPWHK"
  241. "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GKPBBYMTDSSXTBNPDKLEYCJNYDDYKZDDHQHSDZSCTARLL"
  274. "TKZLGECLLKJLQJAQNBDKKGHPJTZQKSECSHALQFMMGJNLYJBBTMLYZXDCJPLDLPCQDHZY"
  275. "CBZSCZBZMSLJFLKRZJSNFRGJHXPDHYJYBZGDLQCSEZGXLBLGYXTWMABCHECMWYJYZLLJ"
  276. "JYHLG[DJLSLYGKDZPZXJ"
  277. "YYZLWCXSZFGWYYDLYHCLJSCMBJHBLYZLYCBLYDPDQYSXQZBYTDKYXJY[CNRJMPDJGKL"
  278. "CLJBCTBJDDBBLBLCZQRPPXJCJLZCSHLTOLJNMDDDLNGKAQHQHJGYKHEZNMSHRP[QQJCH"
  279. "GMFPRXHJGDYCHGHLYRZQLCYQJNZSQTKQJYMSZSWLCFQQQXYFGGYPTQWLMCRNFKKFSYYL"
  280. "QBMQAMMMYXCTPSHCPTXXZZSMPHPSHMCLMLDQFYQXSZYYDYJZZHQPDSZGLSTJBCKBXYQZ"
  281. "JSGPSXQZQZRQTBDKYXZK"
  282. "HHGFLBCSMDLDGDZDBLZYYCXNNCSYBZBFGLZZXSWMSCCMQNJQSBDQSJTXXMBLTXZCLZS"
  283. "HZCXRQJGJYLXZFJPHYMZQQYDFQJJLZZNZJCDGZYGCTXMZYSCTLKPHTXHTLBJXJLXSCDQ"
  284. "XCBBTJFQZFSLTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZGS"
  285. "ZZQLYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJST"
  286. "GSHJQLZFKCGNNNSZFDEQ"
  287. "FHBSAQTGYLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBH"
  288. "LMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ";
  289. const char* cName = name.c_str();
  290. std::string result;
  291. int H = 0, L = 0, W = 0, j = 0;
  292. size_t stringlen = ::strlen(cName);
  293. for (int i = 0; i < stringlen; ++i)
  294. {
  295. H = (unsigned char)(cName[i + 0]);
  296. L = (unsigned char)(cName[i + 1]);
  297. if (H < 0xA1 || L < 0xA1)
  298. {
  299. result += cName[i];
  300. continue;
  301. }
  302. W = (H - 160) * 100 + L - 160;
  303. if (W > 1600 && W < 5590)
  304. {
  305. bool has = false;
  306. for (j = 22; j >= 0; j--)
  307. {
  308. if (W >= secPosValue[j])
  309. {
  310. result += initials[j];
  311. i++;
  312. has = true;
  313. break;
  314. }
  315. }
  316. continue;
  317. }
  318. i++;
  319. W = (H - 160 - 56) * 94 + L - 161;
  320. if (W >= 0 && W <= 3007)
  321. result += secondSecTable[W];
  322. else
  323. {
  324. result += (unsigned char)H;
  325. result += (unsigned char)L;
  326. }
  327. }
  328. return result;
  329. }
  330. // 颜色高亮显示一段字符串
  331. void ColourPrintf(const char* str)
  332. {
  333. // 0-黑 1-蓝 2-绿 3-浅绿 4-红 5-紫 6-黄 7-白 8-灰 9-淡蓝 10-淡绿
  334. // 11-淡浅绿 12-淡红 13-淡紫 14-淡黄 15-亮白
  335. //颜色:前景色 + 背景色*0x10
  336. //例如:字是红色,背景色是白色,即 红色 + 亮白 = 4 + 15*0x10
  337. WORD color = 9 + 0 * 0x10;
  338. WORD colorOld;
  339. HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  340. CONSOLE_SCREEN_BUFFER_INFO csbi;
  341. GetConsoleScreenBufferInfo(handle, &csbi);
  342. colorOld = csbi.wAttributes;
  343. SetConsoleTextAttribute(handle, color);
  344. printf("%s", str);
  345. SetConsoleTextAttribute(handle, colorOld);
  346. }

数据管理模块:

  1. #pragma once
  2. #include"common.h"
  3. #include"./sqlite3/sqlite3.h"
  4. //封装数据库sqlite
  5. class SqliteManager
  6. {
  7. public:
  8. SqliteManager();
  9. ~SqliteManager();
  10. public:
  11. void Open(const string &database); //打开或创建数据库
  12. void Close(); //关闭数据库
  13. void ExecuteSql(const string &sql); //执行SQL
  14. void GetResultTable(const string &sql, char **&ppRet, int &row, int &col);
  15. private:
  16. sqlite3 *m_db;
  17. };
  18. class AutoGetResultTable
  19. {
  20. public:
  21. AutoGetResultTable(SqliteManager &db, const string &sql, char **&ppRet, int &row, int &col);
  22. ~AutoGetResultTable();
  23. private:
  24. SqliteManager &m_db;
  25. char **m_ppRet;
  26. };
  27. #define SQL_BUFFER_SIZE 256
  28. //封装数据管理类
  29. class DataManager
  30. {
  31. public:
  32. static DataManager& GetInstance();
  33. public:
  34. ~DataManager();
  35. public:
  36. void Search(const string &key, vector<pair<string,string>> &doc_path);
  37. public:
  38. void InitSqlite(); //初始化数据库
  39. void InsertDoc(const string &path, const string &doc);
  40. void DeleteDoc(const string &path, const string &doc);
  41. void GetDoc(const string &path, multiset<string> &docs);
  42. public:
  43. static void SplitHighLight(const string &str, const string &key,
  44. string &prefix, string &highlight, string &suffix);
  45. protected:
  46. DataManager();
  47. DataManager(const DataManager &);
  48. DataManager& operator=(const DataManager &);
  49. private:
  50. SqliteManager m_dbmgr;
  51. };
  1. #include"dataManager.h"
  2. #include"log.h"
  3. #include"config.h"
  4. #include"sysutil.h"
  5. #include<algorithm>
  6. #pragma comment(lib, "./sqlite3/sqlite3.lib")
  7. SqliteManager::SqliteManager():m_db(nullptr)
  8. {}
  9. SqliteManager::~SqliteManager()
  10. {
  11. Close(); //关闭数据库
  12. }
  13. void SqliteManager::Open(const string &database)
  14. {
  15. int rc = sqlite3_open(database.c_str(), &m_db);
  16. if (rc != SQLITE_OK)
  17. {
  18. //fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(m_db));
  19. ERROR_LOG("Can't open database: %s\n", sqlite3_errmsg(m_db));
  20. exit(1);
  21. }
  22. else
  23. {
  24. //fprintf(stderr, "Opened database successfully\n");
  25. TRACE_LOG("Opened database successfully");
  26. }
  27. }
  28. void SqliteManager::Close()
  29. {
  30. int rc = sqlite3_close(m_db);
  31. if (rc != SQLITE_OK)
  32. {
  33. //fprintf(stderr, "Can't close database: %s\n", sqlite3_errmsg(m_db));
  34. ERROR_LOG("Can't close database: %s\n", sqlite3_errmsg(m_db));
  35. exit(1);
  36. }
  37. else
  38. {
  39. //fprintf(stderr, "Close database successfully\n");
  40. TRACE_LOG( "Close database successfully");
  41. }
  42. }
  43. void SqliteManager::ExecuteSql(const string &sql)
  44. {
  45. char *zErrMsg = 0;
  46. int rc = sqlite3_exec(m_db, sql.c_str(), 0, 0, &zErrMsg);
  47. if (rc != SQLITE_OK)
  48. {
  49. //fprintf(stderr, "SQL error: %s\n", zErrMsg);
  50. ERROR_LOG("SQL error: %s\n", zErrMsg);
  51. sqlite3_free(zErrMsg);
  52. }
  53. else
  54. {
  55. //fprintf(stdout, "Operation sql successfully\n");
  56. TRACE_LOG("Operation sql successfully");
  57. }
  58. }
  59. void SqliteManager::GetResultTable(const string &sql, char **&ppRet, int &row, int &col)
  60. {
  61. char *zErrMsg = 0;
  62. int rc = sqlite3_get_table(m_db, sql.c_str(), &ppRet, &row, &col, &zErrMsg);
  63. if(rc != SQLITE_OK)
  64. {
  65. //fprintf(stderr, "SQL Error: %s\n", zErrMsg);
  66. ERROR_LOG("SQL error: %s\n", zErrMsg);
  67. sqlite3_free(zErrMsg);
  68. }
  69. else
  70. {
  71. //fprintf(stdout, "Get Result Table successfully\n");
  72. TRACE_LOG("Get Result Table successfully");
  73. }
  74. }
  75. ///
  76. AutoGetResultTable::AutoGetResultTable(SqliteManager &db, const string &sql,
  77. char **&ppRet, int &row, int &col)
  78. :m_db(db), m_ppRet(nullptr)
  79. {
  80. m_db.GetResultTable(sql, ppRet, row, col);
  81. m_ppRet = ppRet;
  82. }
  83. AutoGetResultTable::~AutoGetResultTable()
  84. {
  85. if(m_ppRet)
  86. sqlite3_free_table(m_ppRet);
  87. }
  88. ///
  89. DataManager& DataManager::GetInstance()
  90. {
  91. //懒汉模式
  92. static DataManager _inst;
  93. return _inst;
  94. }
  95. DataManager::DataManager()
  96. {
  97. m_dbmgr.Open(DOC_DB);
  98. InitSqlite(); //创建表
  99. }
  100. DataManager::~DataManager()
  101. {}
  102. void DataManager::InitSqlite()
  103. {
  104. char sql[SQL_BUFFER_SIZE] = {0};
  105. sprintf(sql, "CREATE TABLE if not exists %s(\
  106. id integer primary key autoincrement,\
  107. doc_name text,\
  108. doc_name_py text,\
  109. doc_name_initials text,\
  110. doc_path text)", DOC_TB);
  111. m_dbmgr.ExecuteSql(sql);
  112. }
  113. void DataManager::InsertDoc(const string &path, const string &doc)
  114. {
  115. //汉字转拼音
  116. string doc_py = ChineseConvertPinYinAllSpell(doc);
  117. //汉字转首字母
  118. string doc_initials = ChineseConvertPinYinInitials(doc);
  119. char sql[SQL_BUFFER_SIZE] = {0};
  120. sprintf(sql, "INSERT INTO %s values(null, '%s', '%s','%s', '%s')",
  121. DOC_TB, doc.c_str(), doc_py.c_str(), doc_initials.c_str(), path.c_str());
  122. m_dbmgr.ExecuteSql(sql);
  123. }
  124. void DataManager::DeleteDoc(const string &path, const string &doc)
  125. {
  126. char sql[SQL_BUFFER_SIZE] = {0};
  127. sprintf(sql, "DELETE FROM %s where doc_path='%s' and doc_name='%s'",
  128. DOC_TB, path.c_str(), doc.c_str());
  129. m_dbmgr.ExecuteSql(sql);
  130. }
  131. void DataManager::GetDoc(const string &path, multiset<string> &docs)
  132. {
  133. char sql[SQL_BUFFER_SIZE] = {0};
  134. sprintf(sql, "SELECT doc_name from %s where doc_path='%s'",
  135. DOC_TB, path.c_str());
  136. char **ppRet = 0;
  137. int row = 0, col = 0;
  138. //m_dbmgr.GetResultTable(sql, ppRet, row, col);
  139. AutoGetResultTable at(m_dbmgr, sql, ppRet, row, col);
  140. for(int i=1; i<=row; ++i)
  141. docs.insert(ppRet[i]);
  142. //释放表结果
  143. //sqlite3_free_table(ppRet);
  144. }
  145. void DataManager::Search(const string &key, vector<pair<string,string>> &doc_path)
  146. {
  147. //汉字转拼音
  148. string doc_py = ChineseConvertPinYinAllSpell(key);
  149. //汉字转首字母
  150. string doc_initials = ChineseConvertPinYinInitials(key);
  151. char sql[SQL_BUFFER_SIZE] = {0};
  152. sprintf(sql, "SELECT doc_name, doc_path from %s where doc_name like '%%%s%%' or\
  153. doc_name_py like '%%%s%%' or doc_name_initials like '%%%s%%'",
  154. DOC_TB, key.c_str(), doc_py.c_str(), doc_initials.c_str());
  155. char **ppRet;
  156. int row, col;
  157. //m_dbmgr.GetResultTable(sql, ppRet, row, col);
  158. AutoGetResultTable at(m_dbmgr, sql, ppRet, row, col);
  159. doc_path.clear(); //清除之前搜索的数据
  160. for(int i=1; i<=row; ++i)
  161. {
  162. doc_path.push_back(make_pair(ppRet[i*col], ppRet[i*col+1]));
  163. }
  164. //释放表结果
  165. //sqlite3_free_table(ppRet);
  166. }
  167. void DataManager::SplitHighLight(const string &str, const string &key,
  168. string &prefix, string &highlight, string &suffix)
  169. {
  170. //忽略大小的匹配
  171. string strlower = str;
  172. string keylower = key;
  173. transform(strlower.begin(), strlower.end(), strlower.begin(), tolower);
  174. transform(keylower.begin(), keylower.end(), keylower.begin(), tolower);
  175. //原始字符串能够匹配
  176. size_t pos = strlower.find(keylower);
  177. if(pos != string::npos)
  178. {
  179. prefix = str.substr(0, pos);
  180. highlight = str.substr(pos, keylower.size());
  181. suffix = str.substr(pos+keylower.size(), str.size());
  182. return;
  183. }
  184. //拼音全拼搜索分割
  185. string str_py = ChineseConvertPinYinAllSpell(strlower);
  186. pos = str_py.find(keylower);
  187. if(pos != string::npos)
  188. {
  189. int str_index = 0; //控制原始字符串的下标
  190. int py_index = 0; //控制拼音字符串的下标
  191. int highlight_index = 0; //控制高亮显示字符串的起始位置
  192. int highlight_len = 0; //控制高亮字符串的长度
  193. while(str_index < str.size())
  194. {
  195. if(py_index == pos)
  196. {
  197. //记录高亮的起始位置
  198. highlight_index = str_index;
  199. }
  200. if(py_index >= pos+keylower.size())
  201. {
  202. //关键字搜索结束
  203. highlight_len = str_index - highlight_index;
  204. break;
  205. }
  206. if(str[str_index]>=0 && str[str_index]<=127)
  207. {
  208. //原始字符串是一个字符
  209. str_index++;
  210. py_index++;
  211. }
  212. else
  213. {
  214. //原始字符串是一个汉字
  215. string word(str, str_index, 2); //截取一个汉字 //校
  216. string word_py = ChineseConvertPinYinAllSpell(word);//xiao
  217. str_index += 2;
  218. py_index += word_py.size();
  219. }
  220. }
  221. prefix = str.substr(0, highlight_index);
  222. highlight = str.substr(highlight_index, highlight_len);
  223. suffix = str.substr(highlight_index+highlight_len, str.size());
  224. return;
  225. }
  226. //首字母搜索
  227. string str_initials = ChineseConvertPinYinInitials(strlower);
  228. pos = str_initials.find(keylower);
  229. if(pos != string::npos)
  230. {
  231. int str_index = 0;
  232. int initials_index = 0;
  233. int highlight_index = 0;
  234. int highlight_len = 0;
  235. while(str_index < str.size())
  236. {
  237. if(initials_index == pos)
  238. {
  239. //记录高亮的起始位置
  240. highlight_index = str_index;
  241. }
  242. if(initials_index >= pos+keylower.size())
  243. {
  244. highlight_len = str_index - highlight_index;
  245. break;
  246. }
  247. if(str[str_index]>=0 && str[str_index]<=127)
  248. {
  249. //原始字符串是一个字符
  250. str_index++;
  251. initials_index++;
  252. }
  253. else
  254. {
  255. //原始字符串是一个汉字
  256. str_index += 2;
  257. initials_index++;
  258. }
  259. }
  260. prefix = str.substr(0, highlight_index);
  261. highlight = str.substr(highlight_index, highlight_len);
  262. suffix = str.substr(highlight_index+highlight_len, str.size());
  263. return;
  264. }
  265. //没有搜索到关键字
  266. prefix = str;
  267. highlight.clear();
  268. suffix.clear();
  269. }

扫描管理模块:

  1. #pragma once
  2. #include"common.h"
  3. #include"dataManager.h"
  4. #include<windows.h>
  5. #include<mutex>
  6. #include<condition_variable>
  7. class ScanManager
  8. {
  9. public:
  10. static ScanManager& GetInstance(const string &path);
  11. public:
  12. //同步数据
  13. void ScanDirectory(const string &path);
  14. //扫描线程
  15. void ScanThread(const string &path);
  16. //监控线程
  17. void WatchThread(const string &path);
  18. protected:
  19. ScanManager(const string &path);
  20. ScanManager(ScanManager &);
  21. ScanManager& operator=(const ScanManager&);
  22. private:
  23. //DataManager m_dbmgr;
  24. mutex m_mutex;
  25. condition_variable m_cond;
  26. };
  1. #include"ScanManager.h"
  2. #include"sysutil.h"
  3. #include"log.h"
  4. #include<thread>
  5. ScanManager& ScanManager::GetInstance(const string &path)
  6. {
  7. static ScanManager _inst(path);
  8. return _inst;
  9. }
  10. void ScanManager::ScanThread(const string &path)
  11. {
  12. //初始化扫描
  13. ScanDirectory(path);
  14. while(1)
  15. {
  16. unique_lock<mutex> lock(m_mutex);
  17. m_cond.wait(lock); //条件阻塞
  18. ScanDirectory(path);
  19. }
  20. }
  21. void ScanManager::WatchThread(const string &path)
  22. {
  23. HANDLE hd = FindFirstChangeNotification(path.c_str(), true,
  24. FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME);
  25. if(hd == INVALID_HANDLE_VALUE)
  26. {
  27. //cout<<"监控目录失败."<<endl;
  28. ERROR_LOG("监控目录失败.");
  29. return;
  30. }
  31. while(1)
  32. {
  33. WaitForSingleObject(hd, INFINITE); //永不超时等待
  34. m_cond.notify_one();
  35. FindNextChangeNotification(hd);
  36. }
  37. }
  38. ScanManager::ScanManager(const string &path)
  39. {
  40. //扫描对象
  41. thread ScanObj(&ScanManager::ScanThread, this, path);
  42. ScanObj.detach();
  43. //监控对象
  44. thread WatchObj(&ScanManager::WatchThread, this, path);
  45. WatchObj.detach();
  46. }
  47. //同步本地文件和数据库文件的数据
  48. void ScanManager::ScanDirectory(const string &path)
  49. {
  50. //1 扫描本地文件
  51. vector<string> local_dir;
  52. vector<string> local_file;
  53. DirectionList(path, local_dir, local_file);
  54. multiset<string> local_set;
  55. local_set.insert(local_file.begin(), local_file.end());
  56. local_set.insert(local_dir.begin(), local_dir.end());
  57. //2 扫描数据库文件
  58. multiset<string> db_set;
  59. DataManager &m_dbmgr = DataManager::GetInstance(); //注意一定使用引用接收
  60. m_dbmgr.GetDoc(path, db_set);
  61. //3 同步数据
  62. auto local_it = local_set.begin();
  63. auto db_it = db_set.begin();
  64. while(local_it!=local_set.end() && db_it!=db_set.end())
  65. {
  66. if(*local_it < *db_it)
  67. {
  68. //本地有,数据库没有,数据库插入文件
  69. m_dbmgr.InsertDoc(path, *local_it);
  70. ++local_it;
  71. }
  72. else if(*local_it > *db_it)
  73. {
  74. //本地没有,数据库有,数据库删除文件
  75. m_dbmgr.DeleteDoc(path, *db_it);
  76. ++db_it;
  77. }
  78. else
  79. {
  80. //两者都有
  81. ++local_it;
  82. ++db_it;
  83. }
  84. }
  85. while(local_it != local_set.end())
  86. {
  87. //本地有,数据库没有,数据库插入文件
  88. m_dbmgr.InsertDoc(path, *local_it);
  89. ++local_it;
  90. }
  91. while(db_it != db_set.end())
  92. {
  93. //本地没有,数据库有,数据库删除文件
  94. m_dbmgr.DeleteDoc(path, *db_it);
  95. ++db_it;
  96. }
  97. }

驱动模块:

  1. #include"sysutil.h"
  2. #include"dataManager.h"
  3. #include"ScanManager.h"
  4. #include"log.h"
  5. #include"sysFrame.h"
  6. const char *title = "文档快速搜索工具";
  7. int main(int argc, char *argv[])
  8. {
  9. const string path = "C:\\Bit\\Code\\bit77\\Pro_文档快速搜索工具\\TestDoc";
  10. //const string path = "C:\\Bit\\Book";
  11. //扫描目录
  12. ScanManager &sm = ScanManager::GetInstance(path);
  13. //搜索
  14. DataManager &dm = DataManager::GetInstance();
  15. vector<pair<string,string>> doc_path;
  16. string key;
  17. while(1)
  18. {
  19. //显示界面
  20. DrawFrame(title);
  21. DrawMenu();
  22. cin>>key;
  23. if(key == "exit")
  24. break;
  25. dm.Search(key, doc_path);
  26. int row = 5; //默认5行
  27. int count = 0; //显示的行数
  28. string prefix, highlight, suffix;
  29. for(const auto &e : doc_path) //e : doc_name doc_path
  30. {
  31. //高亮分割
  32. string doc_name = e.first;
  33. DataManager::SplitHighLight(doc_name, key, prefix, highlight, suffix);
  34. //设置文档名显示位置
  35. SetCurPos(2, row+count++);
  36. cout<<prefix;
  37. ColourPrintf(highlight.c_str());
  38. cout<<suffix;
  39. //设置路劲名显示位置
  40. SetCurPos(33, row+count-1);
  41. printf("%--85s\n", e.second.c_str());
  42. }
  43. SystemEnd();
  44. SystemPause();
  45. }
  46. SystemEnd();
  47. return 0;
  48. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/169200?site
推荐阅读
相关标签
  

闽ICP备14008679号