_filepool.tcl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. namespace eval filepool {
  2. set_help_text filepool \
  3. {Manage filepool settings
  4. filepool list
  5. Shows the currently defined filepool entries.
  6. filepool add -path <path> -types <typelist> [-position <pos>]
  7. Add a new entry. Each entry must have a directory and a list of filetypes.
  8. Possible filetypes are 'system_rom', 'rom', 'disk' and 'tape'. Optionally
  9. you can specify the position of this new entry in the list of existing
  10. entries (by default new entries are added at the end).
  11. filepool remove <position>
  12. Remove the filepool entry at the given position.
  13. filepool reset
  14. Reset the filepool settings to the default values.
  15. }
  16. proc filepool_completion {args} {
  17. if {[llength $args] == 2} {
  18. return [list list add remove reset]
  19. }
  20. return [list -path -types -position system_rom rom disk tape]
  21. }
  22. set_tabcompletion_proc filepool [namespace code filepool_completion]
  23. proc filepool {args} {
  24. set cmd [lindex $args 0]
  25. set args [lrange $args 1 end]
  26. switch -- $cmd {
  27. "list" {filepool_list}
  28. "add" {filepool_add {*}$args}
  29. "remove" {filepool_remove $args}
  30. "reset" {filepool_reset}
  31. "default" {
  32. error "Invalid subcommand, expected one of 'list add remove reset', but got '$cmd'"
  33. }
  34. }
  35. }
  36. proc filepool_list {} {
  37. set result ""
  38. set i 1
  39. foreach pool $::__filepool {
  40. append result "$i: [dict get $pool -path] \[[dict get $pool -types]\]\n"
  41. incr i
  42. }
  43. return $result
  44. }
  45. proc filepool_checktypes {types} {
  46. set valid [list "system_rom" "rom" "disk" "tape"]
  47. foreach type $types {
  48. if {$type ni $valid} {
  49. error "Invalid type, expected one of '$valid', but got '$type'"
  50. }
  51. }
  52. }
  53. proc filepool_add {args} {
  54. set pos [llength $::__filepool]
  55. set path ""
  56. set types ""
  57. foreach {name value} $args {
  58. if {$name eq "-position"} {
  59. set pos [expr {$value - 1}]
  60. } elseif {$name eq "-path"} {
  61. set path $value
  62. } elseif {$name eq "-types"} {
  63. filepool_checktypes $value
  64. set types $value
  65. } else {
  66. error "Unknown option: $name"
  67. }
  68. }
  69. if {($pos < 0) || ($pos > [llength $::__filepool])} {
  70. error "Value out of range: [expr {$pos + 1}]"
  71. }
  72. if {$path eq ""} {
  73. error "Missing -path"
  74. }
  75. if {$types eq ""} {
  76. error "Missing -types"
  77. }
  78. set newpool [dict create -path $path -types $types]
  79. if {$pos == [llength $::__filepool]} {
  80. lappend ::__filepool $newpool
  81. } else {
  82. set ::__filepool [lreplace $::__filepool $pos -1 $newpool]
  83. }
  84. return ""
  85. }
  86. proc filepool_remove {id} {
  87. if {($id < 1) || ($id > [llength $::__filepool])} {
  88. error "Value out of range: $id"
  89. }
  90. set idx [expr {$id - 1}]
  91. set ::__filepool [lreplace $::__filepool $idx $idx]
  92. return ""
  93. }
  94. proc filepool_reset {} {
  95. unset ::__filepool
  96. }
  97. proc get_paths_for_type {type} {
  98. set result [list]
  99. foreach pool $::__filepool {
  100. set types [dict get $pool -types]
  101. if {$type in $types} {
  102. lappend result [dict get $pool -path]
  103. }
  104. }
  105. return $result
  106. }
  107. namespace export filepool
  108. } ;# namespace filepool
  109. namespace import filepool::*