网页爬虫WebCrawler(2)-Utilities

在网页爬虫的实现过程中还涉及了一些基本的功能函数,例如获取系统的当前时间函数,进程休眠和字符串替换函数等。

我们将这些多次调用的与过程无关的函数写成一个类Utilities。

Code:

/////Utilities.h
//*************************
//与操作系统相关的函数
//*************************

#ifndef Utilities_H
#define Utilities_H

#if defined(WIN32)
    #include <Windows.h>
#else
    #include <sys/utime.h>
#endif

#include <stdio.h>
#include <time.h>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

class Utilities{

public:
	static void GetSystemTime(std::string& sdateStr,std::string& stimeStr);
	static void StringSearchAndReplace(std::string& sourceStr,std::string toReplace,std::string withThisStr);
	static bool Sleep(long lTimeMsecs);
	static void find_and_replace(std::string &source,const std::string find,std::string replace);
	static std::string replaceAll(const std::string& s,const std::string& f,const std::string& r);
};

#endif


 

//////Utilities.cpp
#include "Utilities.h"

void Utilities::GetSystemTime(std::string& sdateStr,std::string& stimeStr)
{
	char dateStr[9];
	char timeStr[9];
	sdateStr=_strdate(dateStr);
	stimeStr=_strtime(timeStr);
}


void Utilities::StringSearchAndReplace(std::string& sourceStr,std::string toReplace,std::string withThisStr)
{
	std::string::size_type idx=0;
	while(true)
	{
		idx=sourceStr.find(toReplace,idx);
		if(idx==std::string::npos)
			break;
		sourceStr.replace(idx,toReplace.size(),withThisStr);
		idx+=withThisStr.length();
	}
}

bool Utilities::Sleep(long lTimeMsecs)
{
#if defined(WIN32)
	::Sleep(lTimeMsecs);
	return (true);
#else
	timeval tv;
	tv.tv_sec=lTimeMsecs/1000;
	tv.tv_usec=(lTimeMsecs%1000)*1000;
	if(::select(0,0,0,0,&tv)==-1)
		return (false);
	else
		return (true);
#endif
}

void Utilities::find_and_replace(std::string& source,const std::string find, std::string replace)
{
	size_t j;
	for(;(j=source.find(find))!=std::string::npos;)
		source.replace(j,find.length(),replace);
}


std::string Utilities::replaceAll(const std::string& s,const std::string& f,const std::string& r)
{
	if(s.empty()|| f.empty()|| f==r ||s.find(f)==std::string::npos)
		return s;
	std::ostringstream  build_it;
	size_t i=0;
	for(size_t pos;(pos=s.find(f,i))!=std::string::npos;)
	{
		build_it.write(&s[i],pos-i);
		build_it<<r;
		i=pos+f.size();
	}
	if(i!=s.size())
		build_it.write(&s[i],s.size()-i);
	return build_it.str();
}


 

发布了99 篇原创文章 · 获赞 8 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/CodeAsWind/article/details/40375977