llreplace/llreplace.cpp
// ---------------------------------------------------------------------------
// Find fromPat and Replace with toPat in filepath.
//  inFilepath = full file name path
//  outfilePath = full file name paht, can be same as inFilepath
//  backupToName = name only part
Find fromPat and Replace with toPat in filepath.
inFilepath = full file name path
outfilePath = full file name paht, can be same as inFilepath
backupToName = name only part
Find fromPat and Replace with toPat in filepath . inFilepath = full file name path outfilePath = full file name paht can be same as inFilepath backupToName = name only part
bool ReplaceFile(const lstring& inFilepath, const lstring& outFilepath, const lstring& backupToName)
{
    ifstream        in;
    ofstream        out;
    struct stat     filestat;
    std::regex_constants::match_flag_type flags = std::regex_constants::match_default;
    try {
        if (stat(inFilepath, &filestat) != 0)
            return false;
        in.open(inFilepath);
        if (in.good())
        {
            buffer.resize(filestat.st_size);
            streamsize inCnt = in.read(buffer.data(), buffer.size()).gcount();
            in.close();
            std::match_results <const char*> match;
            const char* begPtr = (const char*)buffer.data();
            const char* endPtr = begPtr + inCnt;
            pFilter->init(buffer);
            if (std::regex_search(begPtr, endPtr, match, fromPat, flags)
                && pFilter->valid(match.position(), match.length()))
            {
                if (!backupDir.empty())
                {
                    lstring backupFull;
                    rename(inFilepath, Directory_files::join(backupFull, backupDir, backupToName));
                }
                ostream* outPtr = &cout;
                if (outFilepath != "-") {
                    out.open(outFilepath);
                    if (out.is_open())
                    {
                        outPtr = &out;
                    } else {
                        cerr << strerror(errno) << ", Unable to write to " << outFilepath << endl;
                        return false;
                    }
                }
                std::regex_replace(std::ostreambuf_iterator<char>(*outPtr),
                        begPtr,
                        endPtr,
                        fromPat, toPat, flags);
                if (out.is_open()) {
                    out.close();
                }
                return true;
            }
        }
        else
        {
            cerr << strerror(errno) << ", Unable to open " << inFilepath << endl;
        }
    }
    catch (exception ex)
    {
        cerr << ex.what() << ", Error in file:" << inFilepath << endl;
    }
    return false;
}
bool ReplaceFile ( const lstring & inFilepath , const lstring & outFilepath , const lstring & backupToName ) { ifstream in ; ofstream out ; struct stat filestat ; std :: regex_constants :: match_flag_type flags = std :: regex_constants :: match_default ; try { if ( stat ( inFilepath , & filestat ) != 0 ) return false ; in . open ( inFilepath ) ; if ( in . good ( ) ) { buffer . resize ( filestat . st_size ) ; streamsize inCnt = in . read ( buffer . data ( ) , buffer . size ( ) ) . gcount ( ) ; in . close ( ) ; std :: match_results < const char * > match ; const char * begPtr = ( const char * ) buffer . data ( ) ; const char * endPtr = begPtr + inCnt ; pFilter -> init ( buffer ) ; if ( std :: regex_search ( begPtr , endPtr , match , fromPat , flags ) && pFilter -> valid ( match . position ( ) , match . length ( ) ) ) { if ( ! backupDir . empty ( ) ) { lstring backupFull ; rename ( inFilepath , Directory_files :: join ( backupFull , backupDir , backupToName ) ) ; } ostream * outPtr = & cout ; if ( outFilepath != " " ) { out . open ( outFilepath ) ; if ( out . is_open ( ) ) { outPtr = & out ; } else { cerr << strerror ( errno ) << " " << outFilepath << endl ; return false ; } } std :: regex_replace ( std :: ostreambuf_iterator < char > ( * outPtr ) , begPtr , endPtr , fromPat , toPat , flags ) ; if ( out . is_open ( ) ) { out . close ( ) ; } return true ; } } else { cerr << strerror ( errno ) << " " << inFilepath << endl ; } } catch ( exception ex ) { cerr << ex . what ( ) << " " << inFilepath << endl ; } return false ; }
Find fromPat and Replace with toPat in filepath.
Find fromPat and Replace with toPat in filepath .
// WARNING - LineFilter only validates first match and not multiple replacements. // TODO - support LineFilter validation.