Changeset 10513 in ntrip


Ignore:
Timestamp:
Jun 21, 2024, 10:31:41 AM (4 weeks ago)
Author:
wiese
Message:

ADD: replace

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/scripts/Common.pm

    r10512 r10513  
    1515use File::Basename;
    1616use File::Spec::Functions;
     17use Fcntl ':flock';
    1718use Proc::ProcessTable;
    1819
     
    228229}
    229230
     231#** @function replace ($fileName, $pattern, $replacement)
     232# @brief  Search and replace a string in a file
     233# @param  $fileName    [required] file with full path
     234# @param  $pattern     [required] Search pattern string
     235# @param  $replacement [required] Replacement string
     236# @return true on success, false on error or patter not found
     237#*
     238sub replace {
     239    my ( $fileName, $pattern, $replacement ) = @_;
     240
     241    # TODO getOptions();
     242
     243    TRACE "Search and replace string \'$pattern\' in file $fileName";
     244
     245    my $fileName_tmp = "${fileName}.tmp";
     246    my ( $OLD, $NEW );
     247    if ( !open ( $OLD, "<", $fileName ) ) {
     248        ERROR("Could not open file [$fileName]: $!");
     249        return 0;
     250    }
     251    flock ( $OLD, LOCK_SH ) || ERROR "Could not lock file '$fileName'";    # lock shared (others can read)
     252
     253    if ( !open ( $NEW, ">", $fileName_tmp ) ) {
     254        ERROR("Could not write file [$fileName_tmp]: $!");
     255        flock ( $OLD, LOCK_UN ) || ERROR "Could not unlock file '$fileName'";
     256        close ($OLD)            || ERROR "Could not close file $fileName: $!";
     257        return 0;
     258    }
     259
     260    my $numOfReplacements = 0;
     261    while (<$OLD>) {
     262        if ( $_ =~ /$pattern/ ) {
     263            $_ =~ s/$pattern/$replacement/;
     264            $numOfReplacements++;
     265        }
     266        print $NEW $_;
     267    }
     268    flock ( $OLD, LOCK_UN ) || ERROR "Could not unlock file '$fileName'";
     269    close ($OLD) || ( ERROR "Could not close file $fileName: $!"     && return 0 );
     270    close ($NEW) || ( ERROR "Could not close file $fileName_tmp: $!" && return 0 );
     271
     272    unless ($numOfReplacements) {
     273        WARN "Pattern not found in file";
     274        unlink ("$fileName_tmp");
     275        return 0;    # ist das ein Fehler?
     276    }
     277    else {
     278        TRACE "$numOfReplacements pattern replaced";
     279        rename ( $fileName,     "${fileName}.orig" ) || ( ERROR "Could not rename file $fileName: $!" && return 0 );
     280        rename ( $fileName_tmp, $fileName )
     281          || ( ERROR "Could not rename file $fileName_tmp to $fileName: $!" && return 0 );
     282        unlink ("${fileName}.orig") || ERROR "Could not remove file ${fileName}.orig";
     283        return 1;
     284    }
     285
     286}
     287
    230288#** @function isAlreadyRunning ()
    231289# @brief   Check if this program is already running.
Note: See TracChangeset for help on using the changeset viewer.