#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int _GetPosInFile(const char * pFileName,const char * pDstContent,const wchar_t *wszcontent)
{
HANDLE hFile = CreateFileA(pFileName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
int asclen = lstrlenA(pDstContent);
int iSize = GetFileSize(hFile,0);
if (iSize < asclen)
{
CloseHandle(hFile);
return FALSE;
}
char * pFileBuf = new char [iSize + 1024];
DWORD dwCnt = 0;
int iRes = ReadFile(hFile,pFileBuf,iSize,&dwCnt,0);
CloseHandle(hFile);
if (iRes == 0)
{
delete [] pFileBuf;
return 0;
}
char * pFilePtr = pFileBuf;
for (int i = 0; i <= iSize - asclen; i ++,pFilePtr ++)
{
if (memcmp(pDstContent,pFilePtr, asclen) == 0)
{
int iFilePos = pFilePtr - pFileBuf;
delete [] pFileBuf;
return iFilePos;
}
}
pFilePtr = pFileBuf;
int unilen = wcslen(wszcontent);
for (int i = 0; i <= iSize - unilen * 2; i++, pFilePtr++)
{
if (memcmp((char*)wszcontent, pFilePtr, unilen * 2) == 0)
{
int iFilePos = pFilePtr - pFileBuf;
delete[] pFileBuf;
return iFilePos;
}
}
delete [] pFileBuf;
return FALSE;
}
int _FindFilesInDir(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[1024] = {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\r\n",strPath);
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[1024] = {0};
memcpy(strNextPath, PreStrPath,prepathlen);
*(strNextPath + prepathlen) = 0x5c;
memcpy(strNextPath + prepathlen + 1,stWfd.cFileName,lstrlenA(stWfd.cFileName));
ret = _FindFilesInDir(strNextPath,iLayer+1,pDstContent,wszcontent);
if (ret)
{
//
}
}
else
{
char szFileName[1024] = {0};
memcpy(szFileName,PreStrPath, prepathlen);
*(szFileName + prepathlen) = 0x5c;
memcpy(szFileName + prepathlen + 1,stWfd.cFileName ,lstrlenA(stWfd.cFileName));
int iFilePos = _GetPosInFile(szFileName,pDstContent,wszcontent);
if (iFilePos)
{
printf("found %s in file:%s,position on:%u,searching next...\r\n", pDstContent,szFileName,iFilePos);
result++;
}
}
} while(FindNextFileA(hFind,&stWfd)) ;
FindClose(hFind);
return result;
}
int _findInFile(const char * fn,const char * szcontent,wchar_t * wszcontent) {
int ret = _GetPosInFile(fn, szcontent, wszcontent);
return ret;
}
int main(int argc, char ** argv)
{
int ret = 0;
if ( argc < 2 )
{
printf("parameter wrong!\r\n");
printf("usage:F:\\android\\apktool\\pptreader\\smali Activity\r\n");
printf("usage:Activity\r\n");
getchar();
return FALSE;
}
wchar_t wszcontent[1024] = { 0 };
if (argc == 2)
{
MultiByteToWideChar(CP_ACP,0,argv[1],-1,wszcontent, 1024);
char szcurdir[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH,szcurdir);
printf("starting search string:\"%s\" in path:\"%s\",please wait...\r\n\r\n",argv[1],szcurdir);
ret = _FindFilesInDir(szcurdir,1,argv[1],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, 1024);
printf("starting search string:\"%s\" in path:\"%s\",please wait...\r\n\r\n",argv[2],path.c_str());
ret = GetFileAttributesA(path.c_str());
if (ret & FILE_ATTRIBUTE_DIRECTORY)
{
ret = _FindFilesInDir(path.c_str(), 1, argv[2], wszcontent);
}
else {
ret = _GetPosInFile(path.c_str(), argv[2], wszcontent);
}
}
if (ret == 0)
{
printf("sorry to not found string in path\r\n");
getchar();
return FALSE;
}
else
{
printf("work completely successfully found result:%u\r\n",ret);
getchar();
return TRUE;
}
return TRUE;
}
在指定路径、文件或者默认当前目录下的所有文件中,查找某个字符串
最新推荐文章于 2023-09-12 05:42:20 发布