首页 > 编程语言 >c++遍历搜索关键字

c++遍历搜索关键字

时间:2023-04-25 18:12:53浏览次数:30  
标签:遍历 keyword fileType c++ char 关键字 void NULL queryAnalyser

#include <iostream>
#include <windows.h>
#include <string.h>
#include <strsafe.h>

#define MAX_INPUT_LENGTH 255

using namespace std;

void printMemory(char* location, long size)
{

	printf("\n\n---------------------location:%p------------------------\n\n", location);

	for (long var = 0; var < size; var++)
	{
		printf("[%x] ", *(unsigned char*)(location++));
	}

	printf("\n\n---------------------over:%p----------------------------\n\n", location);

}

long readFile(const char* path, char** fp)
{
	FILE* f = NULL;
	errno_t err;
	if ((err = fopen_s(&f, path, "rb")) != 0 || f == 0)
	{
		printf("file open error!!\n");
		return 0;
	}

	fseek(f, 0L, SEEK_END);
	long length = ftell(f);
	fseek(f, 0L, SEEK_SET);

	*fp = (char*)malloc(length);

	if (fp == NULL)
	{
		printf("memory error!!");
		return 0;
	}

	fread(*fp, sizeof(char), length, f);

	fclose(f);
	return length;
}


template <class T>
unsigned int stringLen(const T* s)
{
	unsigned int count = 0;
	T text = *(T*)s;
	while (text != '\0')
	{
		text = *(T*)(s += 1);
		count++;
	}
	return count;
}

template <class T>
int endsWith(const T* str, const T* tail) {
	int offset = stringLen(str);
	for (int i = 0, len = stringLen(tail); i < len; i++)
		if (str[offset - len + i] != tail[i])return -1;
	return 1;
}

class queryAnalyser
{
public:
	queryAnalyser();
	~queryAnalyser();
	void setkeyword(char* keyword);
	void setFileType(char* fileType);
	void fileIterator(char* dir);
	void test();

private:
	void analyse();
	long getCodepoint(char keyword);
	void setFilePath(char* path);
	void release();

	const char* filePath = NULL;
	char* fpointer = NULL;
	long fileLength = 0, keywordLength = 0;
	char* keyword = NULL;
	char* fileType = NULL;
};

queryAnalyser::queryAnalyser() {}

void queryAnalyser::release() {
	free(this->fpointer);
	fpointer = NULL;
}

void queryAnalyser::test() {
	cout << "test";
}

void queryAnalyser::setkeyword(char* keyword) {
	this->keywordLength = strlen(keyword) - 2;
	this->keyword = new char[keywordLength+1];

	for (int i = 0; i <= keywordLength - 1; i++)
		this->keyword[i] = this->getCodepoint(keyword[i]);
	this->keyword[keywordLength] = '\0';
}

void queryAnalyser::setFileType(char* fileType) {
	int len = strlen(fileType);
	this->fileType = (char*)malloc(len);
	if (this->fileType == NULL) {
		cout << "cannot allocate new memory";
		exit(0);
	}
	//strcpy_s(this->fileType, len, (const char*) fileType);
	memcpy(this->fileType, fileType, len - 2);
	memcpy((char*)(this->fileType + len - 2), "\0\0", 2);
}
void queryAnalyser::setFilePath(char* path) {
	this->filePath = path;
	this->fileLength = readFile(path, &this->fpointer);
}
long queryAnalyser::getCodepoint(char keyword) {
	char utf8[5];
	int len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)&keyword, 1, utf8, 5, NULL, NULL);
	utf8[len] = '\0';
	unsigned char* p = (unsigned char*)utf8;
	long codepoint = 0;
	if (*p <= 0x7F) {
		codepoint = *p;
	}
	else if (*p <= 0xDF) {
		codepoint = (*p & 0x1F) << 6;
		p++;
		codepoint |= (*p & 0x3F);
	}
	else if (*p <= 0xEF) {
		codepoint = (*p & 0x0F) << 12;
		p++;
		codepoint |= (*p & 0x3F) << 6;
		p++;
		codepoint |= (*p & 0x3F);
	}
	else {
		codepoint = (*p & 0x07) << 18;
		p++;
		codepoint |= (*p & 0x3F) << 12;
		p++;
		codepoint |= (*p & 0x3F) << 6;
		p++;
		codepoint |= (*p & 0x3F);
	}
	return codepoint;
}

