⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a_frep.cpp

📁 一个不错
💻 CPP
字号:
/* Copyright is licensed under GNU LGPL.                 by I.J.Wang 2005 Replace string of file(s). Source files are not modified. In stead, the  output files are the given file pathname appended with ".rtmp". Build: make a_frep*/#include "../src/wy_dirfile.h"#include "../src/wyregfile.h"#include "../src/wyfilestat.h"#include "../src/wy_uty.h"#include "../src/wy_atdestroy.h"#include <fnmatch.h>#include <unistd.h>#include <stdlib.h>static const WyCSeg OutputExt=".rtmp";// [Syn] Copy file fname to the file of which the pathname is appended with//       ".rtmp". And replace occurrence of string fndstr with repstr//static WyRet replace_string(const WyStr& fname,                     const WyStr& fndstr, const WyStr& repstr)try { WyRet r; // check for unwanted file {   WyFileStat stt;   if((r=Wy::stat(fname,stt))!=Ok) {     WY_RETURN(r);   }   if(stt.is_dir()==true) {     WY_RETURN(Wym_EISDIR);   }   if(stt.is_reg()==false) {     WY_RETURN(Wym_EINVAL);   } } WyRegFile rfile(fname,O_RDONLY); // create work file (*.rtmp) const WyStr wfname(fname+OutputExt); WyRegFile wfile; if((r=WyRegFile::create(wfile,wfname,O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR))!=Ok) {   WY_RETURN(r); } // unlink work file (wfname) in case of failure Wy_AtDestroy<WyRet,const WyStr&> rrid(Wy::unlink,wfname); bool replaced_occurred=false; WyStr buf; for(;;) {   const size_t Btr(2048);   size_t n_rd;   // read some bytes into buf   if((r=rfile.read(buf,Btr,n_rd))!=Ok) {     WY_RETURN(r);   }   if(n_rd==0) { // EOF, drain buf     wfile << buf;     break;   }   // Find occurrence of fndstr in buf and replace with repstr and   // write to wfile (some chars may left in buf (less than fndstr.size())   //   for(size_t sidx(0);;) {     //     // Con: char's offset less than sidx are drained to wfile     //     size_t blft( buf.size()-sidx );     if(blft<fndstr.size()) {       break;     }     size_t fidx, drain_size;     if(buf.find(&fidx,sidx,fndstr.cseg())==false) {       //       // drain buf which is known no occurrence of fndstr       // leaving fndstr.size()-1 char's in buf       //       drain_size=blft-fndstr.size()+1;       wfile << buf.cseg(sidx,drain_size);       buf.erase(0,sidx+drain_size);       break;     }     replaced_occurred=true;     drain_size=fidx-sidx;     wfile << buf.cseg(sidx,drain_size);     wfile << repstr;     sidx=fidx+fndstr.size();   } }  if(replaced_occurred==true) {   rrid.release(); } else { } return(Ok);}catch(const WyStr::Reply& e) { WY_RETURN(e);}catch(const WyRegFile::Reply& e) { WY_RETURN(e);}catch(const WyByteFlow::Reply& e) { WY_RETURN(e);}catch(const WyRet& e) { WY_THROW( WyRet(e) );}catch(...) { WY_TERMINATE("unknown throw type");};// Command-Line Messages//static const char *cmd_syntax=   "\n"   " Find string in file and write to file with the string replaced\n"   " The output file name is the input file name appended with '.rtmp'\n"   "\n"   " [Usage]$a_frep [-h] fnds reps file{file}\n"   " fnds     string to find\n"   " reps     string to replace with\n"   " file     pathname of regular files to find fnds\n"   " -h       print this message\n"   ;int main(int argc, char *argv[])try { if(argc<4) {   Wy::cerr << cmd_syntax;   return(-1); } // Command-line options // {  const char optstr[]="h";  int optch;    while((optch=::getopt(argc,argv,optstr))!=-1) {    switch(optch) {      case 'h':	      Wy::cout << cmd_syntax;	      return(0);      case ':':     // missing parameter      case '?':     // unknown option	      Wy::cerr << "parameter error\n";	      Wy::cerr << cmd_syntax;              return(-1);      default:	      Wy::cerr << "parameter fault\n";              return(-1);    }  }  WyRet r;  const WyStr src_str( argv[optind++] );  // source string for replace  const WyStr des_str( argv[optind++] );  // destination string for replace  for( ; optind<argc; optind++) {    const WyStr fname(argv[optind]);    // + screen out dirfile    if((r=replace_string(fname,src_str,des_str))==Ok) {      // For semantics of commonly used utility 'replace', code here:      //  1. rename fname to backup file      //  2. rename output .rtmp file to fname    } else {      Wy::cerr << fname << ": "               << Wy::wrd(r) << '\n';    }  } } return(0);}catch(const WyRet& e) { Wy::cerr << Wy::wrd(e) << '\n'; return(-1);}catch(...) { Wy::cerr << "main caught(...)\n"; return(-1);};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -