Web crawler WebCrawler (2)-utilities

Source: Internet
Author: User
Tags string find

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.


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,&AMP;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.