| 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 | | |
#| -------------+--------------------+------------
|