//transform lib function
//void StringParse(ifstream &ifs, string &in_file_name, vector<string> vec_str) {
void StringParse(std::string &in_file_name) {
	//cut words sector
	EncodingConverter trans;
	std::vector<std::string> word_vec; //store word into vector
	std::vector<std::string>::iterator iter_word_vec;

	std::string line; //读入行
	std::string content; //一次性读入全部
	std::string out_file_name = in_file_name; //生成新的文件名,就是替换替换txt为utf8
	std::ifstream ifs; //文本输入流
	std::ofstream ofs; //文本输出流

	out_file_name.erase(out_file_name.size() - 3, out_file_name.npos);
	out_file_name += "utf8";

	OpenInFile(ifs, in_file_name);
	OpenOutFile(ofs, out_file_name);

	while (getline(ifs, line)) {
		content += line + "\n";
	}
	ofs << trans.gbk_to_utf8(content) << std::endl;
	log_file << "tranform complete " << out_file_name << std::endl;

	ofs.close();
	ifs.close();
}
Beispiel #2
0
//启动UDP服务器
void UDPServer::start(ThreadPool &thread_pool) {
//	fd_set fd_read, fd_read_back;
//	struct timeval tm;
//	FD_ZERO(&fd_read);
	//监听socket上的输入
//	FD_SET(server_fd, &fd_read);
//	tm.tv_sec = 3;
//	tm.tv_usec = 0;
	int addr_len = sizeof(client_addr);
	char *recv_buf = new char[1024];
	EncodingConverter trans;
	while(1) {
//		tm.tv_sec = 3;
//		tm.tv_usec = 0;
//		fd_read_back = fd_read;
		std::cout << "recv。。。" << std::endl ;
//		select(1024, &fd_read_back, NULL, NULL, &tm);
//		if(FD_ISSET(server_fd, &fd_read_back)) {

			//接受数据
			memset(recv_buf, 0, 1024);
			memset(&client_addr, 0, sizeof(client_addr));
			cout << "================" << endl;
			if (-1 == recvfrom(server_fd, recv_buf, 1024, 0, (struct sockaddr*)&client_addr, (socklen_t*)&addr_len))
				Log::get_instance()->write("UDPServer recv data from client failed!");
			string recv = trans.gbk_to_utf8(string(recv_buf));
			cout << "server recv: " << recv << endl;
			Task task;
			task.set_task(recv_buf);
			task.set_addr(client_addr);
			//输出地址
			//cout << "UDPServer: " << task._addr.sin_addr.s_addr << endl;

			if(!thread_pool.add_task_to_pool(task)) {
				Log::get_instance()->write("failed to add task to pool");
			}
			cout << "-----------------------------" << endl;
			//cout << "queue size: " << thread_pool.get_task_queue_size() << endl;
//		}
	}
}