path.odin 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // The path/filepath package uses either forward slashes or backslashes depending on the operating system
  2. // To process paths usch as URLs that depend on forward slashes regardless of the OS, use the path package
  3. package filepath
  4. import "core:strings"
  5. // is_separator checks whether the byte is a valid separator character
  6. is_separator :: proc(c: byte) -> bool {
  7. switch c {
  8. case '/': return true
  9. case '\\': return ODIN_OS == "windows"
  10. }
  11. return false
  12. }
  13. @(private)
  14. is_slash :: proc(c: byte) -> bool {
  15. return c == '\\' || c == '/'
  16. }
  17. split :: proc(path: string) -> (dir, file: string) {
  18. vol := volume_name(path)
  19. i := len(path) - 1
  20. for i >= len(vol) && !is_separator(path[i]) {
  21. i -= 1
  22. }
  23. return path[:i+1], path[i+1:]
  24. }
  25. volume_name :: proc(path: string) -> string {
  26. return path[:volume_name_len(path)]
  27. }
  28. volume_name_len :: proc(path: string) -> int {
  29. if ODIN_OS == "windows" {
  30. if len(path) < 2 {
  31. return 0
  32. }
  33. c := path[0]
  34. if path[1] == ':' {
  35. switch c {
  36. case 'a'..='z', 'A'..='Z':
  37. return 2
  38. }
  39. }
  40. // URL: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
  41. if l := len(path); l >= 5 && is_slash(path[0]) && is_slash(path[1]) &&
  42. !is_slash(path[2]) && path[2] != '.' {
  43. for n := 3; n < l-1; n += 1 {
  44. if is_slash(path[n]) {
  45. n += 1
  46. if !is_slash(path[n]) {
  47. if path[n] == '.' {
  48. break
  49. }
  50. }
  51. for ; n < l; n += 1 {
  52. if is_slash(path[n]) {
  53. break
  54. }
  55. }
  56. return n
  57. }
  58. break
  59. }
  60. }
  61. }
  62. return 0
  63. }
  64. base :: proc(path: string) -> string {
  65. if path == "" {
  66. return "."
  67. }
  68. path := path
  69. for len(path) > 0 && is_separator(path[len(path)-1]) {
  70. path = path[:len(path)-1]
  71. }
  72. path = path[volume_name_len(path):]
  73. i := len(path)-1
  74. for i >= 0 && !is_separator(path[i]) {
  75. i -= 1
  76. }
  77. if i >= 0 {
  78. path = path[i+1:]
  79. }
  80. if path == "" {
  81. return SEPARATOR_STRING
  82. }
  83. return path
  84. }
  85. clean :: proc(path: string, allocator := context.allocator) -> string {
  86. context.allocator = allocator
  87. path := path
  88. original_path := path
  89. vol_len := volume_name_len(path)
  90. path = path[vol_len:]
  91. if path == "" {
  92. if vol_len > 1 && original_path[1] != ':' {
  93. s, ok := from_slash(original_path)
  94. if !ok {
  95. s = strings.clone(s)
  96. }
  97. return s
  98. }
  99. return strings.concatenate({original_path, "."})
  100. }
  101. rooted := is_separator(path[0])
  102. n := len(path)
  103. out := &Lazy_Buffer{
  104. s = path,
  105. vol_and_path = original_path,
  106. vol_len = vol_len,
  107. }
  108. r, dot_dot := 0, 0
  109. if rooted {
  110. lazy_buffer_append(out, SEPARATOR)
  111. r, dot_dot = 1, 1
  112. }
  113. for r < n {
  114. switch {
  115. case is_separator(path[r]):
  116. r += 1
  117. case path[r] == '.' && (r+1 == n || is_separator(path[r+1])):
  118. r += 1
  119. case path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_separator(path[r+2])):
  120. r += 2
  121. switch {
  122. case out.w > dot_dot:
  123. out.w -= 1
  124. for out.w > dot_dot && !is_separator(lazy_buffer_index(out, out.w)) {
  125. out.w -= 1
  126. }
  127. case !rooted:
  128. if out.w > 0 {
  129. lazy_buffer_append(out, SEPARATOR)
  130. }
  131. lazy_buffer_append(out, '.')
  132. lazy_buffer_append(out, '.')
  133. dot_dot = out.w
  134. }
  135. case:
  136. if rooted && out.w != 1 || !rooted && out.w != 0 {
  137. lazy_buffer_append(out, SEPARATOR)
  138. }
  139. for ; r < n && !is_separator(path[r]); r += 1 {
  140. lazy_buffer_append(out, path[r])
  141. }
  142. }
  143. }
  144. if out.w == 0 {
  145. lazy_buffer_append(out, '.')
  146. }
  147. s := lazy_buffer_string(out)
  148. cleaned, new_allocation := from_slash(s)
  149. if new_allocation {
  150. delete(s)
  151. lazy_buffer_destroy(out)
  152. }
  153. return cleaned
  154. }
  155. from_slash :: proc(path: string, allocator := context.allocator) -> (new_path: string, new_allocation: bool) {
  156. if SEPARATOR == '/' {
  157. return path, false
  158. }
  159. return strings.replace_all(path, "/", SEPARATOR_STRING, allocator)
  160. }
  161. to_slash :: proc(path: string, allocator := context.allocator) -> (new_path: string, new_allocation: bool) {
  162. if SEPARATOR == '/' {
  163. return path, false
  164. }
  165. return strings.replace_all(path, SEPARATOR_STRING, "/", allocator)
  166. }
  167. ext :: proc(path: string) -> string {
  168. for i := len(path)-1; i >= 0 && !is_separator(path[i]); i -= 1 {
  169. if path[i] == '.' {
  170. return path[i:]
  171. }
  172. }
  173. return ""
  174. }
  175. Relative_Error :: enum {
  176. None,
  177. Cannot_Relate,
  178. }
  179. rel :: proc(base_path, target_path: string, allocator := context.allocator) -> (string, Relative_Error) {
  180. context.allocator = allocator
  181. base_clean, target_clean := clean(base_path), clean(target_path)
  182. delete_target := true
  183. defer {
  184. if delete_target {
  185. delete(target_clean)
  186. }
  187. delete(base_clean)
  188. }
  189. if strings.equal_fold(target_clean, base_clean) {
  190. return strings.clone("."), .None
  191. }
  192. base_vol, target_vol := volume_name(base_path), volume_name(target_path)
  193. base := base_clean[len(base_vol):]
  194. target := target_clean[len(target_vol):]
  195. if base == "." {
  196. base = ""
  197. }
  198. base_slashed := len(base) > 0 && base[0] == SEPARATOR
  199. target_slashed := len(target) > 0 && target[0] == SEPARATOR
  200. if base_slashed != target_slashed || !strings.equal_fold(base_vol, target_vol) {
  201. return "", .Cannot_Relate
  202. }
  203. bl, tl := len(base), len(target)
  204. b0, bi, t0, ti: int
  205. for {
  206. for bi < bl && base[bi] != SEPARATOR {
  207. bi += 1
  208. }
  209. for ti < tl && target[ti] != SEPARATOR {
  210. ti += 1
  211. }
  212. if !strings.equal_fold(target[t0:ti], base[b0:bi]) {
  213. break
  214. }
  215. if bi < bl {
  216. bi += 1
  217. }
  218. if ti < tl {
  219. ti += 1
  220. }
  221. b0, t0 = bi, ti
  222. }
  223. if base[b0:bi] == ".." {
  224. return "", .Cannot_Relate
  225. }
  226. if b0 != bl {
  227. seps := strings.count(base[b0:bl], SEPARATOR_STRING)
  228. size := 2 + seps*3
  229. if tl != t0 {
  230. size += 1 + tl - t0
  231. }
  232. buf := make([]byte, size)
  233. n := copy(buf, "..")
  234. for in 0..<seps {
  235. buf[n] = SEPARATOR
  236. copy(buf[n+1:], "..")
  237. n += 3
  238. }
  239. if t0 != tl {
  240. buf[n] = SEPARATOR
  241. copy(buf[n+1:], target[t0:])
  242. }
  243. return string(buf), .None
  244. }
  245. delete_target = false
  246. return target[t0:], .None
  247. }
  248. dir :: proc(path: string, allocator := context.allocator) -> string {
  249. vol := volume_name(path)
  250. i := len(path) - 1
  251. for i >= len(vol) && !is_separator(path[i]) {
  252. i -= 1
  253. }
  254. dir := clean(path[len(vol) : i+1], allocator)
  255. defer delete(dir, allocator)
  256. if dir == "." && len(vol) > 2 {
  257. return strings.clone(vol)
  258. }
  259. return strings.concatenate({vol, dir})
  260. }
  261. split_list :: proc(path: string, allocator := context.allocator) -> []string {
  262. if path == "" {
  263. return nil
  264. }
  265. start: int
  266. quote: bool
  267. start, quote = 0, false
  268. count := 0
  269. for i := 0; i < len(path); i += 1 {
  270. c := path[i]
  271. switch {
  272. case c == '"':
  273. quote = !quote
  274. case c == LIST_SEPARATOR && !quote:
  275. count += 1
  276. }
  277. }
  278. start, quote = 0, false
  279. list := make([]string, count, allocator)
  280. index := 0
  281. for i := 0; i < len(path); i += 1 {
  282. c := path[i]
  283. switch {
  284. case c == '"':
  285. quote = !quote
  286. case c == LIST_SEPARATOR && !quote:
  287. list[index] = path[start:i]
  288. index += 1
  289. start = i + 1
  290. }
  291. }
  292. assert(index == count)
  293. for s0, i in list {
  294. s, new := strings.replace_all(s0, `"`, ``, allocator)
  295. if !new {
  296. s = strings.clone(s, allocator)
  297. }
  298. list[i] = s
  299. }
  300. return list
  301. }
  302. /*
  303. Lazy_Buffer is a lazily made path buffer
  304. When it does allocate, it uses the context.allocator
  305. */
  306. @(private)
  307. Lazy_Buffer :: struct {
  308. s: string,
  309. b: []byte,
  310. w: int, // write index
  311. vol_and_path: string,
  312. vol_len: int,
  313. }
  314. @(private)
  315. lazy_buffer_index :: proc(lb: ^Lazy_Buffer, i: int) -> byte {
  316. if lb.b != nil {
  317. return lb.b[i]
  318. }
  319. return lb.s[i]
  320. }
  321. @(private)
  322. lazy_buffer_append :: proc(lb: ^Lazy_Buffer, c: byte) {
  323. if lb.b == nil {
  324. if lb.w < len(lb.s) && lb.s[lb.w] == c {
  325. lb.w += 1
  326. return
  327. }
  328. lb.b = make([]byte, len(lb.s))
  329. copy(lb.b, lb.s[:lb.w])
  330. }
  331. lb.b[lb.w] = c
  332. lb.w += 1
  333. }
  334. @(private)
  335. lazy_buffer_string :: proc(lb: ^Lazy_Buffer) -> string {
  336. if lb.b == nil {
  337. return strings.clone(lb.vol_and_path[:lb.vol_len+lb.w])
  338. }
  339. x := lb.vol_and_path[:lb.vol_len]
  340. y := string(lb.b[:lb.w])
  341. z := make([]byte, len(x)+len(y))
  342. copy(z, x)
  343. copy(z[len(x):], y)
  344. return string(z)
  345. }
  346. @(private)
  347. lazy_buffer_destroy :: proc(lb: ^Lazy_Buffer) {
  348. delete(lb.b)
  349. lb^ = {}
  350. }