Skip to content

Commit 9e1f036

Browse files
committed
New builtin file_chunks()
1 parent 80c1197 commit 9e1f036

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

turbine/code/export/files.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
"turbine" "0.1"
6666
[ "set <<s>> [ turbine::file_lines_impl <<f>> <<comment>> ] " ];
6767
68+
@pure
69+
(string s[]) file_chunks(file f, string delimiter="----",
70+
string comment="#")
71+
"turbine" "0.1"
72+
[ "set <<s>> [ turbine::file_chunks_impl <<f>> <<delimiter>> <<comment>> ] " ];
73+
6874
@pure
6975
(string d) dirname_string(string p)
7076
"turbine" "0.0"

turbine/code/lib/files.tcl

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -692,19 +692,6 @@ namespace eval turbine {
692692
}
693693
}
694694

695-
proc file_lines { result input comment } {
696-
set src [ lindex $input 0 ]
697-
rule_file_helper "file_lines-$result-$src" [ list ] \
698-
[ list $comment ] [ list $src ] \
699-
$::turbine::WORK \
700-
[ list file_lines_body $result $src ]
701-
}
702-
proc file_lines_body { result input } {
703-
set input_val [ retrieve_decr_file $input ]
704-
set comment_val [ retrive_decr $comment ]
705-
set lines_val [ file_lines_impl $input_val $comment_val ]
706-
array_kv_build $result $lines_val 1 integer string
707-
}
708695
# input_file: local file representation
709696
proc file_lines_impl { input_file comment } {
710697
set input_name [ local_file_path $input_file ]
@@ -727,6 +714,40 @@ namespace eval turbine {
727714
return $lines
728715
}
729716

717+
# input_file: local file representation
718+
proc file_chunks_impl { input_file delimiter comment } {
719+
set input_name [ local_file_path $input_file ]
720+
set fp [ ::open $input_name r ]
721+
set chunk_number 0
722+
set chunks [ dict create ]
723+
# Comments are disabled if the comment string is empty
724+
# or just whitespace
725+
set comment [ string trim $comment ]
726+
set comments_enabled [ string length $comment ]
727+
while { ! [ eof $fp ] } {
728+
set chunk ""
729+
while { [ gets $fp line ] >= 0 } {
730+
if $comments_enabled {
731+
regsub "${comment}.*" $line "" line
732+
}
733+
734+
if { $line eq $delimiter } break
735+
736+
if { [ string length $line ] > 0 } {
737+
if { $chunk ne "" } {
738+
append chunk "\n"
739+
}
740+
append chunk $line
741+
}
742+
}
743+
set chunk [ string trim $chunk ]
744+
dict append chunks $chunk_number $chunk
745+
incr chunk_number
746+
}
747+
close $fp
748+
return $chunks
749+
}
750+
730751
proc file_mtime_impl { filename } {
731752
if [ catch { set result [ file mtime $filename ] } e ] {
732753
turbine_error "file_mtime(): $e"

0 commit comments

Comments
 (0)