sexpr_config_file.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ## $Id$
  2. ##
  3. ## Flexlay - A Generic 2D Game Editor
  4. ## Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  5. ##
  6. ## This program is free software: you can redistribute it and/or modify
  7. ## it under the terms of the GNU General Public License as published by
  8. ## the Free Software Foundation, either version 3 of the License, or
  9. ## (at your option) any later version.
  10. ##
  11. ## This program is distributed in the hope that it will be useful,
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ## GNU General Public License for more details.
  15. ##
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. require "sexpr.rb"
  19. # Options is a class to manage configuration options and save/load of
  20. # them
  21. class SExprConfigFile
  22. # The name is used to derive the location of the config file in the
  23. # file system, name must not contain / or \
  24. def initialize(name, &block)
  25. @name = name
  26. @defaults = {}
  27. @options = {}
  28. home = ENV['HOME']
  29. if home then
  30. dir = home + "/.#{name}"
  31. if not (File.exists?(dir) and File.directory?(dir)) then
  32. Dir::mkdir(dir)
  33. end
  34. @filename = home + "/.#{@name}/config.scm"
  35. else
  36. # assuming windows
  37. @filename = "#{name}-config.scm"
  38. end
  39. if block then
  40. instance_eval(&block);
  41. read()
  42. end
  43. end
  44. # Register default values
  45. def register(name, value)
  46. @defaults[name] = value
  47. end
  48. def get(name)
  49. if @options.has_key?(name) then
  50. return @options[name]
  51. elsif @defaults.has_key?(name) then
  52. return @defaults[name]
  53. else
  54. raise "Error: Options:get: don't have a #{name} option"
  55. end
  56. end
  57. def set(name, value)
  58. if @defaults.has_key?(name) then
  59. @options[name] = value
  60. else
  61. raise "Error: Options:set: don't have a #{name} option"
  62. end
  63. end
  64. def read()
  65. sexpr = SExpression.new_from_file(@filename)
  66. sexpr = sexpr.cdr()
  67. sexpr.each_pair() {|key, value|
  68. if @defaults.has_key?(key.to_s) then
  69. if @defaults[key.to_s].is_a?(Array) then
  70. @options[key.to_s] = value.value()
  71. else
  72. @options[key.to_s] = value.value()[0]
  73. end
  74. else
  75. puts "Warning: Ignoring unknown config key: #{key}"
  76. end
  77. }
  78. end
  79. def value2sexpr(value)
  80. if value.is_a?(Fixnum) or value.is_a?(Float) then
  81. return value.to_s
  82. elsif value.is_a?(String)
  83. return value.inspect
  84. elsif value.is_a?(TrueClass) or value.is_a?(FalseClass)
  85. if value then
  86. return "#t"
  87. else
  88. return "#f"
  89. end
  90. elsif value.is_a?(Array)
  91. str = ""
  92. value.each() {|v|
  93. str += value2sexpr(v)
  94. str += " "
  95. }
  96. return str
  97. else
  98. raise "Error: Unknown option type: #{value.class}"
  99. end
  100. end
  101. def write()
  102. # FIXME: Move this over to a SExpression writer
  103. f = File.new(@filename, "w")
  104. f.puts("(#{@name}-config")
  105. @defaults.each_pair {|key, value|
  106. if @options.has_key?(key) and @options[key] != nil then
  107. f.puts(" (%-20s %s)" % [key, value2sexpr(@options[key])])
  108. else
  109. f.puts(";; (%-20s %s)" % [key, value2sexpr(value)])
  110. end
  111. }
  112. f.puts(")\n\n;; EOF ;;")
  113. f.close()
  114. end
  115. end
  116. # EOF #