In the implementation of web crawler also involved in some basic functions, such as the acquisition of the system's current time function, process hibernation and string substitution function.
We write the procedure-independent functions of these multiple invocations into a class utilities.
Code:
utilities.h//*************************//functions associated with the operating system//************************* #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); #elsetimeval tv;tv.tv_ Sec=ltimemsecs/1000;tv.tv_usec= (ltimemsecs%1000) *1000;if (:: Select (0,0,0,0,&TV) ==-1) return (false); Elsereturn (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 ();}
Web crawler WebCrawler (2)-utilities