Changeset 10512 in ntrip


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

add: blurt function

File:
1 edited

Legend:

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

    r10099 r10512  
    1515use File::Basename;
    1616use File::Spec::Functions;
     17use Proc::ProcessTable;
    1718
    1819#$| = 1; # Buffer off (print)
     
    102103}
    103104
     105# blurt opens a new file, prints the data in $data to it and closes the file.
     106# If $options->{append} is set to a true value, data will be appended to the file. Default is false, existing files will be overwritten.
     107#
     108# If the string is a Unicode string, use the utf8 option: 'blurt( $unicode_string, $file, {utf8 => 1} );'
     109sub blurt {
     110    my ( $data, $file, $options ) = @_;
     111
     112    # legacy signature
     113    if ( defined $options and ref $options eq "" ) {
     114        $options = { append => 1 };
     115    }
     116
     117    $options = {} unless defined $options;
     118
     119    local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
     120
     121    $options->{append} = 0 unless defined $options->{append};
     122
     123   #_confirm( ( $options->{append} ? "Appending" : "Writing" ) . " " . length ($data) . " bytes to $file" ) or return 1;
     124
     125    open my $fh, ">" . ( $options->{append} ? ">" : "" ) . $file
     126      or LOGCROAK("Cannot open $file for writing ($!)");
     127
     128    binmode $fh;    # Win32 wants that
     129
     130    if ( $options->{utf8} ) {
     131        binmode $fh, ":utf8";
     132    }
     133
     134    print $fh $data
     135      or LOGCROAK("Cannot write to $file ($!)");
     136    close $fh
     137      or LOGCROAK("Cannot close $file ($!)");
     138}
     139
    104140#** @function filePosition ($file, $filePos)
    105141# @brief Save position of a file in an idxfile.
Note: See TracChangeset for help on using the changeset viewer.