ini.odin 4.4 KB

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