Ignore:
Timestamp:
2024-02-13T17:10:07+13:00 (4 months ago)
Author:
davidb
Message:

Handy to have a subroutine that can read in a text file (csv,tsv) raw as a string if we know it is definitely in UTF8

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/util.pm

    r38298 r38740  
    2727
    2828use strict;
     29no strict 'subs';
    2930no strict 'refs'; # make an exception so we can use variables as filehandles
     31
     32
    3033use FileUtils;
    3134
     
    3538use File::Copy;
    3639use File::Basename;
     40
     41use gsprintf;
     42use multiread;
     43
    3744# Config for getting the perlpath in the recommended way, though it uses paths that are
    3845# hard-coded into the Config file that's generated upon configuring and compiling perl.
     
    4047# but if launched with plain "perl" (no full-path), that will be just what it returns.
    4148use Config;
     49
    4250# New module for file related utility functions - intended as a
    4351# placeholder for an extension that allows a variety of different
     
    20102018}
    20112019
     2020sub read_utf8_textfile {
     2021    my ($input_filename) = @_;
     2022
     2023    my $text_file_content_raw = undef;
     2024   
     2025    if (!open(TEXT_FILE,"<$input_filename")) {
     2026        &gsprintf::gsprintf(STDERR, "util::read_utf8_file() {ReadTextFile.could_not_open_for_reading} ($!)\n", $input_filename);
     2027    die "\n";
     2028    }
     2029   
     2030    my $file_reader = new multiread();
     2031    $file_reader->set_handle('util::TEXT_FILE');
     2032    $file_reader->read_file(\$text_file_content_raw);
     2033
     2034    close(TEXT_FILE);
     2035
     2036    my $text_file_content = decode("utf8",$text_file_content_raw);
     2037
     2038    return $text_file_content;
     2039}
     2040
     2041   
    201220421;
Note: See TracChangeset for help on using the changeset viewer.