config.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // configuration support
  2. package al
  3. /*
  4. #include <stdlib.h>
  5. #include <allegro5/allegro.h>
  6. #include "helpers.h"
  7. */
  8. import "C"
  9. import "runtime"
  10. type Config struct {
  11. handle * C.ALLEGRO_CONFIG
  12. }
  13. // Converts a config to it's underlying C pointer
  14. func (self * Config) toC() *C.ALLEGRO_CONFIG {
  15. return (*C.ALLEGRO_CONFIG)(self.handle)
  16. }
  17. // Destroys the config.
  18. func (self *Config) Destroy() {
  19. if self.handle != nil {
  20. C.al_destroy_config(self.toC())
  21. }
  22. self.handle = nil
  23. }
  24. // Wraps a C config into a go config
  25. func wrapConfigRaw(data *C.ALLEGRO_CONFIG) *Config {
  26. if data == nil {
  27. return nil
  28. }
  29. return &Config{data}
  30. }
  31. // Sets up a finalizer for this Config that calls Destroy()
  32. func (self *Config) SetDestroyFinalizer() *Config {
  33. if self != nil {
  34. runtime.SetFinalizer(self, func(me *Config) { me.Destroy() })
  35. }
  36. return self
  37. }
  38. // Wraps a C config into a go config and sets up a finalizer that calls Destroy()
  39. func wrapConfig(data *C.ALLEGRO_CONFIG) *Config {
  40. self := wrapConfigRaw(data)
  41. return self.SetDestroyFinalizer()
  42. }
  43. type ConfigSection C.ALLEGRO_CONFIG_SECTION
  44. func (cs * ConfigSection) toC() * C.ALLEGRO_CONFIG_SECTION {
  45. return (*C.ALLEGRO_CONFIG_SECTION)(cs)
  46. }
  47. func wrapConfigSectionRaw(ccs * C.ALLEGRO_CONFIG_SECTION) * ConfigSection {
  48. return (*ConfigSection)(ccs)
  49. }
  50. type ConfigEntry C.ALLEGRO_CONFIG_ENTRY
  51. func (cs * ConfigEntry) toC() * C.ALLEGRO_CONFIG_ENTRY {
  52. return (*C.ALLEGRO_CONFIG_ENTRY)(cs)
  53. }
  54. func wrapConfigEntryRaw(ccs * C.ALLEGRO_CONFIG_ENTRY) * ConfigEntry {
  55. return (*ConfigEntry)(ccs)
  56. }
  57. func SystemConfig() * Config {
  58. return wrapConfig(C.al_get_system_config())
  59. }
  60. func CreateConfig() * Config {
  61. return wrapConfig(C.al_create_config())
  62. }
  63. func (cf * Config) AddSection(name string) {
  64. cname := cstr(name); defer cstrFree(cname)
  65. C.al_add_config_section(cf.toC(), cname)
  66. }
  67. func (cf * Config) SetValue(section, key, value string) {
  68. csection := cstr(section); defer cstrFree(csection)
  69. ckey := cstr(key); defer cstrFree(ckey)
  70. cvalue := cstr(value); defer cstrFree(cvalue)
  71. C.al_set_config_value(cf.toC(), csection, ckey, cvalue)
  72. }
  73. func (cf * Config) AddComment(section, key, comment string) {
  74. csection := cstr(section); defer cstrFree(csection)
  75. ckey := cstr(key); defer cstrFree(ckey)
  76. ccomment := cstr(comment); defer cstrFree(ccomment)
  77. C.al_set_config_value(cf.toC(), csection, ckey, ccomment)
  78. }
  79. func (cf * Config) Value(section, key string) string {
  80. csection := cstr(section); defer cstrFree(csection)
  81. ckey := cstr(key); defer cstrFree(ckey)
  82. return C.GoString(C.al_get_config_value(cf.toC(), csection, ckey))
  83. }
  84. func LoadConfig(filename string) * Config{
  85. cfilename := cstr(filename); defer cstrFree(cfilename)
  86. return wrapConfig(C.al_load_config_file(cfilename))
  87. }
  88. func LoadConfigFile(file * File) * Config{
  89. return wrapConfig(C.al_load_config_file_f(file.toC()))
  90. }
  91. func (cf * Config) Save(filename string) bool {
  92. cfilename := cstr(filename); defer cstrFree(cfilename)
  93. return bool(C.al_save_config_file(cfilename, cf.toC()))
  94. }
  95. func (cf * Config) SaveFile(file * File) bool {
  96. return bool(C.al_save_config_file_f(file.toC(), cf.toC()))
  97. }
  98. func (cf * Config) MergeInto(add * Config) {
  99. C.al_merge_config_into(cf.toC(), add.toC())
  100. }
  101. func (cf * Config) Merge(cf2 * Config) * Config {
  102. return wrapConfig(C.al_merge_config(cf.toC(), cf2.toC()))
  103. }
  104. func (cf * Config) RemoveSection(name string) bool {
  105. cname := cstr(name); defer cstrFree(cname)
  106. return bool(C.al_remove_config_section(cf.toC(), cname))
  107. }
  108. func (cf * Config) RemoveKey(section, key string) bool {
  109. csection := cstr(section); defer cstrFree(csection)
  110. ckey := cstr(key); defer cstrFree(ckey)
  111. return bool(C.al_remove_config_key(cf.toC(), csection, ckey))
  112. }
  113. func (cf * Config) FirstSection() (name string, section * ConfigSection) {
  114. section = &ConfigSection{}
  115. csection := section.toC()
  116. name = C.GoString(C.al_get_first_config_section(cf.toC(), &csection))
  117. return name, section
  118. }
  119. func (section * ConfigSection) Next() (name string, ok bool) {
  120. csection := section.toC()
  121. cname := C.al_get_next_config_section(&csection)
  122. if cname == nil {
  123. name = ""
  124. ok = false
  125. } else {
  126. name = C.GoString(cname)
  127. ok = true
  128. }
  129. return name, ok
  130. }
  131. func (cf * Config) FirstEntry(section string) (name string, entry * ConfigEntry) {
  132. csection := cstr(section); defer cstrFree(csection)
  133. entry = &ConfigEntry{}
  134. centry := entry.toC()
  135. name = C.GoString(C.al_get_first_config_entry(cf.toC(), csection, &centry))
  136. return name, entry
  137. }
  138. func (entry * ConfigEntry) Next() (name string, ok bool) {
  139. centry := entry.toC()
  140. cname := C.al_get_next_config_entry(&centry)
  141. if cname == nil {
  142. name = ""
  143. ok = false
  144. } else {
  145. name = C.GoString(cname)
  146. ok = true
  147. }
  148. return name, ok
  149. }