path.odin 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. // The path/filepath package uses either forward slashes or backslashes depending on the operating system
  2. // To process paths such as URLs that depend on forward slashes regardless of the OS, use the path package
  3. package filepath
  4. import "base:runtime"
  5. import "core:strings"
  6. SEPARATOR_CHARS :: `/\`
  7. // is_separator checks whether the byte is a valid separator character
  8. is_separator :: proc(c: byte) -> bool {
  9. switch c {
  10. case '/': return true
  11. case '\\': return ODIN_OS == .Windows
  12. }
  13. return false
  14. }
  15. @(private)
  16. is_slash :: proc(c: byte) -> bool {
  17. return c == '\\' || c == '/'
  18. }
  19. // Splits path immediate following the last separator; separating the path into a directory and file.
  20. // If no separator is found, `dir` will be empty and `path` set to `path`.
  21. split :: proc(path: string) -> (dir, file: string) {
  22. vol := volume_name(path)
  23. i := len(path) - 1
  24. for i >= len(vol) && !is_separator(path[i]) {
  25. i -= 1
  26. }
  27. return path[:i+1], path[i+1:]
  28. }
  29. /*
  30. Returns leading volume name.
  31. e.g.
  32. "C:\foo\bar\baz" will return "C:" on Windows.
  33. Everything else will be "".
  34. */
  35. volume_name :: proc(path: string) -> string {
  36. return path[:volume_name_len(path)]
  37. }
  38. // Returns the length of the volume name in bytes.
  39. volume_name_len :: proc(path: string) -> int {
  40. if ODIN_OS == .Windows {
  41. if len(path) < 2 {
  42. return 0
  43. }
  44. c := path[0]
  45. if path[1] == ':' {
  46. switch c {
  47. case 'a'..='z', 'A'..='Z':
  48. return 2
  49. }
  50. }
  51. // URL: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
  52. if l := len(path); l >= 5 && is_slash(path[0]) && is_slash(path[1]) &&
  53. !is_slash(path[2]) && path[2] != '.' {
  54. for n := 3; n < l-1; n += 1 {
  55. if is_slash(path[n]) {
  56. n += 1
  57. if !is_slash(path[n]) {
  58. if path[n] == '.' {
  59. break
  60. }
  61. }
  62. for ; n < l; n += 1 {
  63. if is_slash(path[n]) {
  64. break
  65. }
  66. }
  67. return n
  68. }
  69. break
  70. }
  71. }
  72. }
  73. return 0
  74. }
  75. /*
  76. Gets the file name and extension from a path.
  77. e.g.
  78. 'path/to/name.tar.gz' -> 'name.tar.gz'
  79. 'path/to/name.txt' -> 'name.txt'
  80. 'path/to/name' -> 'name'
  81. Returns "." if the path is an empty string.
  82. */
  83. base :: proc(path: string) -> string {
  84. if path == "" {
  85. return "."
  86. }
  87. path := path
  88. for len(path) > 0 && is_separator(path[len(path)-1]) {
  89. path = path[:len(path)-1]
  90. }
  91. path = path[volume_name_len(path):]
  92. i := len(path)-1
  93. for i >= 0 && !is_separator(path[i]) {
  94. i -= 1
  95. }
  96. if i >= 0 {
  97. path = path[i+1:]
  98. }
  99. if path == "" {
  100. return SEPARATOR_STRING
  101. }
  102. return path
  103. }
  104. /*
  105. Gets the name of a file from a path.
  106. The stem of a file is such that stem(path) + ext(path) = base(path).
  107. Only the last dot is considered when splitting the file extension.
  108. See `short_stem`.
  109. e.g.
  110. 'name.tar.gz' -> 'name.tar'
  111. 'name.txt' -> 'name'
  112. Returns an empty string if there is no stem. e.g: '.gitignore'.
  113. Returns an empty string if there's a trailing path separator.
  114. */
  115. stem :: proc(path: string) -> string {
  116. if len(path) > 0 && is_separator(path[len(path) - 1]) {
  117. // NOTE(tetra): Trailing separator
  118. return ""
  119. }
  120. // NOTE(tetra): Get the basename
  121. path := path
  122. if i := strings.last_index_any(path, SEPARATOR_CHARS); i != -1 {
  123. path = path[i+1:]
  124. }
  125. if i := strings.last_index_byte(path, '.'); i != -1 {
  126. return path[:i]
  127. }
  128. return path
  129. }
  130. /*
  131. Gets the name of a file from a path.
  132. The short stem is such that short_stem(path) + long_ext(path) = base(path).
  133. The first dot is used to split off the file extension, unlike `stem` which uses the last dot.
  134. e.g.
  135. 'name.tar.gz' -> 'name'
  136. 'name.txt' -> 'name'
  137. Returns an empty string if there is no stem. e.g: '.gitignore'.
  138. Returns an empty string if there's a trailing path separator.
  139. */
  140. short_stem :: proc(path: string) -> string {
  141. s := stem(path)
  142. if i := strings.index_byte(s, '.'); i != -1 {
  143. return s[:i]
  144. }
  145. return s
  146. }
  147. /*
  148. Gets the file extension from a path, including the dot.
  149. The file extension is such that stem(path) + ext(path) = base(path).
  150. Only the last dot is considered when splitting the file extension.
  151. See `long_ext`.
  152. e.g.
  153. 'name.tar.gz' -> '.gz'
  154. 'name.txt' -> '.txt'
  155. Returns an empty string if there is no dot.
  156. Returns an empty string if there is a trailing path separator.
  157. */
  158. ext :: proc(path: string) -> string {
  159. for i := len(path)-1; i >= 0 && !is_separator(path[i]); i -= 1 {
  160. if path[i] == '.' {
  161. return path[i:]
  162. }
  163. }
  164. return ""
  165. }
  166. /*
  167. Gets the file extension from a path, including the dot.
  168. The long file extension is such that short_stem(path) + long_ext(path) = base(path).
  169. The first dot is used to split off the file extension, unlike `ext` which uses the last dot.
  170. e.g.
  171. 'name.tar.gz' -> '.tar.gz'
  172. 'name.txt' -> '.txt'
  173. Returns an empty string if there is no dot.
  174. Returns an empty string if there is a trailing path separator.
  175. */
  176. long_ext :: proc(path: string) -> string {
  177. if len(path) > 0 && is_separator(path[len(path) - 1]) {
  178. // NOTE(tetra): Trailing separator
  179. return ""
  180. }
  181. // NOTE(tetra): Get the basename
  182. path := path
  183. if i := strings.last_index_any(path, SEPARATOR_CHARS); i != -1 {
  184. path = path[i+1:]
  185. }
  186. if i := strings.index_byte(path, '.'); i != -1 {
  187. return path[i:]
  188. }
  189. return ""
  190. }
  191. /*
  192. Returns the shortest path name equivalent to `path` through solely lexical processing.
  193. It applies the folliwng rules until none of them can be applied:
  194. * Replace multiple separators with a single one
  195. * Remove each current directory (`.`) path name element
  196. * Remove each inner parent directory (`..`) path and the preceding paths
  197. * Remove `..` that begin at the root of a path
  198. * All possible separators are replaced with the OS specific separator
  199. The return path ends in a slash only if it represents the root of a directory (`C:\` on Windows and `/` on *nix systems).
  200. If the result of the path is an empty string, the returned path with be `"."`.
  201. */
  202. clean :: proc(path: string, allocator := context.allocator) -> (cleaned: string, err: runtime.Allocator_Error) #optional_allocator_error {
  203. context.allocator = allocator
  204. path := path
  205. original_path := path
  206. vol_len := volume_name_len(path)
  207. path = path[vol_len:]
  208. if path == "" {
  209. if vol_len > 1 && original_path[1] != ':' {
  210. s, ok := from_slash(original_path)
  211. if !ok {
  212. s = strings.clone(s) or_return
  213. }
  214. return s, nil
  215. }
  216. return strings.concatenate({original_path, "."})
  217. }
  218. rooted := is_separator(path[0])
  219. n := len(path)
  220. out := &Lazy_Buffer{
  221. s = path,
  222. vol_and_path = original_path,
  223. vol_len = vol_len,
  224. }
  225. defer lazy_buffer_destroy(out)
  226. r, dot_dot := 0, 0
  227. if rooted {
  228. lazy_buffer_append(out, SEPARATOR) or_return
  229. r, dot_dot = 1, 1
  230. }
  231. for r < n {
  232. switch {
  233. case is_separator(path[r]):
  234. r += 1
  235. case path[r] == '.' && (r+1 == n || is_separator(path[r+1])):
  236. r += 1
  237. case path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_separator(path[r+2])):
  238. r += 2
  239. switch {
  240. case out.w > dot_dot:
  241. out.w -= 1
  242. for out.w > dot_dot && !is_separator(lazy_buffer_index(out, out.w)) {
  243. out.w -= 1
  244. }
  245. case !rooted:
  246. if out.w > 0 {
  247. lazy_buffer_append(out, SEPARATOR) or_return
  248. }
  249. lazy_buffer_append(out, '.') or_return
  250. lazy_buffer_append(out, '.') or_return
  251. dot_dot = out.w
  252. }
  253. case:
  254. if rooted && out.w != 1 || !rooted && out.w != 0 {
  255. lazy_buffer_append(out, SEPARATOR) or_return
  256. }
  257. for ; r < n && !is_separator(path[r]); r += 1 {
  258. lazy_buffer_append(out, path[r]) or_return
  259. }
  260. }
  261. }
  262. if out.w == 0 {
  263. lazy_buffer_append(out, '.') or_return
  264. }
  265. s := lazy_buffer_string(out) or_return
  266. new_allocation: bool
  267. cleaned, new_allocation = from_slash(s)
  268. if new_allocation {
  269. delete(s)
  270. }
  271. return
  272. }
  273. // Returns the result of replacing each forward slash `/` character in the path with the separate OS specific character.
  274. from_slash :: proc(path: string, allocator := context.allocator) -> (new_path: string, new_allocation: bool) {
  275. if SEPARATOR == '/' {
  276. return path, false
  277. }
  278. return strings.replace_all(path, "/", SEPARATOR_STRING, allocator)
  279. }
  280. // Returns the result of replacing each OS specific separator with a forward slash `/` character.
  281. to_slash :: proc(path: string, allocator := context.allocator) -> (new_path: string, new_allocation: bool) {
  282. if SEPARATOR == '/' {
  283. return path, false
  284. }
  285. return strings.replace_all(path, SEPARATOR_STRING, "/", allocator)
  286. }
  287. Relative_Error :: enum {
  288. None,
  289. Cannot_Relate,
  290. }
  291. /*
  292. Returns a relative path that is lexically equivalent to the `target_path` when joined with the `base_path` with an OS specific separator.
  293. e.g. `join(base_path, rel(base_path, target_path))` is equivalent to `target_path`
  294. On failure, the `Relative_Error` will be state it cannot compute the necessary relative path.
  295. */
  296. rel :: proc(base_path, target_path: string, allocator := context.allocator) -> (string, Relative_Error) {
  297. context.allocator = allocator
  298. base_clean := clean(base_path, allocator)
  299. target_clean := clean(target_path, allocator)
  300. defer delete(base_clean, allocator)
  301. defer delete(target_clean, allocator)
  302. if strings.equal_fold(target_clean, base_clean) {
  303. return strings.clone(".", allocator), .None
  304. }
  305. base_vol := volume_name(base_path)
  306. target_vol := volume_name(target_path)
  307. base := base_clean [len(base_vol):]
  308. target := target_clean[len(target_vol):]
  309. if base == "." {
  310. base = ""
  311. }
  312. base_slashed := len(base) > 0 && base [0] == SEPARATOR
  313. target_slashed := len(target) > 0 && target[0] == SEPARATOR
  314. if base_slashed != target_slashed || !strings.equal_fold(base_vol, target_vol) {
  315. return "", .Cannot_Relate
  316. }
  317. bl, tl := len(base), len(target)
  318. b0, bi, t0, ti: int
  319. for {
  320. for bi < bl && base[bi] != SEPARATOR {
  321. bi += 1
  322. }
  323. for ti < tl && target[ti] != SEPARATOR {
  324. ti += 1
  325. }
  326. strings.equal_fold(target[t0:ti], base[b0:bi]) or_break
  327. if bi < bl {
  328. bi += 1
  329. }
  330. if ti < tl {
  331. ti += 1
  332. }
  333. b0, t0 = bi, ti
  334. }
  335. if base[b0:bi] == ".." {
  336. return "", .Cannot_Relate
  337. }
  338. if b0 != bl {
  339. seps := strings.count(base[b0:bl], SEPARATOR_STRING)
  340. size := 2 + seps*3
  341. if tl != t0 {
  342. size += 1 + tl - t0
  343. }
  344. buf := make([]byte, size, allocator)
  345. n := copy(buf, "..")
  346. for _ in 0..<seps {
  347. buf[n] = SEPARATOR
  348. copy(buf[n+1:], "..")
  349. n += 3
  350. }
  351. if t0 != tl {
  352. buf[n] = SEPARATOR
  353. copy(buf[n+1:], target[t0:])
  354. }
  355. return string(buf), .None
  356. }
  357. return strings.clone(target[t0:], allocator), .None
  358. }
  359. /*
  360. Returns all but the last element path, usually the path's directory. Once the final element has been removed,
  361. `dir` calls `clean` on the path and trailing separators are removed. If the path consists purely of separators,
  362. then `"."` is returned.
  363. */
  364. dir :: proc(path: string, allocator := context.allocator) -> string {
  365. context.allocator = allocator
  366. vol := volume_name(path)
  367. i := len(path) - 1
  368. for i >= len(vol) && !is_separator(path[i]) {
  369. i -= 1
  370. }
  371. dir := clean(path[len(vol) : i+1])
  372. defer delete(dir)
  373. if dir == "." && len(vol) > 2 {
  374. return strings.clone(vol)
  375. }
  376. return strings.concatenate({vol, dir})
  377. }
  378. // Splits the PATH-like `path` string, returning an array of its separated components (delete after use).
  379. // For Windows the separator is `;`, for Unix it's `:`.
  380. // An empty string returns nil. A non-empty string with no separators returns a 1-element array.
  381. // Any empty components will be included, e.g. `a::b` will return a 3-element array, as will `::`.
  382. // Separators within pairs of double-quotes will be ignored and stripped, e.g. `"a:b"c:d` will return []{`a:bc`, `d`}.
  383. split_list :: proc(path: string, allocator := context.allocator) -> (list: []string, err: runtime.Allocator_Error) #optional_allocator_error {
  384. if path == "" {
  385. return nil, nil
  386. }
  387. start: int
  388. quote: bool
  389. start, quote = 0, false
  390. count := 0
  391. for i := 0; i < len(path); i += 1 {
  392. c := path[i]
  393. switch {
  394. case c == '"':
  395. quote = !quote
  396. case c == LIST_SEPARATOR && !quote:
  397. count += 1
  398. }
  399. }
  400. start, quote = 0, false
  401. list = make([]string, count + 1, allocator) or_return
  402. index := 0
  403. for i := 0; i < len(path); i += 1 {
  404. c := path[i]
  405. switch {
  406. case c == '"':
  407. quote = !quote
  408. case c == LIST_SEPARATOR && !quote:
  409. list[index] = path[start:i]
  410. index += 1
  411. start = i + 1
  412. }
  413. }
  414. assert(index == count)
  415. list[index] = path[start:]
  416. for s0, i in list {
  417. s, new := strings.replace_all(s0, `"`, ``, allocator)
  418. if !new {
  419. s = strings.clone(s, allocator) or_return
  420. }
  421. list[i] = s
  422. }
  423. return list, nil
  424. }
  425. /*
  426. Lazy_Buffer is a lazily made path buffer
  427. When it does allocate, it uses the context.allocator
  428. */
  429. @(private)
  430. Lazy_Buffer :: struct {
  431. s: string,
  432. b: []byte,
  433. w: int, // write index
  434. vol_and_path: string,
  435. vol_len: int,
  436. }
  437. @(private)
  438. lazy_buffer_index :: proc(lb: ^Lazy_Buffer, i: int) -> byte {
  439. if lb.b != nil {
  440. return lb.b[i]
  441. }
  442. return lb.s[i]
  443. }
  444. @(private)
  445. lazy_buffer_append :: proc(lb: ^Lazy_Buffer, c: byte) -> (err: runtime.Allocator_Error) {
  446. if lb.b == nil {
  447. if lb.w < len(lb.s) && lb.s[lb.w] == c {
  448. lb.w += 1
  449. return
  450. }
  451. lb.b = make([]byte, len(lb.s)) or_return
  452. copy(lb.b, lb.s[:lb.w])
  453. }
  454. lb.b[lb.w] = c
  455. lb.w += 1
  456. return
  457. }
  458. @(private)
  459. lazy_buffer_string :: proc(lb: ^Lazy_Buffer) -> (s: string, err: runtime.Allocator_Error) {
  460. if lb.b == nil {
  461. return strings.clone(lb.vol_and_path[:lb.vol_len+lb.w])
  462. }
  463. x := lb.vol_and_path[:lb.vol_len]
  464. y := string(lb.b[:lb.w])
  465. z := make([]byte, len(x)+len(y)) or_return
  466. copy(z, x)
  467. copy(z[len(x):], y)
  468. return string(z), nil
  469. }
  470. @(private)
  471. lazy_buffer_destroy :: proc(lb: ^Lazy_Buffer) -> runtime.Allocator_Error {
  472. err := delete(lb.b)
  473. lb^ = {}
  474. return err
  475. }