home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
komix
/
DATA.Z
/
config.tcl
< prev
next >
Wrap
Text File
|
1996-11-28
|
4KB
|
116 lines
#---------------------------------------------------------------------------
#
# Copyright (c) 1993 by Westmount Technology B.V., Delft, The Netherlands.
#
# This software is furnished under a license and may be used only in
# accordance with the terms of such license and with the inclusion of
# the above copyright notice. This software or any other copies thereof
# may not be provided or otherwise made available to any other person.
# No title to and ownership of the software is hereby transferred.
#
# The information in this software is subject to change without notice
# and should not be construed as a commitment by Westmount Technology B.V.
#
#---------------------------------------------------------------------------
#
# File : @(#)config.tcl /main/titanic/3
# Original date : 1-3-1993
# Description : Common procedures for retrieving information from
# configuration-files.
#---------------------------------------------------------------------------
# SccsId = @(#)config.tcl /main/titanic/3 28 Nov 1996 Copyright 1994 Westmount Technology
#
# read 1 line from the configuration file 'file' into 'var'.
# strip of the comments
#
proc get_config_line {file var} {
upvar $var result
set result ""
while { [gets $file line] != -1 } {
# strip of comment
if {[regsub -all {(\\\\)*(\\#)|(\\\\)*#.*$} $line {\1\2\3} line_list]} {
set line $line_list
}
if { [string trim $line] == "" } continue
# join lines ending with \
if {[regsub {(([^\\])(\\\\)*)\\$} $line {\1} line_list]} {
set line $line_list
set result $result$line
continue
} else {
set result $result$line
break
}
}
if { $result == "" } {
return -1
} else {
return 0
}
}
#
# remove whitespace around the seperators in a string
#
proc sep_clean {str {sep {|}} {space { }}} {
# without the extra \'s, the condition must look like:
#regsub -all {(\\\\)*(\\.)?[$space]*([^|$sep]|$)[$space]*} $str {\1\2\3} result
# ^^ creates a loop
if {[regsub -all "(\\\\\\\\)*(\\\\.)?\[$space\]*(\[$sep\]|\$)\[$space\]*" $str {\1\2\3} result]} {
return [string trimleft $result $space]
}
return [string trimleft $str $space]
}
#
# read the configuration file with name "file_name" into array "arr"
# Default it is assumed that the first column is unique. If the key consists
# of more columns then 'keys' defines the number of columns that form the
# key.
#
proc read_configuration_file {file_name arr {keys 1} {sep {|}}} {
upvar $arr larr
set file [open $file_name]
set line_list ""
while { [get_config_line $file line] != -1 } {
# remove whitespace around the seperators
set line [sep_clean $line $sep]
# substitute \a with a
if {[regsub -all {\\n} $line {\1} line_list]} {
set line $line_list
}
set line_list [split $line $sep]
set index [lrange $line_list 0 [expr $keys-1]]
set line_list [lrange $line_list $keys end]
if { $index != "" } {
set larr($index) $line_list
}
}
close $file
}
#
# read the configuration file with name "fileName"
#
proc readConfigurationFile {fileName {sep {|}}} {
set file [open $fileName]
set lineList ""
set fileList ""
while { [get_config_line $file line] != -1 } {
# remove whitespace around the seperators
set line [sep_clean $line $sep]
# substitute \a with a
if {[regsub -all {\\n} $line {\1} lineList]} {
set line $lineList
}
set lineList [split $line $sep]
lappend fileList $lineList
}
close $file
return $fileList
}