void queryAnalyser::analyse() {
	char* pointer = (char*)this->fpointer;
	for (long var = 0, len = this->fileLength - this->keywordLength; var < len; var++)
	{
		for (int i = 0; i < this->keywordLength; i++)
		{
			char cur = *(unsigned char*)(pointer + i);
			if (cur != this->keyword[i] )//&& cur != '\0'
				break;
			if (i == this->keywordLength - 1)
			{
				cout << "---> ";
				cout << this->filePath;
				cout << "\n";
				goto outer;
			}
		}
		pointer++;
	}
outer:
	pointer = NULL;
}

void queryAnalyser::fileIterator(char* dir) {
	char path[MAX_PATH];
	WIN32_FIND_DATAA  FindFileData;

	strcpy_s(path, MAX_PATH, dir);
	if (endsWith(path, "\\") != 1)
		strcat_s(path, MAX_PATH, "\\");
	strcat_s(path, MAX_PATH, "*");

	HANDLE hFind = FindFirstFileA(path, &FindFileData);
	do
	{
		if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (*FindFileData.cFileName == '.')continue;
			char subPath[MAX_PATH];
			strcpy_s(subPath, MAX_PATH, dir);
			if (endsWith(subPath, "\\") == -1)
				strcat_s(subPath, MAX_PATH, "\\");
			strcat_s(subPath, MAX_PATH, FindFileData.cFileName);

			fileIterator(subPath);
		}
		else
		{
			char subPath[MAX_PATH];
			strcpy_s(subPath, MAX_PATH, dir);
			if (endsWith(subPath, "\\") == -1)
				strcat_s(subPath, MAX_PATH, "\\");
			strcat_s(subPath, MAX_PATH, FindFileData.cFileName);

			if (endsWith(FindFileData.cFileName, this->fileType) == 1) {
				this->setFilePath(subPath);
				this->analyse();
			}
		}
	} while (FindNextFileA(hFind, &FindFileData) != 0);
}



queryAnalyser::~queryAnalyser()
{
	free(this->fileType);
	printf("search over");
}

char* getInputFromConsole() {
	SetConsoleOutputCP(CP_UTF8);
	SetConsoleCP(CP_UTF8);

	wchar_t wstr[MAX_INPUT_LENGTH];
	char mb_str[MAX_INPUT_LENGTH * 3 + 1];

	unsigned long read;
	void* con = GetStdHandle(STD_INPUT_HANDLE);

	ReadConsole(con, wstr, MAX_INPUT_LENGTH, &read, NULL);

	int size = WideCharToMultiByte(CP_UTF8, 0, wstr, read, mb_str, sizeof(mb_str), NULL, NULL);
	mb_str[size] = 0;

	return mb_str;
}

int main()
{
	queryAnalyser* inst = new queryAnalyser();
	while (true) {
		cout << "please enter search keyword: \n";
		char* keyword = getInputFromConsole();
		inst->setkeyword(keyword);
		cout << "please enter file affix for example: (.txt) : \n";
		char* fileType = getInputFromConsole();
		inst->setFileType(fileType);
		printMemory((char*)keyword, strlen(fileType)-2);
		inst->fileIterator((char*)".\\");
	}

	delete inst;


	return 0;
}


标签:遍历,keyword,fileType,c++,char,关键字,void,NULL,queryAnalyser
From: https://www.cnblogs.com/laremehpe/p/17353444.html

