path.odin 11 KB

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