Tk Library Source Code

Artifact [327945d27f]
Login

Artifact 327945d27fd5897a1dfe5297edad621900ba040b:

Attachment "481023.diff" to ticket [481023ffff] added by andreas_kupries 2001-11-13 05:21:12.
Index: ChangeLog
===================================================================
RCS file: /cvsroot/tcllib/tcllib/ChangeLog,v
retrieving revision 1.76
diff -u -r1.76 ChangeLog
--- ChangeLog	2001/11/12 17:23:18	1.76
+++ ChangeLog	2001/11/12 22:20:03
@@ -1,7 +1,10 @@
-2001-11-11  Andreas Kupries  <[email protected]>
+2001-11-12  Andreas Kupries  <[email protected]>
 
+	* csv: Implemented FR #481023.
+
 	* textutil: Added 'expander' code by William H. Duquette
-	  <[email protected]>.
+	  <[email protected]>. Added option -strictlength to
+	  adjust. Code by Dan Kuchler <[email protected]>.
 
 2001-11-09  Joe English  <[email protected]>
 
Index: modules/csv/ChangeLog
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/csv/ChangeLog,v
retrieving revision 1.7
diff -u -r1.7 ChangeLog
--- modules/csv/ChangeLog	2001/10/14 20:16:50	1.7
+++ modules/csv/ChangeLog	2001/11/12 22:20:03
@@ -1,3 +1,11 @@
+2001-11-12  Andreas Kupries  <[email protected]>
+
+	* csv.test:
+	* cvs.n:
+	* csv.tcl (split2matrix, read2matrix): Implemented FR
+	  #481023. Added additional expansion behaviours, controlled via
+	  an optional argument.
+
 2001-10-14  Jeff Hobbs  <[email protected]>
 
 	* csv.test (csv-1.7): 
Index: modules/csv/csv.n
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/csv/csv.n,v
retrieving revision 1.5
diff -u -r1.5 csv.n
--- modules/csv/csv.n	2001/08/02 16:38:06	1.5
+++ modules/csv/csv.n	2001/11/12 22:20:03
@@ -19,7 +19,7 @@
 .sp
 \fB::csv::joinlist\fR \fIvalues {sepChar ,}\fR
 .sp
-\fB::csv::read2matrix\fR \fIchan m {sepChar ,}\fR
+\fB::csv::read2matrix\fR \fIchan m {sepChar ,} {expand none}\fR
 .sp
 \fB::csv::read2queue\fR \fIchan q {sepChar ,}\fR
 .sp
@@ -27,6 +27,8 @@
 .sp
 \fB::csv::split\fR \fIline {sepChar ,}\fR
 .sp
+\fB::csv::split2matrix\fR \fIq line {sepChar ,} {expand none}\fR
+.sp
 \fB::csv::split2queue\fR \fIq line {sepChar ,}\fR
 .sp
 \fB::csv::writematrix\fR \fIm chan {sepChar ,}\fR
@@ -55,10 +57,11 @@
 the result. The elements of each record are formatted as usual (via
 \fB::csv::join\fR).
 .TP
-\fB::csv::read2matrix\fR \fIchan m {sepChar ,}\fR
+\fB::csv::read2matrix\fR \fIchan m {sepChar ,} {expand none}\fR
 A wrapper around \fB::csv::split2matrix\fR (see below) reading from
 CSV-formatted lines from the specified channel (until EOF) and adding
-it to the given matrix.
+it to the given matrix. For an explanation of the \fIexpand\fR
+argument see \fB::csv::split2matrix\fR.
 .TP
 \fB::csv::read2queue\fR \fIchan q {sepChar ,}\fR
 A wrapper around \fB::csv::split2queue\fR (see below) reading from
@@ -79,12 +82,33 @@
 each other can be defined by the caller, via \fIsepChar\fR, but this
 is optional. The default is ",".
 .TP
-\fB::csv::split2matrix\fR \fIm line {sepChar ,}\fR
+\fB::csv::split2matrix\fR \fIm line {sepChar ,} {expand none}\fR
 The same as \fB::csv::split\fR, but appends the resulting list as a
-new row to the matrix \fIm\fR, using the method \fBadd row\fR. It is the
-responsibility of the caller to ensure that the matrix has enough
-columns to contain the full line. If there are not enough columns the
-list of values is silently truncated at the end to fit.
+new row to the matrix \fIm\fR, using the method \fBadd row\fR. The
+expansion mode specified via \fIexpand\fR determines how the command
+handles a matrix with less columns than contained in \fIline\fR. The
+allowed modes are:
+.RS
+.TP
+\fBnone\fR
+This is the \fBdefault mode\fR. In this mode it is the responsibility
+of the caller to ensure that the matrix has enough columns to contain
+the full line. If there are not enough columns the list of values is
+silently truncated at the end to fit.
+.TP
+\fBempty\fR
+In this mode the command expands an empty matrix to hold all columns
+of the specified line, but goes no further. The overall effect is that
+the first of a series of lines determines the number of columns in the
+matrix and all following lines are truncated to that size, as if mode
+\fBnone\fR was set.
+.TP
+\fBauto\fR
+In this mode the command expands the matrix as needed to hold all
+columns contained in \Iline\fR. The overall effect is that after
+adding a series of lines the matrix will have enough columns to hold
+all columns of the longest line encoutered so far.
+.RE
 .TP
 \fB::csv::split2queue\fR \fIq line {sepChar ,}\fR
 The same as \fB::csv::split\fR, but appending the resulting list as a
