文本搜索小程序

一个很简单的小程序程序。使用vs开发,代码只有200行左右。用于在一个路径中搜索某一段文本,包括asc和unicode字符编码。

工作生活中,经常需要搜索某一段文本,现有工具很少具备这种本文搜索功能。比如everything虽然强大快速,但是只能搜索文件,无法搜索文件内容。为了解决这个问题,花费了一点时间写了一个小程序。

程序执行时的参数:

  1. 路径
  2. 搜索的字符串。

源码:

cpp 复制代码
#include <windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;

//melody.shop.ele.me/login
//https://melody.shop.ele.me/login


void upcase(char* str, int size,char * dststr) {
	for (int i = 0; i < size; i++) {
		if (str[i] >= 'A' && str[i] <= 'Z') {
			dststr[i] = str[i] + 0x20;
		}
		else {
			dststr[i] = str[i];
		}
	}
}


void lowcase(char* str, int size,char * dststr) {
	for (int i = 0; i < size; i++) {
		if (str[i] >= 'a' && str[i] <= 'z') {
			dststr[i] = str[i] - 0x20;
		}
		else {
			dststr[i] = str[i];
		}
	}
}


int searchStrInFile(const char * szFileName,const char * pDstContent,const wchar_t *wszcontent)
{
	HANDLE hFile = CreateFileA(szFileName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		printf("%s CreateFle:%s error:%d\r\n", __FUNCTION__, szFileName, GetLastError());
		return FALSE;
	}

	int asclen = lstrlenA(pDstContent);

	int iSize = GetFileSize(hFile,0);
	if (iSize < asclen)
	{
		CloseHandle(hFile);
		return FALSE;
	}

	char * pFileBuf = new char [iSize + 16];

	DWORD dwCnt = 0;
	int iRes = ReadFile(hFile,pFileBuf,iSize,&dwCnt,0);
	*(pFileBuf + iSize) = 0;
	CloseHandle(hFile);
	if (iRes == 0)
	{
		delete [] pFileBuf;
		return 0;
	}

	for (int i = 0; i <= iSize - asclen; i ++)
	{
		upcase(pFileBuf + i, asclen, pFileBuf + i);
		if (memcmp(pDstContent, pFileBuf + i, asclen) == 0)
		{
			 
			//delete [] pFileBuf;
			//return i;

			printf("found string:%s in file:%s,position:%u,continue searching in ther files...\r\n",
				pDstContent, szFileName, i);
		}
	}


	int unilen = wcslen(wszcontent) * sizeof(wchar_t);
	for (int i = 0; i <= iSize - unilen ; i++)
	{
		upcase(pFileBuf + i, unilen, pFileBuf + i);
		if (memcmp((char*)wszcontent, pFileBuf + i, unilen ) == 0)
		{

			//delete[] pFileBuf;
			//return i;
			printf("found string:%s in file:%s,position:%u,continue searching in ther files...\r\n",
				pDstContent, szFileName, i);
		}
	}
	
	delete [] pFileBuf;
	return FALSE;
}

int searchFile(const char * PreStrPath, int iLayer,const char * pDstContent,const wchar_t * wszcontent)   
{   
	int ret = 0;

	static int result = 0;

	int prepathlen = lstrlenA(PreStrPath);

	char strPath[0x1000] = {0};
	memcpy(strPath,PreStrPath, prepathlen);
	memcpy(strPath + prepathlen,"\\*.*",lstrlenA("\\*.*"));


	WIN32_FIND_DATAA stWfd = { 0 };
	HANDLE hFind = FindFirstFileA(strPath,&stWfd);
	if(hFind== INVALID_HANDLE_VALUE)
	{
		printf("FindFirstFileA:%s error:%d\r\n",strPath,GetLastError());
		return 0;  
	}

	char szLastDir[] = { '.','.',0 };
	char szCurDir[] = { '.',0 };
	do
	{   
		if (stWfd.dwFileAttributes ==FILE_ATTRIBUTE_DIRECTORY) 
		{  
			if (lstrcmpiA(stWfd.cFileName, szLastDir) == 0 || lstrcmpiA(stWfd.cFileName, szCurDir) == 0)
			{
				continue;
			}

			char strNextPath[0x1000] = {0};
			memcpy(strNextPath, PreStrPath,prepathlen);
			*(strNextPath + prepathlen) = 0x5c;
			memcpy(strNextPath + prepathlen + 1,stWfd.cFileName,lstrlenA(stWfd.cFileName));
			ret = searchFile(strNextPath,iLayer+1,pDstContent,wszcontent);
			if (ret)
			{
				
			}
			else {

			}
		}   
		else     
		{   
			char szFileName[0x1000] = {0};
			memcpy(szFileName,PreStrPath, prepathlen);
			*(szFileName + prepathlen) = 0x5c;
			memcpy(szFileName + prepathlen + 1,stWfd.cFileName ,lstrlenA(stWfd.cFileName));
			int iFilePos = searchStrInFile(szFileName,pDstContent,wszcontent);
			if (iFilePos)
			{

				result++;
			}
		}  
	} while(FindNextFileA(hFind,&stWfd)) ;

	FindClose(hFind);

	return result;
}



