add some-system-path<->string functions to scheme/path

svn: r13065
This commit is contained in:
Matthew Flatt 2009-01-11 14:47:51 +00:00
parent 4180d67e34
commit 2530e04720
3 changed files with 55 additions and 3 deletions

View File

@ -6,7 +6,9 @@
normalize-path normalize-path
filename-extension filename-extension
file-name-from-path file-name-from-path
path-only) path-only
some-system-path->string
string->some-system-path)
(define (simple-form-path p) (define (simple-form-path p)
(unless (path-string? p) (unless (path-string? p)
@ -168,3 +170,18 @@
[name (and name (path->bytes name))]) [name (and name (path->bytes name))])
(cond [(and name (regexp-match #rx#"[.]([^.]+)$" name)) => cadr] (cond [(and name (regexp-match #rx#"[.]([^.]+)$" name)) => cadr]
[else #f]))) [else #f])))
(define (some-system-path->string path)
(unless (path-for-some-system? path)
(raise-type-error 'some-system-path->string "path (for any platform)" path))
(bytes->string/utf-8 (path->bytes path)))
(define (string->some-system-path path kind)
(unless (string? path)
(raise-type-error 'string->some-system-path "string" path))
(unless (or (eq? kind 'unix)
(eq? kind 'windows))
(raise-type-error 'string->some-system-path "'unix or 'windows" kind))
(bytes->path (string->bytes/utf-8 path) kind))

View File

@ -65,7 +65,9 @@ Beware that the current locale might not encode every string, in which
case @scheme[string->path] can produce the same path for different case @scheme[string->path] can produce the same path for different
@scheme[str]s. See also @scheme[string->path-element], which should be @scheme[str]s. See also @scheme[string->path-element], which should be
used instead of @scheme[string->path] when a string represents a used instead of @scheme[string->path] when a string represents a
single path element.} single path element.
See also @scheme[string->some-system-path].}
@defproc[(bytes->path [bstr bytes?] @defproc[(bytes->path [bstr bytes?]
[type (or/c 'unix 'windows) (system-path-convention-type)]) [type (or/c 'unix 'windows) (system-path-convention-type)])
@ -97,7 +99,9 @@ Furthermore, for display and sorting based on individual path elements
(such as pathless file names), use @scheme[path-element->string], (such as pathless file names), use @scheme[path-element->string],
instead, to avoid special encodings use to represent some relative instead, to avoid special encodings use to represent some relative
paths. See @secref["windowspaths"] for specific information about paths. See @secref["windowspaths"] for specific information about
the conversion of Windows paths.} the conversion of Windows paths.
See also @scheme[some-system-path->string].}
@defproc[(path->bytes [path path?]) bytes?]{ @defproc[(path->bytes [path path?]) bytes?]{
@ -560,6 +564,27 @@ Returns @scheme[(simplify-path (path->complete-path path))], which
ensures that the result is a complete path containing no up- or ensures that the result is a complete path containing no up- or
same-directory indicators.} same-directory indicators.}
@defproc[(some-system-path->string [path path-for-some-system?])
string?]{
Converts @scheme[path] to a string using a UTF-8 encoding of the
path's bytes.
Use this function when working with paths for a different system
(whose encoding of pathnames might be unrelated to the current
locale's encoding) and when starting and ending with strings.}
@defproc[(string->some-system-path [str string?]
[kind (or/c 'unix 'windows)])
path-for-some-system?]{
Converts @scheme[str] to a @scheme[kind] path using a UTF-8 encoding
of the path's bytes.
Use this function when working with paths for a different system
(whose encoding of pathnames might be unrelated to the current
locale's encoding) and when starting and ending with strings.}
@;------------------------------------------------------------------------ @;------------------------------------------------------------------------
@include-section["unix-paths.scrbl"] @include-section["unix-paths.scrbl"]
@include-section["windows-paths.scrbl"] @include-section["windows-paths.scrbl"]

View File

@ -66,4 +66,14 @@
;; ---------------------------------------- ;; ----------------------------------------
(test "a" some-system-path->string (string->path "a"))
(test "a" some-system-path->string (bytes->path #"a" 'unix))
(test "a" some-system-path->string (bytes->path #"a" 'windows))
(test #t path-for-some-system? (string->some-system-path "a" 'unix))
(test #t path-for-some-system? (string->some-system-path "a" 'windows))
(test "a" some-system-path->string (string->some-system-path "a" 'unix))
(test "a" some-system-path->string (string->some-system-path "a" 'windows))
;; ----------------------------------------
(report-errs) (report-errs)