boxcl

BOXcl: a single-script Sokoban clone in Tcl/Tk 8.6
git clone git://git.luxferre.top/boxcl.git
Log | Files | Refs

slcread.tcl (1279B)


      1 #!/usr/bin/env tclsh
      2 # slcread: convert a Sokoban levelset in the (XML-based) SLC format
      3 # into the plaintext format readable by BOXcl, depends on TclXML only
      4 # Usage: tclsh slcread.tcl input.scl > output.txt
      5 # Created by Luxferre in 2024, released into public domain
      6 
      7 package require xml
      8 
      9 set levelsetfile ""
     10 if {$argc > 0} { # get the level set file name
     11   set levelsetfile [lindex $argv 0]
     12 } else {
     13   puts "Missing input file!"
     14   exit 1
     15 }
     16 
     17 set fp [open $levelsetfile r]
     18 fconfigure $fp -encoding utf-8 -translation auto
     19 set fdata [read $fp]
     20 close $fp
     21 
     22 set dataout 0
     23 set xtitle 0
     24 
     25 proc edata {name attlist args} {
     26   global dataout xtitle
     27   if {$name eq "Title"} {set xtitle 1}
     28   if {$name eq "LevelCollection"} {
     29     puts "Author: [dict get $attlist Copyright]"
     30   }
     31   if {$name eq "Level"} {
     32     puts "\nTitle: [dict get $attlist Id]"
     33   }
     34   if {$name eq "L"} {
     35     set dataout 1
     36   }
     37 }
     38 
     39 proc edataend {name args} {
     40   global dataout
     41   if {$name eq "L"} {
     42     set dataout 0
     43   }
     44 }
     45 
     46 proc cdata {data args} {
     47   global dataout xtitle
     48   if {$dataout eq 1} {
     49     puts $data
     50   }
     51   if {$xtitle eq 1} {
     52     puts "Title: $data"
     53     set xtitle 0
     54   }
     55 }
     56 
     57 set parser [::xml::parser -characterdatacommand cdata -elementstartcommand edata -elementendcommand edataend]
     58 $parser parse $fdata
     59 puts ""