int main(int argc, char ** argv)
{
	int ret = 0;

	wchar_t wszcontent[1024] = { 0 };

	char* searchpath = 0;
	char* searchstr = 0;
	if ( argc < 2 )
	{
		//printf("parameter wrong!\r\n");
		//printf("usage 1 (search in defined directory):c:\\test\\ stringvalue\r\n");
		//printf("usage 2 (search in current directory):stringvalue\r\n");
		//getchar();
		//return FALSE;

		printf("input the path the content in which you want to search:");
		char inputpath[1024];
		char inputstr[1024];
		scanf("%s", inputpath);

		printf("input the content you want to search:");
		scanf("%s", inputstr);


		MultiByteToWideChar(CP_ACP, 0, inputstr, -1, wszcontent, sizeof(wszcontent));

		ret = searchFile(inputpath, 1, inputstr, wszcontent);
	}
	else {
		if (argc == 2)
		{
			MultiByteToWideChar(CP_ACP, 0, argv[1], -1, wszcontent, sizeof(wszcontent));

			char szcurdir[MAX_PATH];
			GetCurrentDirectoryA(MAX_PATH, szcurdir);

			printf("start searching string:\"%s\" in path:\"%s\",please to wait patiently...\r\n\r\n", argv[1], szcurdir);

			char szcontent[1024];
			upcase(argv[1],lstrlenA(argv[1]), szcontent);
			ret = searchFile(szcurdir, 1, szcontent, wszcontent);

		}
		else if (argc >= 3)
		{
			string path = argv[1];
			if (path.back() == '/' || path.back() == '\\')
			{
				path = path.substr(0, path.length() - 1);
			}
			MultiByteToWideChar(CP_ACP, 0, argv[2], -1, wszcontent, sizeof(wszcontent));

			printf("start searching string:\"%s\" in path:\"%s\",please wait patiently...\r\n\r\n", argv[2], path.c_str());

			ret = GetFileAttributesA(path.c_str());

			char szcontent[1024];
			upcase(argv[2], lstrlenA(argv[1]), szcontent);

			if (ret & FILE_ATTRIBUTE_DIRECTORY)
			{
				ret = searchFile(path.c_str(), 1, szcontent, wszcontent);
			}
			else {
				ret = searchStrInFile(path.c_str(), szcontent, wszcontent);
			}
		}
	}

	if (ret == 0)
	{
		printf("Search string in path failed!\r\n");
		getchar();
		return FALSE;
	}
	else
	{
		printf("Search string successfully, found string position(offset from beginning):%u\r\n",ret);
		getchar();
		return TRUE;
	}
	return TRUE;
}
相关推荐
chaosama1 小时前
禁止uni小程序ios端上下拉伸(橡皮筋效果)
ios·小程序
瑶琴AI前端2 小时前
uniapp实现H5和微信小程序获取当前位置(腾讯地图)
微信小程序·小程序·uni-app
丁总学Java6 小时前
页面、组件、应用、生命周期(微信小程序)
微信小程序·小程序·生命周期
编程千纸鹤11 小时前
高校宿舍信息管理系统小程序
小程序·宿舍管理小程序
说私域12 小时前
基于开源 AI 智能名片 S2B2C 商城小程序的视频号交易小程序优化研究
人工智能·小程序·零售
丁总学Java1 天前
微信小程序,点击bindtap事件后,没有跳转到详情页,有可能是app.json中没有正确配置页面路径
微信小程序·小程序·json
说私域1 天前
基于开源 AI 智能名片、S2B2C 商城小程序的用户获取成本优化分析
人工智能·小程序
mosen8681 天前
Uniapp去除顶部导航栏-小程序、H5、APP适用
vue.js·微信小程序·小程序·uni-app·uniapp
qq22951165021 天前
微信小程序的汽车维修预约管理系统
微信小程序·小程序·汽车
尚梦1 天前
uni-app 封装刘海状态栏(适用小程序, h5, 头条小程序)
前端·小程序·uni-app