ini.odin 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. if len(val) > 0 && (val[0] == '"' || val[0] == '\'') {
  71. v, allocated, ok := strconv.unquote_string(val)
  72. if !ok {
  73. return strings.clone(val)
  74. }
  75. if allocated {
  76. return v, nil
  77. }
  78. return strings.clone(v), nil
  79. }
  80. return strings.clone(val)
  81. }
  82. context.allocator = allocator
  83. it := iterator_from_string(src, options)
  84. for key, value in iterate(&it) {
  85. section := it.section
  86. if section not_in m {
  87. section = strings.clone(section) or_return
  88. m[section] = {}
  89. }
  90. // store key-value pair
  91. pairs := &m[section]
  92. new_key := unquote(key) or_return
  93. if options.key_lower_case {
  94. old_key := new_key
  95. new_key = strings.to_lower(key) or_return
  96. delete(old_key) or_return
  97. }
  98. pairs[new_key] = unquote(value) or_return
  99. }
  100. return
  101. }
  102. load_map_from_path :: proc(path: string, allocator: runtime.Allocator, options := DEFAULT_OPTIONS) -> (m: Map, err: runtime.Allocator_Error, ok: bool) {
  103. data := os.read_entire_file(path, allocator) or_return
  104. defer delete(data, allocator)
  105. m, err = load_map_from_string(string(data), allocator, options)
  106. ok = err == nil
  107. defer if !ok {
  108. delete_map(m)
  109. }
  110. return
  111. }
  112. save_map_to_string :: proc(m: Map, allocator: runtime.Allocator) -> (data: string) {
  113. b := strings.builder_make(allocator)
  114. _, _ = write_map(strings.to_writer(&b), m)
  115. return strings.to_string(b)
  116. }
  117. delete_map :: proc(m: Map) {
  118. allocator := m.allocator
  119. for section, pairs in m {
  120. for key, value in pairs {
  121. delete(key, allocator)
  122. delete(value, allocator)
  123. }
  124. delete(section)
  125. delete(pairs)
  126. }
  127. delete(m)
  128. }
  129. write_section :: proc(w: io.Writer, name: string, n_written: ^int = nil) -> (n: int, err: io.Error) {
  130. defer if n_written != nil { n_written^ += n }
  131. io.write_byte (w, '[', &n) or_return
  132. io.write_string(w, name, &n) or_return
  133. io.write_byte (w, ']', &n) or_return
  134. io.write_byte (w, '\n', &n) or_return
  135. return
  136. }
  137. write_pair :: proc(w: io.Writer, key: string, value: $T, n_written: ^int = nil) -> (n: int, err: io.Error) {
  138. defer if n_written != nil { n_written^ += n }
  139. io.write_string(w, key, &n) or_return
  140. io.write_string(w, " = ", &n) or_return
  141. when intrinsics.type_is_string(T) {
  142. val := string(value)
  143. if len(val) > 0 && (val[0] == ' ' || val[len(val)-1] == ' ') {
  144. io.write_quoted_string(w, val, n_written=&n) or_return
  145. } else {
  146. io.write_string(w, val, &n) or_return
  147. }
  148. } else {
  149. n += fmt.wprint(w, value)
  150. }
  151. io.write_byte(w, '\n', &n) or_return
  152. return
  153. }
  154. write_map :: proc(w: io.Writer, m: Map) -> (n: int, err: io.Error) {
  155. section_index := 0
  156. for section, pairs in m {
  157. if section_index == 0 && section == "" {
  158. // ignore section
  159. } else {
  160. write_section(w, section, &n) or_return
  161. }
  162. for key, value in pairs {
  163. write_pair(w, key, value, &n) or_return
  164. }
  165. section_index += 1
  166. }
  167. return
  168. }