You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.3 KiB
Plaintext
75 lines
2.3 KiB
Plaintext
10 years ago
|
#lang scribble/manual
|
||
|
|
||
|
@(require scribble/eval (for-label racket sugar))
|
||
|
|
||
|
@(define my-eval (make-base-eval))
|
||
|
@(my-eval `(require sugar))
|
||
|
|
||
9 years ago
|
@title{File extensions}
|
||
9 years ago
|
@defmodule[#:multi (sugar/file (submod sugar/file safe))]
|
||
10 years ago
|
|
||
9 years ago
|
These functions don't access the filesystem.
|
||
10 years ago
|
|
||
|
Arguments that are @racket[pathish?] can take either a string or a path. For clarity below, I've used strings.
|
||
|
|
||
|
@defproc[
|
||
|
(get-ext
|
||
|
[file-path pathish?])
|
||
|
(or/c #f string?)]
|
||
|
Return the last file extension of @racket[_file-path] as a string, or @racket[#f] if it has no extension. Omit the intervening @litchar{.} separator.
|
||
|
|
||
|
@examples[#:eval my-eval
|
||
|
(get-ext "foo.txt")
|
||
|
(get-ext "/path/to/foo.txt")
|
||
|
(get-ext "/path/to/foo.txt.bar")
|
||
|
(get-ext "/path/to/file-without-extension")
|
||
|
(get-ext "/path/to/directory/")]
|
||
|
|
||
|
@defproc[
|
||
|
(has-ext?
|
||
|
[file-path pathish?]
|
||
|
[ext stringish?])
|
||
|
boolean?]
|
||
|
Return @racket[#t] if the last file extension of @racket[_file-path] is @racket[_ext], otherwise @racket[#f].
|
||
|
|
||
|
@examples[#:eval my-eval
|
||
|
(has-ext? "foo.txt" "txt")
|
||
|
(has-ext? "foo.txt" "jpg")
|
||
|
(has-ext? "foo.jpg.txt" "jpg")]
|
||
|
|
||
|
@defproc[
|
||
|
(remove-ext
|
||
|
[file-path pathish?])
|
||
|
path?]
|
||
|
Remove the last file extension of @racket[_file-path], and return the path that remains. If @racket[_file-path] has no extension, you just get the same @racket[_file-path]. Does not use the filesystem.
|
||
|
|
||
|
@examples[#:eval my-eval
|
||
|
(remove-ext "foo.txt")
|
||
|
(remove-ext "/path/to/foo.txt")
|
||
|
(remove-ext "/path/to/foo.txt.bar")
|
||
|
(remove-ext (remove-ext "/path/to/foo.txt.bar"))]
|
||
|
|
||
|
@defproc[
|
||
|
(remove-ext*
|
||
|
[file-path pathish?])
|
||
|
path?]
|
||
|
Like @racket[remove-ext], just more. Remove all file extensions from @racket[_file-path], and return the path that remains. If @racket[_file-path] has no extensions, you just get the same @racket[_file-path]. Does not use the filesystem.
|
||
|
|
||
|
@examples[#:eval my-eval
|
||
|
(remove-ext* "foo.txt")
|
||
|
(remove-ext* "/path/to/foo.txt")
|
||
|
(remove-ext* "/path/to/foo.txt.bar")
|
||
|
(remove-ext* (remove-ext* "/path/to/foo.txt.bar"))]
|
||
|
|
||
|
@defproc[
|
||
|
(add-ext
|
||
|
[file-path pathish?]
|
||
|
[ext stringish?])
|
||
|
path?]
|
||
|
Return a new @racket[_file-path] with @racket[_ext] appended. Note that this does not replace an existing file extension. If that's what you want, then do @racket[(add-ext (remove-ext _file-path) _ext)].
|
||
|
|
||
|
@examples[#:eval my-eval
|
||
|
(add-ext "foo" "txt")
|
||
|
(add-ext "foo.txt" "jpg")
|
||
|
(add-ext (remove-ext "foo.txt") "jpg")]
|