文本搜索小程序

一个很简单的小程序程序。使用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;
}
相关推荐
破无差3 小时前
《赛事报名系统小程序》
小程序·html·uniapp
00后程序员张4 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬6 小时前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106326 小时前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
亮子AI7 小时前
【小程序】微信小程序隐私协议
微信小程序·小程序
weixin_177297220697 小时前
短剧小程序系统开发:打造个性化娱乐新平台
小程序·娱乐·短剧
weixin_lynhgworld7 小时前
剧本杀小程序系统开发:开启沉浸式社交娱乐新纪元
小程序·娱乐
~废弃回忆 �༄9 小时前
mobx-miniprogram小程序的数据传输
小程序
说私域12 小时前
“开源AI智能名片链动2+1模式S2B2C商城小程序”在直播公屏引流中的应用与效果
人工智能·小程序·开源
!win !13 小时前
uni-app项目支付宝端Input不受控
小程序·uni-app·支付宝小程序