Greetings.

I'm trying to write a CSV import routine for MySQL ( possibly will
extend it with a flashy GUI and release open-source ).

I'm having great difficulty doing this under Windows. If I use the code
below under Linux, it works perfectly, detecting correctly whether the
file has a Windows ( \r\n ) EOL sequence or a Unix ( \n ) EOL sequence.

However when I run it under Windows, the 1st substr() function that gets
the last 2 characters doesn't see things properly! Instead, it sees the
\n character and then a double quote ( " ) ... all the fields are
wrapped in double-quotes.

Why doesn't this code see the \r portion under Windows?

How should I be doing the stuff below so that it works?

Thanks :)

Dan

---

# Parse the 1st line of the import file and extract fieldnames
eval{
    open SOURCE, $options->{source}
        || die "Failed to open file $options->{source}.\nIs the file
already open?";
};

if ( $@ ) {
    Gtk2::Ex::Dialogs::ErrorMsg->new_and_run(
        title   => "Error opening file!",
        text    => $@
                                            );
    return FALSE;
}

# Read the 1st line
my $fieldnames = <SOURCE>;

# Close file
close SOURCE;

# Figure out what happens at the end of each line
# This should either be \n ( Unix ), or \r\n ( Windows )
my $line_terminator;

if ( substr( $fieldnames, length( $fieldnames ) -2, 2 ) eq "\r\n" ) {
   
    # We've found a Windows EOL sequence
    $line_terminator = "\r\n";
    $fieldnames = substr( $fieldnames, 0, length( $fieldnames ) - 2 );
    print "\n\nDetected a Windows ( \\r\\n ) EOL sequence\n";
   
} else {
   
    # Test for a Unix NewLine sequence
    if ( substr( $fieldnames, length( $fieldnames ) -1, 1 ) eq "\n" ) {
       
        # We've found a Unix NewLine sequence
        $line_terminator = chop( $fieldnames );
        print "\n\nDetected a Linux ( \\n ) EOL sequence\n";
       
    } else {
       
        print "\n\n$fieldnames\n\n";
       
        Gtk2::Ex::Dialogs::ErrorMsg->new_and_run(
            title   => "Please get Dan ...",
            text    => "modules::mysql::import failed to find EOL sequence!"
                                                );
        return FALSE;
       
    }
   
}

-- 
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: [EMAIL PROTECTED]
website: http://www.nusconsulting.com.au

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to