home *** CD-ROM | disk | FTP | other *** search
/ PC World 1997 November / PCWorld_1997-11_cd.bin / software / programy / komix / DATA.Z / filehandle.tcl < prev    next >
Text File  |  1997-05-14  |  7KB  |  262 lines

  1. #---------------------------------------------------------------------------
  2. #
  3. #      (c)     Cayenne Software Inc.    1997
  4. #
  5. #      File:           @(#)filehandle.tcl    /main/hindenburg/11
  6. #      Author:         <generated>
  7. #      Description:
  8. #---------------------------------------------------------------------------
  9. # SccsId = @(#)filehandle.tcl    /main/hindenburg/11   14 May 1997 Copyright 1997 Cayenne Software Inc.
  10.  
  11. # Start user added include file section
  12. require cgen_msg.tcl
  13. require machdep.tcl
  14. # End user added include file section
  15.  
  16.  
  17. # This class does generic file handling.
  18.  
  19. Class FileHandler : {GCObject} {
  20.     constructor
  21.     method destructor
  22.     method getFileName
  23.     method openFile
  24.     method closeFile
  25.     method checkUniqueFiles
  26.     method writeSectionToFile
  27.     method writeSectionToNamedFile
  28.     method sourceTclFiles
  29.     method sourceAllTclFilesInSystem
  30.     method importExternal
  31.     method findExternalSource
  32.     method setImpFrom
  33. }
  34.  
  35. constructor FileHandler {class this} {
  36.     set this [GCObject::constructor $class $this]
  37.     # Start constructor user section
  38.     # End constructor user section
  39.     return $this
  40. }
  41.  
  42. method FileHandler::destructor {this} {
  43.     # Start destructor user section
  44.     # End destructor user section
  45. }
  46.  
  47.  
  48. # Returns file name for <class> with type <fileType>.
  49. #
  50. method FileHandler::getFileName {this class fileType} {
  51.     return "[$class getName].$fileType"
  52. }
  53.  
  54.  
  55. # Opens the file for <class> with type <fileType>.
  56. # Returns a file descriptor on success,
  57. # the empty string on failure.
  58. #
  59. method FileHandler::openFile {this class fileType} {
  60.     set fileName [$this getFileName $class $fileType]
  61.     if [catch {set fd [fstorage::open $fileName r]}] {
  62.     return ""
  63.     }
  64.     return $fd
  65. }
  66.  
  67.  
  68. # Closes a file that was opened
  69. # by openFile
  70. #
  71. method FileHandler::closeFile {this fileDesc} {
  72.     if {$fileDesc != ""} {
  73.         fstorage::close $fileDesc
  74.     }
  75. }
  76.  
  77.  
  78. # Checks whether the classes in the current
  79. # system map to different file names.
  80. # If this is not the case, an error is generated.
  81. #
  82. method FileHandler::checkUniqueFiles {this classList} {
  83.     set fileToClass [Dictionary new]
  84.     # any type will do for this check
  85.     set aType [[$this getFileTypes] index 0]
  86.  
  87.     $classList foreach class {
  88.     set fileName [$this getFileName $class $aType]
  89.     if [$class isExternal] {
  90.         continue
  91.     }
  92.     set className [$class getName]
  93.     if [$fileToClass exists $fileName] {
  94.         error "Classes '$className' and '[$fileToClass set $fileName]' map to the same file name" "" ERR_UNIQUE_FILENAME
  95.     }
  96.     $fileToClass set $fileName $className
  97.     }
  98. }
  99.  
  100.  
  101. # Writes the contents of <section> to the file for
  102. # <class> with type <fileType>. If
  103. # - the file existed already and has not changed or 
  104. # - the section is empty or
  105. # - there were checking errors
  106. # the file is not written and 0 is returned.
  107. # Otherwise 1 is returned.
  108. #
  109. method FileHandler::writeSectionToFile {this section class fileType} {
  110.     if { [$section contents] == "" } {
  111.     return 0
  112.     }
  113.  
  114.     set fileName [$this getFileName $class $fileType]
  115.  
  116.     if [section_equals_file $section $fileName] {
  117.     puts "$fileName has not changed: file not written"
  118.     return 0
  119.     }
  120.  
  121.     if {[M4CheckManager::getErrorCount] > 0} {
  122.     puts "Not saving $fileName because of previous errors"
  123.     return 0
  124.     }
  125.  
  126.     puts stdout "Creating $fileName"
  127.  
  128.     if [catch {set fd [fstorage::open $fileName w]} reason] {
  129.     puts stderr $reason
  130.     m4_error $E_FILE_OPEN_WRITE $fileName
  131.     return 0
  132.     } else {
  133.     if [catch {$this setImpFrom $fileName $class} reason] {
  134.         puts stderr $reason
  135.     }
  136.  
  137.     $section write $fd
  138.     fstorage::close $fd
  139.     }
  140.  
  141.     return 1
  142. }
  143.  
  144.  
  145. # Writes the contents of <section> to file <fileName>.
  146. #
  147. method FileHandler::writeSectionToNamedFile {this section fileName} {
  148.     if [catch {set fd [fstorage::open $fileName w]} reason] {
  149.         puts stderr $reason
  150.         m4_error $E_FILE_OPEN_WRITE $fileName
  151.         return
  152.     }
  153.  
  154.     $section write $fd
  155.     fstorage::close $fd
  156. }
  157.  
  158.  
  159. # Sources tcl files in the current system,
  160. # and tcl files in the system called Tcl if it exists.
  161. #
  162. method FileHandler::sourceTclFiles {this} {
  163.     set cc [ClientContext::global]
  164.  
  165.     set thisSystem [$cc levelNameAt System]
  166.  
  167.     if {![catch {fstorage::goto_system Tcl.system} msg]} {
  168.     $this sourceAllTclFilesInSystem
  169.     fstorage::goto_system $thisSystem
  170.     }
  171.  
  172.     $this sourceAllTclFilesInSystem
  173. }
  174.  
  175.  
  176. # Sources all Tcl files in the current system.
  177. #
  178. method FileHandler::sourceAllTclFilesInSystem {this} {
  179.     foreach fileName [fstorage::dir tcl] {
  180.     set absoluteFileName [fstorage::get_uenv_path $fileName absolute]
  181.     if [catch {source $absoluteFileName}] {
  182.         m4_error $E_USER_TCL $absoluteFileName
  183.         puts stderr $errorInfo
  184.     }
  185.     }
  186. }
  187.  
  188.  
  189. # Imports the external file <fileName> in the file
  190. # for <class> with type <fileType>,
  191. # if <fileName> exists. This is a legacy 
  192. # method with old code.
  193. #
  194. method FileHandler::importExternal {this class fileType fileName} {
  195.     set class_name [$class getName]
  196.     set newFileName [$this getFileName $class $fileType]
  197.  
  198.     set absoluteFileName [$this findExternalSource $fileName]
  199.     if {$absoluteFileName == ""} {
  200.     puts -nonewline "ERROR: class '[$class getName]': "
  201.     puts "external class source file '$fileName' not found"
  202.     return
  203.     }
  204.  
  205.     puts "Importing external '$absoluteFileName'"
  206.     puts "Creating $newFileName"
  207.  
  208.     if [catch {set out [fstorage::open $newFileName w]} reason] {
  209.     puts stderr $reason
  210.     m4_error $E_FILE_OPEN_WRITE $newFileName
  211.     return
  212.     }
  213.  
  214.     if [catch {fstorage::set_imp_from $newFileName [$class getName]} reason] {
  215.     puts stderr $reason
  216.     }
  217.  
  218.     set max 8092
  219.     set in [open $absoluteFileName r]
  220.     while {[set result [read $in $max]] != ""} {
  221.     puts -nonewline $out $result
  222.     }
  223.  
  224.     close $in
  225.     fstorage::close $out
  226. }
  227.  
  228.  
  229. # Looks for a file named <fileName> in the current
  230. # directory and in all directories specified by the
  231. # global exsrc_searchpath. Returns the absolute
  232. # file name on success, the empty string otherwise.
  233. #
  234. method FileHandler::findExternalSource {this fileName} {
  235.     if [file exists $fileName] {
  236.     return $fileName
  237.     }
  238.  
  239.     global exsrc_searchpath
  240.     if {! [info exists exsrc_searchpath]} {
  241.     return ""
  242.     }
  243.  
  244.     set sep [searchPathSeparator]
  245.     foreach dirName [split $exsrc_searchpath $sep] {
  246.     set absoluteFileName [path_name concat $dirName $fileName]
  247.     if [file exists $absoluteFileName] {
  248.         return $absoluteFileName
  249.     }
  250.     }
  251.     return ""
  252. }
  253.  
  254. method FileHandler::setImpFrom {this fileName class} {
  255.     return [fstorage::set_imp_from $fileName [$class getName]]
  256. }
  257.  
  258. # Do not delete this line -- regeneration end marker
  259.  
  260.