##
## Compiler2 - IView
##
## (C) IPN - Ingenieurbuero fuer Praezisionsnumerik
##
## Dipl.-Ing. Andreas Otto
## Ulmenstrasse 3
## D-34289 Zierenberg
## mailto:aotto@t-online.de
## http://tclcompiler.sourceforge.net
##
## Alle Rechte vorbehalten
##
package require StdLib
package require FileLib
PkgProvide IView
##
## -----------------------------------------------------------------
##
namespace eval ::IView {
variable VDIR [ file join [::Env::COMPILER_HOME] exe [::Env::COMPILER_ARCH] var View ]
variable SDIR [ file join [::Env::COMPILER_HOME] var View ]
variable FMIME [ file join $VDIR .mime ]
variable MVIEW ; ## mime -> viewer
variable MDESC ; ## mime -> desc
variable MREGE ; ## mime -> rege
variable MARGS ; ## mime -> args
}
proc ::IView::Init {} {
variable FMIME
variable MVIEW
variable MDESC
variable MREGE
variable MARGS
if {[info exists MVIEW] || ![ file exists $FMIME ]} return
set _D {}
set _V {}
set _R {}
set _A {}
foreach { M R D V A } [ ReadFile $FMIME ] {
lappend _V $M $V
lappend _D $M $D
lappend _R $M $R
lappend _A $M $A
}
array set MVIEW $_V
array set MDESC $_D
array set MREGE $_R
array set MARGS $_A
}
::IView::Init
proc ::IView::ErrorN { args } {
::StdLib::ErrorN {VIEWER ERROR} $args
}
##
## --------------------------------------------------------------
## public
##
proc ::IView::Exec { Typ File } {
variable MVIEW
variable MREGE
variable MARGS
::FileLib::Check { exist readable file } $File
if {![ info exists MREGE($Typ) ]} {
ErrorN "mime type \"$Typ\" was not defined"
}
if {![ regexp $MREGE($Typ) $File ]} {
ErrorN "file \"$File\"" "does not fit regular expression \"$MREGE($Typ)\""
}
::StdLib::Exec "$MVIEW($Typ) $MARGS($Typ) $File"
}
proc ::IView::ExecN { File } {
variable MVIEW
variable MREGE
::FileLib::Check { exist readable file } $File
foreach { M R } [ array get MREGE ] {
if {![ regexp $R $File ]} continue
set C "$MVIEW($M) $File"
::StdLib::Push ERROR ERROR
if {[catch {::StdLib::Exec $C} ERR ]} {
ErrorN "the exec of \"$C\" failed" "$ERR" \
"please use \"cct view --add ...\" to add the right mime-handler"
}
::StdLib::Pop ERROR
return
}
ErrorN "can not file mime type for" $File
}
##
## --------------------------------------------------------------
## Mime interface
##
proc ::IView::List {} {
variable MVIEW
variable MDESC
variable MREGE
variable MARGS
set RET {}
foreach {M V} [ array get MVIEW ] {
lappend RET $M $V $MARGS($M) $MREGE($M) $MDESC($M)]
}
return $RET
}
proc ::IView::MimeList {} {
puts ""
puts [ format {%5s : %-10s : %-5s : %-15s : %s} mime viewer args regexp description ]
puts ""
foreach {M V A R D} [ List ] {
puts [ format {%5s : %-10s : %-5s : %-15s : %s} $M $V $A $R $D]
}
puts ""
}
proc ::IView::MimeAdd { List } {
variable FMIME
::FileLib::Append $FMIME $List
}
##
## --------------------------------------------------------------
## Doc interface
##
proc ::IView::DocAdd { PKG List {DIR žNOž}} {
if {$DIR == "žNOž"} {
set DIR [ file join [::Env::COMPILER_HOME] var View $PKG ]
} else {
set DIR [ file join $DIR var View $PKG ]
}
file mkdir [ file dirname $DIR ]
WriteFile $DIR $List
}
proc ::IView::DocDel { PKG {DIR žNOž} } {
if {$DIR == "žNOž"} {
set DIR [ file join [::Env::COMPILER_HOME] var View $PKG ]
} else {
set DIR [ file join $DIR var View $PKG ]
}
file delete -force $DIR
}
proc ::IView::DocList { PKG } {
variable SDIR
foreach F [ glob -nocomplain -directory $SDIR $PKG ] {
::FileLib::Check { exist readable file } $F
puts "\nPACKAGE [file tail $F]\n"
set I 0
foreach { M F } [ ReadFile $F ] {
puts [ format { %2i) %-5s : %s} $I $M [ file tail $F ] ]
incr I
}
}
}
proc ::IView::DocExec { PKG NUM} {
variable SDIR
set D [ file join $SDIR ]
set F [ glob -nocomplain -directory $D $PKG ]
if {$F == ""} {
::StdLib::Error "(BUG) can resolve package name \"PKG\""
}
if {![ string is integer $NUM ]} {
::StdLib::Error "expect integer but got \"$NUM\""
}
if {[ llength $F ] > 1} {
ErrorN "get more than one package \"$F\""
}
foreach { M D } [ lrange [ ReadFile $F ] [expr {$NUM*2}] end ] break
Exec $M [ file join [::Env::COMPILER_HOME] $D ]
}