ini.odin 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Reader and writer for a variant of the `.ini` file format with `key = value` entries in `[sections]`.
  2. package encoding_ini
  3. import "base:runtime"
  4. import "base:intrinsics"
  5. import "core:strings"
  6. import "core:strconv"
  7. import "core:io"
  8. import "core:os"
  9. import "core:fmt"
  10. _ :: fmt
  11. Options :: struct {
  12. comment: string,
  13. key_lower_case: bool,
  14. }
  15. DEFAULT_OPTIONS :: Options {
  16. comment = ";",
  17. key_lower_case = false,
  18. }
  19. Iterator :: struct {
  20. section: string,
  21. _src: string,
  22. options: Options,
  23. }
  24. iterator_from_string :: proc(src: string, options := DEFAULT_OPTIONS) -> Iterator {
  25. return {
  26. section = "",
  27. options = options,
  28. _src = src,
  29. }
  30. }
  31. // Returns the raw `key` and `value`. `ok` will be false if no more key=value pairs cannot be found.
  32. // They key and value may be quoted, which may require the use of `strconv.unquote_string`.
  33. iterate :: proc(it: ^Iterator) -> (key, value: string, ok: bool) {
  34. for line_ in strings.split_lines_iterator(&it._src) {
  35. line := strings.trim_space(line_)
  36. if len(line) == 0 {
  37. continue
  38. }
  39. if line[0] == '[' {
  40. end_idx := strings.index_byte(line, ']')
  41. if end_idx < 0 {
  42. end_idx = len(line)
  43. }
  44. it.section = line[1:end_idx]
  45. continue
  46. }
  47. if it.options.comment != "" && strings.has_prefix(line, it.options.comment) {
  48. continue
  49. }
  50. equal := strings.index(line, " =") // check for things keys that `ctrl+= = zoom_in`
  51. quote := strings.index_byte(line, '"')
  52. if equal < 0 || quote > 0 && quote < equal {
  53. equal = strings.index_byte(line, '=')
  54. if equal < 0 {
  55. continue
  56. }
  57. } else {
  58. equal += 1
  59. }
  60. key = strings.trim_space(line[:equal])
  61. value = strings.trim_space(line[equal+1:])
  62. ok = true
  63. return
  64. }
  65. it.section = ""
  66. return
  67. }
  68. Map :: distinct map[string]map[string]string
  69. load_map_from_string :: proc(src: string, allocator: runtime.Allocator, options := DEFAULT_OPTIONS) -> (m: Map, err: runtime.Allocator_Error) {
  70. unquote :: proc(val: string) -> (string, runtime.Allocator_Error) {
  71. if len(val) > 0 && (val[0] == '"' || val[0] == '\'') {
  72. v, allocated, ok := strconv.unquote_string(val)
  73. if !ok {
  74. return strings.clone(val)
  75. }
  76. if allocated {
  77. return v, nil
  78. }
  79. return strings.clone(v), nil
  80. }
  81. return strings.clone(val)
  82. }
  83. context.allocator = allocator
  84. it := iterator_from_string(src, options)
  85. for key, value in iterate(&it) {
  86. section := it.section
  87. if section not_in m {
  88. section = strings.clone(section) or_return
  89. m[section] = {}
  90. }
  91. // store key-value pair
  92. pairs := &m[section]
  93. new_key := unquote(key) or_return
  94. if options.key_lower_case {
  95. old_key := new_key
  96. new_key = strings.to_lower(key) or_return
  97. delete(old_key) or_return
  98. }
  99. pairs[new_key] = unquote(value) or_return
  100. }
  101. return
  102. }
  103. load_map_from_path :: proc(path: string, allocator: runtime.Allocator, options := DEFAULT_OPTIONS) -> (m: Map, err: runtime.Allocator_Error, ok: bool) {
  104. data := os.read_entire_file(path, allocator) or_return
  105. defer delete(data, allocator)
  106. m, err = load_map_from_string(string(data), allocator, options)
  107. ok = err == nil
  108. defer if !ok {
  109. delete_map(m)
  110. }
  111. return
  112. }
  113. save_map_to_string :: proc(m: Map, allocator: runtime.Allocator) -> (data: string) {
  114. b := strings.builder_make(allocator)
  115. _, _ = write_map(strings.to_writer(&b), m)
  116. return strings.to_string(b)
  117. }
  118. delete_map :: proc(m: Map) {
  119. allocator := m.allocator
  120. for section, pairs in m {
  121. for key, value in pairs {
  122. delete(key, allocator)
  123. delete(value, allocator)
  124. }
  125. delete(section)
  126. delete(pairs)
  127. }
  128. delete(m)
  129. }
  130. write_section :: proc(w: io.Writer, name: string, n_written: ^int = nil) -> (n: int, err: io.Error) {
  131. defer if n_written != nil { n_written^ += n }
  132. io.write_byte (w, '[', &n) or_return
  133. io.write_string(w, name, &n) or_return
  134. io.write_byte (w, ']', &n) or_return
  135. io.write_byte (w, '\n', &n) or_return
  136. return
  137. }
  138. write_pair :: proc(w: io.Writer, key: string, value: $T, n_written: ^int = nil) -> (n: int, err: io.Error) {
  139. defer if n_written != nil { n_written^ += n }
  140. io.write_string(w, key, &n) or_return
  141. io.write_string(w, " = ", &n) or_return
  142. when intrinsics.type_is_string(T) {
  143. val := string(value)
  144. if len(val) > 0 && (val[0] == ' ' || val[len(val)-1] == ' ') {
  145. io.write_quoted_string(w, val, n_written=&n) or_return
  146. } else {
  147. io.write_string(w, val, &n) or_return
  148. }
  149. } else {
  150. n += fmt.wprint(w, value)
  151. }
  152. io.write_byte(w, '\n', &n) or_return
  153. return
  154. }
  155. write_map :: proc(w: io.Writer, m: Map) -> (n: int, err: io.Error) {
  156. section_index := 0
  157. for section, pairs in m {
  158. if section_index == 0 && section == "" {
  159. // ignore section
  160. } else {
  161. write_section(w, section, &n) or_return
  162. }
  163. for key, value in pairs {
  164. write_pair(w, key, value, &n) or_return
  165. }
  166. section_index += 1
  167. }
  168. return
  169. }