相关文章

  • 我总结的一些 C++ 高频面试题(收藏)
    extern“C”extern是C/C++语言中表明函数和全局变量作用范围的关键字,该关键字告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。被extern"C"修饰的变量和函数是按照C语言方式编译和连接的。extern“C”这个声明的真实目的:解决名字匹配问题,实现C++与C的混合......
  • c++11 std::forward使用场景以及作用
    不使用 std::forward时,下述代码G不管传入什么类型的参数,只会最终调用 voidF(int&a);usingnamespacestd; voidF(int&a){  cout<<"int&version"<<a<<endl;} voidF(int&&a){  //dosomething  cout<<"in......
  • C++语言亚马逊国际获取AMAZON商品详情 API接口
    跨境电子商务是一种全新的互联网电商模式,运用电子化方式促成线上跨境交易,利用跨境物流运送商品,有利于打破传统的贸易格局,成为新的经济增长点。对我国来说,跨境电商平台正用一种全新的力量改变我国产业链的结构,并有利于增加贸易机会,拓展我国外贸在国际市场的广度与深度,赢得广阔的海......
  • 名字修饰约定: extern "C"、extern "C++" 和__stdcall、__cdecl相关的约定、__imp_前
    关于extern_C通常,在C语言的头文件中经常可以看到类似下面这种形式的代码#ifdef__cplusplusextern"C"{#endif/****somedeclarationorso*****/#ifdef__cplusplus}#endif/*endof__cplusplus*/那么,这种写法什么用呢?实际上,这是为了让CPP能够与C......
  • 《c++徒步》方法篇
    按值传递和按地址传递参考链接:https://blog.csdn.net/scrence/article/details/79835572参考链接:https://www.jb51.net/article/250343.htm1、按值传递#include<iostream>usingnamespacestd;voidchangeNumber(intx);intmain(void){ inta=10; cout<<"a="......
  • C++ shared_ptr 虚析构函数 特殊
    classa{public:~a(){cout<<"a"<<endl;}classb:publica{public:~b(){cout<<"b"<<endl;}voidmain(){shared_ptrA;{shared_ptrB(newb());//智能指针内部,uses引用值为1A=B;//智能指针内部,uses引用值为2,子类父类的智能指针可以一起计数}//离开作用......
  • 深入理解C#泛型:new与where关键字全解析
    C#泛型中new和where是重要的关键字,它们都可以用于约束泛型类型参数的限制;它们都用于提高代码的安全性和可用性,它们的作用在很大程度上提高了代码的可读性和可维护性。在这篇文章中,我们将一起了解泛型中的new和where,以及它们之间的区别。1.new关键字在C#泛型中,new关键字被用于指......
  • 【面试宝典】C/C++ 基础
    一.语言基础 数组和指针的区别 数组指针概念是用于储存多个相同类型数据的集合。 数组名是首元素的地址特殊的变量,存放的是其它变量在内存中的地址。 指针名指向了内存的首地址赋值只能一个一个元素的赋值或拷贝同类型指针变量可以相互赋值存放方式连续......
  • C语言的5种存储类以及关键字volatile、restrict
    《CPrimerPlus》读到12章,我的C语言复习进展的挺不错。这一章介绍存储类、连接和内存管理,可以说是重中之重。C的5种存储类:自动——在一个代码块内(或在一个函数头部作为参量)声明的变量,无论有没有存储类修饰符auto,都属于自动存储类。该类具有自动存储时期、代码块的作用域和空链接(n......
  • Effective C++总结
    1.视C++为一个语言联邦c++是C、面向对象C++、泛型编程、以及stl的集合。2.尽量以const\enum\inline替换#define3.尽可能使用const4.确定对象使用前已经被初始化5.了解C++默认生成并调用哪些函数7.为多态基类声明virtual析构函数8.别让异常逃离析构函数9.绝不在构造和析构过程中调......