Tk Library Source Code

View Ticket
Login
Ticket UUID: 3105412
Title: ::csv incorrect format implementation on LINE BREAK records
Type: Bug Version: None
Submitter: cyril_roux Created on: 2010-11-08 17:11:17
Subsystem: csv Assigned To: andreas_kupries
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2011-12-06 04:57:59
Resolution: Duplicate Closed By: andreas_kupries
    Closed on: 2011-12-05 21:57:59
Description:
Hi,

Hope this post will allow you to fixe this annoying problem...

Many thanks
Cyril ROUX

===========
According to "Network Working Group" (rfc4180 - October 2005) about CSV format :
2.  Definition of the CSV Format
   6.  Fields containing line breaks (CRLF), double quotes, and commas
       should be enclosed in double-quotes.  For example:

       "aaa","b CRLF
       bb","ccc" CRLF
       zzz,yyy,xxx

That is to say tcllib ::csv package have to take into account LINE BREAK the
same way it manages separatorChar (,) and delimiterChar (")...

==========
Especialy, bug can be fixed in "join" procedure by adding line break (\n) to
match list of special characters (, and ") that generate quoting records :
proc ::csv::join {values {sepChar ,} {delChar \"}} {
    set out ""
    set sep {}
    foreach val $values {
        if {[string match "*\[${delChar}$sepChar\n\]*" $val]} {
            append out $sep${delChar}[string map [list $delChar ${delChar}${delChar}] $val]${delChar}
        } else {
            append out $sep${val}
        }
        set sep $sepChar
    }
    return $out
}

==========
To reproduce this problem jus t create a matrix with records containing line break
then dump it in a CSV file, then read CSV file back in TCL and consistency will be lost !

lappend ::auto_path [file join  /sw/freetools/tcl/8.5.6/Linux/rh4/x86_64  lib]

package require csv
package require struct::matrix

struct::matrix M
M add columns 3
M insert row 0 { "name" "code" "description" }
M add row { "me" {if ( test == null ){
  var = false;
} else {
  var = true;
}
} {code by:
me in JAVA} }

#display matrix
M format 2string

#then we can see consistency is preserved
#-----+----------------------+------------
#name | code                 | description
#-----+----------------------+------------
#me   | if ( test == null ){ | code by:   
#     |   var = false;       | me in JAVA 
#     | } else {             |           
#     |   var = true;        |          
#     | }                    |           
#-----+----------------------+------------

# dump matrix in CSV file
set CSV [open /tmp/test.csv w]
::csv::writematrix ::M $CSV
close $CSV

#CSV file is without delimiters around
#any values with LINE BREAK :
#
#name,code,description
#me,if ( test == null ){
#  var = false;
#} else {
#  var = true;
#}
#,code by:
#me in JAVA

# Recover CSV
set CSV [open /tmp/test.csv r]
::csv::read2matrix $CSV Mread , auto
close $CSV

#display matrix
Mread format 2string

#then now we can see consistency is lost due to NO delimiters used when line breaks!!!
#+--------------+--------------------+-----------+
#|name          |code                |description|
#|--------------+--------------------+------------
#|me            |if ( test == null ){|           |
#|--------------+--------------------+------------
#|  var = false;|                    |           |
#|--------------+--------------------+------------
#|} else {      |                    |           |
#|--------------+--------------------+------------
#|  var = true; |                    |           |
#|--------------+--------------------+------------
#|}             |                    |           |
#|--------------+--------------------+------------
#|              |code by:            |           |
#|--------------+--------------------+------------
#|me in JAVA    |                    |           |
#| -------------+--------------------+------------
User Comments: andreas_kupries added on 2011-12-06 04:57:59:

allow_comments - 1

This seems to be a duplicate of bug
https://sourceforge.net/tracker/?func=detail&atid=112883&aid=1724818&group_id=12883

That bug has been fixed in the CVS head and is slated for release with Tcllib 1.14 (next week).