Index: modules/csv/csv.tcl
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/csv/csv.tcl,v
retrieving revision 1.7
diff -u -r1.7 csv.tcl
--- modules/csv/csv.tcl	2001/10/14 20:16:50	1.7
+++ modules/csv/csv.tcl	2001/11/12 22:20:03
@@ -77,15 +77,19 @@
 #	m		The matrix to add the read data too.
 #	chan		The channel to read from.
 #	sepChar		The separator character, defaults to comma
+#	expand		The expansion mode. The default is none
 #
 # Results:
 #	A list of the values in 'line'.
 
-proc ::csv::read2matrix {chan m {sepChar ,}} {
+proc ::csv::read2matrix {chan m {sepChar ,} {expand none}} {
+    # FR #481023
+    # See 'split2matrix' for the available expansion modes.
+
     while {![eof $chan]} {
 	if {[gets $chan line] < 0} {continue}
 	if {$line == {}} {continue}
-	split2matrix $m $line $sepChar
+	split2matrix $m $line $sepChar $expand
     }
     return
 }
@@ -202,12 +206,43 @@
 #	m		The matrix to write the resulting list to.
 #	line		The string to split
 #	sepChar		The separator character, defaults to comma
+#	expand		The expansion mode. The default is none
 #
 # Results:
 #	A list of the values in 'line', written to 'q'.
+
+proc ::csv::split2matrix {m line {sepChar ,} {expand none}} {
+    # FR #481023
 
-proc ::csv::split2matrix {m line {sepChar ,}} {
-    $m add row [split $line $sepChar]
+    set csv [split $line $sepChar]
+
+    # Expansion modes
+    # - none  : default, behaviour of original implementation.
+    #           no expansion is done, lines are silently truncated
+    #           to the number of columns in the matrix.
+    #
+    # - empty : A matrix without columns is expanded to the number
+    #           of columns in the first line added to it. All
+    #           following lines are handled as if "mode == none"
+    #           was set.
+    #
+    # - auto  : Full auto-mode. The matrix is expanded as needed to
+    #           hold all columns of all lines.
+
+    switch -exact -- $expand {
+	none {}
+	empty {
+	    if {[$m columns] == 0} {
+		$m add columns [llength $csv]
+	    }
+	}
+	auto {
+	    if {[$m columns] < [llength $csv]} {
+		$m add columns [expr {[llength $csv] - [$m columns]}]
+	    }
+	}
+    }
+    $m add row $csv
     return
 }
 
Index: modules/csv/csv.test
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/csv/csv.test,v
retrieving revision 1.3
diff -u -r1.3 csv.test
--- modules/csv/csv.test	2001/10/14 20:16:50	1.3
+++ modules/csv/csv.test	2001/11/12 22:20:03
@@ -162,6 +162,27 @@
 } {1255}
 
 
+test csv-5.5 {reading csv files, empty expansion mode} {
+    set f [open [file join $::tcltest::testsDirectory mem_debug_bench.csv] r]
+    ::struct::matrix m
+    ::csv::read2matrix $f m , empty
+    close $f
+    set result [m get rect 0 227 end 231]
+    m destroy
+    set result
+} {{227 {STR append (1MB + 1MB * 3)} 125505 327765 38.29} {228 {STR append (1MB + 1MB * 5)} 158507 855295 18.53} {229 {STR append (1MB + (1b + 1K + 1b) * 100)} 33101 174031 19.02} {230 {STR info locals match} 946 1521 62.20} {231 {TRACE no trace set} 34 121 28.10}}
+
+test csv-5.6 {reading csv files, auto expansion mode} {
+    set f [open [file join $::tcltest::testsDirectory mem_debug_bench.csv] r]
+    ::struct::matrix m
+    m add columns 1
+    ::csv::read2matrix $f m , auto
+    close $f
+    set result [m get rect 0 227 end 231]
+    m destroy
+    set result
+} {{227 {STR append (1MB + 1MB * 3)} 125505 327765 38.29} {228 {STR append (1MB + 1MB * 5)} 158507 855295 18.53} {229 {STR append (1MB + (1b + 1K + 1b) * 100)} 33101 174031 19.02} {230 {STR info locals match} 946 1521 62.20} {231 {TRACE no trace set} 34 121 28.10}}
+
 tcltest::makeFile {} eval-out1.csv
 tcltest::makeFile {} eval-out2.csv
 tcltest::makeFile {} eval-out3.csv
Index: modules/textutil/ChangeLog
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/textutil/ChangeLog,v
retrieving revision 1.11
diff -u -r1.11 ChangeLog
--- modules/textutil/ChangeLog	2001/11/12 18:33:10	1.11
+++ modules/textutil/ChangeLog	2001/11/12 22:20:03
@@ -1,4 +1,4 @@
-2001-11-08  Andreas Kupries  <[email protected]>
+2001-11-12  Andreas Kupries  <[email protected]>
 
 	* textutil.n:
 	* adjust.tcl: