/usr/share/tcltk/tcllib1.17/csv/csv.tcl is in tcllib 1.17-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 | # csv.tcl --
#
# Tcl implementations of CSV reader and writer
#
# Copyright (c) 2001 by Jeffrey Hobbs
# Copyright (c) 2001-2013 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: csv.tcl,v 1.28 2011/11/23 02:22:10 andreas_kupries Exp $
package require Tcl 8.4
package provide csv 0.8
namespace eval ::csv {
namespace export join joinlist read2matrix read2queue report
namespace export split split2matrix split2queue writematrix writequeue
}
# ::csv::join --
#
# Takes a list of values and generates a string in CSV format.
#
# Arguments:
# values A list of the values to join
# sepChar The separator character, defaults to comma
# delChar The delimiter character, defaults to quote
# delMode If set to 'always', values are always surrounded by delChar
#
# Results:
# A string containing the values in CSV format.
proc ::csv::join {values {sepChar ,} {delChar \"} {delMode auto}} {
set out ""
set sep {}
foreach val $values {
if {($delMode eq "always") || [string match "*\[${delChar}$sepChar\r\n\]*" $val]} {
append out $sep${delChar}[string map [list $delChar ${delChar}${delChar}] $val]${delChar}
} else {
append out $sep${val}
}
set sep $sepChar
}
return $out
}
# ::csv::joinlist --
#
# Takes a list of lists of values and generates a string in CSV
# format. Each item in the list is made into a single CSV
# formatted record in the final string, the records being
# separated by newlines.
#
# Arguments:
# values A list of the lists of the values to join
# sepChar The separator character, defaults to comma
# delChar The delimiter character, defaults to quote
# delMode If set to 'always', values are always surrounded by delChar
#
# Results:
# A string containing the values in CSV format, the records
# separated by newlines.
proc ::csv::joinlist {values {sepChar ,} {delChar \"} {delMode auto}} {
set out ""
foreach record $values {
# note that this is ::csv::join
append out "[join $record $sepChar $delChar $delMode]\n"
}
return $out
}
# ::csv::joinmatrix --
#
# Takes a matrix object following the API specified for the
# struct::matrix package. Each row of the matrix is converted
# into a single CSV formatted record in the final string, the
# records being separated by newlines.
#
# Arguments:
# matrix Matrix object command.
# sepChar The separator character, defaults to comma
# delChar The delimiter character, defaults to quote
# delMode If set to 'always', values are always surrounded by delChar
#
# Results:
# A string containing the values in CSV format, the records
# separated by newlines.
proc ::csv::joinmatrix {matrix {sepChar ,} {delChar \"} {delMode auto}} {
return [joinlist [$matrix get rect 0 0 end end] $sepChar $delChar $delMode]
}
# ::csv::iscomplete --
#
# A predicate checking if the argument is a complete csv record.
#
# Arguments
# data The (partial) csv record to check.
#
# Results:
# A boolean flag indicating the completeness of the data. The
# result is true if the data is complete.
proc ::csv::iscomplete {data} {
expr {1 - [regexp -all \" $data] % 2}
}
# ::csv::read2matrix --
#
# A wrapper around "Split2matrix" reading CSV formatted
# lines from the specified channel and adding it to the given
# matrix.
#
# Arguments:
# 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 {args} {
# FR #481023
# See 'split2matrix' for the available expansion modes.
# Argument syntax:
#
#2) chan m
#3) chan m sepChar
#3) -alternate chan m
#4) -alternate chan m sepChar
#4) chan m sepChar expand
#5) -alternate chan m sepChar expand
set alternate 0
set sepChar ,
set expand none
switch -exact -- [llength $args] {
2 {
foreach {chan m} $args break
}
3 {
foreach {a b c} $args break
if {[string equal $a "-alternate"]} {
set alternate 1
set chan $b
set m $c
} else {
set chan $a
set m $b
set sepChar $c
}
}
4 {
foreach {a b c d} $args break
if {[string equal $a "-alternate"]} {
set alternate 1
set chan $b
set m $c
set sepChar $d
} else {
set chan $a
set m $b
set sepChar $c
set expand $d
}
}
5 {
foreach {a b c d e} $args break
if {![string equal $a "-alternate"]} {
return -code error "wrong#args: Should be ?-alternate? chan m ?separator? ?expand?"
}
set alternate 1
set chan $b
set m $c
set sepChar $d
set expand $e
}
0 - 1 -
default {
return -code error "wrong#args: Should be ?-alternate? chan m ?separator? ?expand?"
}
}
if {[string length $sepChar] < 1} {
return -code error "illegal separator character \"$sepChar\", is empty"
} elseif {[string length $sepChar] > 1} {
return -code error "illegal separator character \"$sepChar\", is a string"
}
set data ""
while {![eof $chan]} {
if {[gets $chan line] < 0} {continue}
# Why skip empty lines? They may be in data. Except if the
# buffer is empty, i.e. we are between records.
if {$line == {} && $data == {}} {continue}
append data $line
if {![iscomplete $data]} {
# Odd number of quotes - must have embedded newline
append data \n
continue
}
Split2matrix $alternate $m $data $sepChar $expand
set data ""
}
return
}
# ::csv::read2queue --
#
# A wrapper around "::csv::split2queue" reading CSV formatted
# lines from the specified channel and adding it to the given
# queue.
#
# Arguments:
# q The queue to add the read data too.
# chan The channel to read from.
# sepChar The separator character, defaults to comma
#
# Results:
# A list of the values in 'line'.
proc ::csv::read2queue {args} {
# Argument syntax:
#
#2) chan q
#3) chan q sepChar
#3) -alternate chan q
#4) -alternate chan q sepChar
set alternate 0
set sepChar ,
switch -exact -- [llength $args] {
2 {
foreach {chan q} $args break
}
3 {
foreach {a b c} $args break
if {[string equal $a "-alternate"]} {
set alternate 1
set chan $b
set q $c
} else {
set chan $a
set q $b
set sepChar $c
}
}
4 {
foreach {a b c d} $args break
if {![string equal $a "-alternate"]} {
return -code error "wrong#args: Should be ?-alternate? chan q ?separator?"
}
set alternate 1
set chan $b
set q $c
set sepChar $d
}
0 - 1 -
default {
return -code error "wrong#args: Should be ?-alternate? chan q ?separator?"
}
}
if {[string length $sepChar] < 1} {
return -code error "illegal separator character \"$sepChar\", is empty"
} elseif {[string length $sepChar] > 1} {
return -code error "illegal separator character \"$sepChar\", is a string"
}
set data ""
while {![eof $chan]} {
if {[gets $chan line] < 0} {continue}
# Why skip empty lines? They may be in data. Except if the
# buffer is empty, i.e. we are between records.
if {$line == {} && $data == {}} {continue}
append data $line
if {![iscomplete $data]} {
# Odd number of quotes - must have embedded newline
append data \n
continue
}
$q put [Split $alternate $data $sepChar]
set data ""
}
return
}
# ::csv::report --
#
# A report command which can be used by the matrix methods
# "format-via" and "format2chan-via". For the latter this
# command delegates the work to "::csv::writematrix". "cmd" is
# expected to be either "printmatrix" or
# "printmatrix2channel". The channel argument, "chan", has to
# be present for the latter and must not be present for the first.
#
# Arguments:
# cmd Either 'printmatrix' or 'printmatrix2channel'
# matrix The matrix to format.
# args 0 (chan): The channel to write to
#
# Results:
# None for 'printmatrix2channel', else the CSV formatted string.
proc ::csv::report {cmd matrix args} {
switch -exact -- $cmd {
printmatrix {
if {[llength $args] > 0} {
return -code error "wrong # args:\
::csv::report printmatrix matrix"
}
return [joinlist [$matrix get rect 0 0 end end]]
}
printmatrix2channel {
if {[llength $args] != 1} {
return -code error "wrong # args:\
::csv::report printmatrix2channel matrix chan"
}
writematrix $matrix [lindex $args 0]
return ""
}
default {
return -code error "Unknown method $cmd"
}
}
}
# ::csv::split --
#
# Split a string according to the rules for CSV processing.
# This assumes that the string contains a single line of CSVs
#
# Arguments:
# line The string to split
# sepChar The separator character, defaults to comma
#
# Results:
# A list of the values in 'line'.
proc ::csv::split {args} {
# Argument syntax:
#
# (1) line
# (2) line sepChar
# (2) -alternate line
# (3) -alternate line sepChar
# (3) line sepChar delChar
# (4) -alternate line sepChar delChar
set alternate 0
set sepChar ,
set delChar \"
switch -exact -- [llength $args] {
1 {
set line [lindex $args 0]
}
2 {
foreach {a b} $args break
if {[string equal $a "-alternate"]} {
set alternate 1
set line $b
} else {
set line $a
set sepChar $b
}
}
3 {
foreach {a b c} $args break
if {[string equal $a "-alternate"]} {
set alternate 1
set line $b
set sepChar $c
} else {
set line $a
set sepChar $b
set delChar $c
}
}
4 {
foreach {a b c d} $args break
if {![string equal $a "-alternate"]} {
return -code error "wrong#args: Should be ?-alternate? line ?separator? ?delimiter?"
}
set alternate 1
set line $b
set sepChar $c
set delChar $d
}
0 -
default {
return -code error "wrong#args: Should be ?-alternate? line ?separator? ?delimiter?"
}
}
if {[string length $sepChar] < 1} {
return -code error "illegal separator character ${delChar}$sepChar${delChar}, is empty"
} elseif {[string length $sepChar] > 1} {
return -code error "illegal separator character ${delChar}$sepChar${delChar}, is a string"
}
if {[string length $delChar] < 1} {
return -code error "illegal separator character \"$delChar\", is empty"
} elseif {[string length $delChar] > 1} {
return -code error "illegal separator character \"$delChar\", is a string"
}
return [Split $alternate $line $sepChar $delChar]
}
proc ::csv::Split {alternate line sepChar {delChar \"}} {
# Protect the sepchar from special interpretation by
# the regex calls below.
set sepRE \\$sepChar
set delRE \\$delChar
if {$alternate} {
# The alternate syntax requires a different parser.
# A variation of the string map / regsub parser for the
# regular syntax was tried but does not handle embedded
# doubled " well (testcase csv-91.3 was 'knownBug', sole
# one, still a bug). Now we just tokenize the input into
# the primary parts (sep char, "'s and the rest) and then
# use an explicitly coded state machine (DFA) to parse
# and convert token sequences.
## puts 1->>$line<<
set line [string map [list \
$sepChar \0$sepChar\0 \
$delChar \0${delChar}\0 \
] $line]
## puts 2->>$line<<
set line [string map [list \0\0 \0] $line]
regsub "^\0" $line {} line
regsub "\0$" $line {} line
## puts 3->>$line<<
set val ""
set res ""
set state base
## puts 4->>[::split $line \0]
foreach token [::split $line \0] {
## puts "\t*= $state\t>>$token<<"
switch -exact -- $state {
base {
if {[string equal $token "${delChar}"]} {
set state qvalue
continue
}
if {[string equal $token $sepChar]} {
lappend res $val
set val ""
continue
}
append val $token
}
qvalue {
if {[string equal $token "${delChar}"]} {
# May end value, may be a doubled "
set state endordouble
continue
}
append val $token
}
endordouble {
if {[string equal $token "${delChar}"]} {
# Doubled ", append to current value
append val ${delChar}
set state qvalue
continue
}
# Last " was end of quoted value. Close it.
# We expect current as $sepChar
lappend res $val
set val ""
set state base
if {[string equal $token $sepChar]} {continue}
# Undoubled " in middle of text. Just assume that
# remainder is another qvalue.
set state qvalue
}
default {
return -code error "Internal error, illegal parsing state"
}
}
}
## puts "/= $state\t>>$val<<"
lappend res $val
## puts 5->>$res<<
return $res
} else {
regsub -- "$sepRE${delRE}${delRE}$" $line $sepChar\0${delChar}${delChar}\0 line
regsub -- "^${delRE}${delRE}$sepRE" $line \0${delChar}${delChar}\0$sepChar line
regsub -all -- {(^${delChar}|${delChar}$)} $line \0 line
set line [string map [list \
$sepChar${delChar}${delChar}${delChar} $sepChar\0${delChar} \
${delChar}${delChar}${delChar}$sepChar ${delChar}\0$sepChar \
${delChar}${delChar} ${delChar} \
${delChar} \0 \
] $line]
set end 0
while {[regexp -indices -start $end -- {(\0)[^\0]*(\0)} $line \
-> start end]} {
set start [lindex $start 0]
set end [lindex $end 0]
set range [string range $line $start $end]
if {[string first $sepChar $range] >= 0} {
set line [string replace $line $start $end \
[string map [list $sepChar \1] $range]]
}
incr end
}
set line [string map [list $sepChar \0 \1 $sepChar \0 {} ] $line]
return [::split $line \0]
}
}
# ::csv::split2matrix --
#
# Split a string according to the rules for CSV processing.
# This assumes that the string contains a single line of CSVs.
# The resulting list of values is appended to the specified
# matrix, as a new row. The code assumes that the matrix provides
# the same interface as the queue provided by the 'struct'
# module of tcllib, "add row" in particular.
#
# Arguments:
# 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 {args} {
# FR #481023
# Argument syntax:
#
#2) m line
#3) m line sepChar
#3) -alternate m line
#4) -alternate m line sepChar
#4) m line sepChar expand
#5) -alternate m line sepChar expand
set alternate 0
set sepChar ,
set expand none
switch -exact -- [llength $args] {
2 {
foreach {m line} $args break
}
3 {
foreach {a b c} $args break
if {[string equal $a "-alternate"]} {
set alternate 1
set m $b
set line $c
} else {
set m $a
set line $b
set sepChar $c
}
}
4 {
foreach {a b c d} $args break
if {[string equal $a "-alternate"]} {
set alternate 1
set m $b
set line $c
set sepChar $d
} else {
set m $a
set line $b
set sepChar $c
set expand $d
}
}
4 {
foreach {a b c d e} $args break
if {![string equal $a "-alternate"]} {
return -code error "wrong#args: Should be ?-alternate? m line ?separator? ?expand?"
}
set alternate 1
set m $b
set line $c
set sepChar $d
set expand $e
}
0 - 1 -
default {
return -code error "wrong#args: Should be ?-alternate? m line ?separator? ?expand?"
}
}
if {[string length $sepChar] < 1} {
return -code error "illegal separator character \"$sepChar\", is empty"
} elseif {[string length $sepChar] > 1} {
return -code error "illegal separator character \"$sepChar\", is a string"
}
Split2matrix $alternate $m $line $sepChar $expand
return
}
proc ::csv::Split2matrix {alternate m line sepChar expand} {
set csv [Split $alternate $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
}
# ::csv::split2queue --
#
# Split a string according to the rules for CSV processing.
# This assumes that the string contains a single line of CSVs.
# The resulting list of values is appended to the specified
# queue, as a single item. IOW each item in the queue represents
# a single CSV record. The code assumes that the queue provides
# the same interface as the queue provided by the 'struct'
# module of tcllib, "put" in particular.
#
# Arguments:
# q The queue to write the resulting list to.
# line The string to split
# sepChar The separator character, defaults to comma
#
# Results:
# A list of the values in 'line', written to 'q'.
proc ::csv::split2queue {args} {
# Argument syntax:
#
#2) q line
#3) q line sepChar
#3) -alternate q line
#4) -alternate q line sepChar
set alternate 0
set sepChar ,
switch -exact -- [llength $args] {
2 {
foreach {q line} $args break
}
3 {
foreach {a b c} $args break
if {[string equal $a "-alternate"]} {
set alternate 1
set q $b
set line $c
} else {
set q $a
set line $b
set sepChar $c
}
}
4 {
foreach {a b c d} $args break
if {![string equal $a "-alternate"]} {
return -code error "wrong#args: Should be ?-alternate? q line ?separator?"
}
set alternate 1
set q $b
set line $c
set sepChar $d
}
0 - 1 -
default {
return -code error "wrong#args: Should be ?-alternate? q line ?separator?"
}
}
if {[string length $sepChar] < 1} {
return -code error "illegal separator character \"$sepChar\", is empty"
} elseif {[string length $sepChar] > 1} {
return -code error "illegal separator character \"$sepChar\", is a string"
}
$q put [Split $alternate $line $sepChar]
return
}
# ::csv::writematrix --
#
# A wrapper around "::csv::join" taking the rows in a matrix and
# writing them as CSV formatted lines into the channel.
#
# Arguments:
# m The matrix to take the data to write from.
# chan The channel to write into.
# sepChar The separator character, defaults to comma
#
# Results:
# None.
proc ::csv::writematrix {m chan {sepChar ,} {delChar \"}} {
set n [$m rows]
for {set r 0} {$r < $n} {incr r} {
puts $chan [join [$m get row $r] $sepChar $delChar]
}
# Memory intensive alternative:
# puts $chan [joinlist [m get rect 0 0 end end] $sepChar $delChar]
return
}
# ::csv::writequeue --
#
# A wrapper around "::csv::join" taking the rows in a queue and
# writing them as CSV formatted lines into the channel.
#
# Arguments:
# q The queue to take the data to write from.
# chan The channel to write into.
# sepChar The separator character, defaults to comma
#
# Results:
# None.
proc ::csv::writequeue {q chan {sepChar ,} {delChar \"}} {
while {[$q size] > 0} {
puts $chan [join [$q get] $sepChar $delChar]
}
# Memory intensive alternative:
# puts $chan [joinlist [$q get [$q size]] $sepChar $delChar]
return
}
|