REBOL [ Title: "Import datatypes." Author: "Brett Handley" Date: 3-Aug-2000 File: %import-rebol-datatypes.r Purpose: "Imports some Rebol datatypes into objects with fields." Rights: {Copyright © Brett Handley 2001 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.} ] unprotect [ import-url import-file ] import-file: function [ "Takes a rebol file type and returns parts of it as fields of an object." file-to-import [file!] ] [ file-object parsed-file filename-starts extension-starts ] [ file-object: make object! [ path: none name: none extension: none full: func[][rejoin [path name extension]] ] parsed-file: parse file-to-import "/" ; Find where the filename starts. filename-starts: find/last/tail file-to-import "/" if not filename-starts [filename-starts: head file-to-import] ; Find where the extension starts. extension-starts: find filename-starts "." if not extension-starts [extension-starts: tail file-to-import] ; The path must be before the filename. file-object/path: copy/part file-to-import filename-starts ; The filename before the extension. file-object/name: copy/part filename-starts extension-starts ; Finally the extension. file-object/extension: copy extension-starts ; Ta da. file-object ] ; ; I became aware of the url parsing functionality in net-utils after I had created this. ; I'm leaving this function as is because I have code that depends on the behaviour of this function. ; import-url: function [ "Takes an url and returns parts of the url seperated as fields of an object." url [url!] /httpclient "Sets empty path to / and adds extra fields" ] [url-object url-scheme url-domain url-path url-target part-position] [ url-domain: "" url-path: "" url-target: "" url-scheme: to-string url part-position: find url-scheme "://" either part-position [ part-position: skip part-position 3 url-domain: copy part-position clear part-position part-position: find url-domain "/" either part-position [ url-path: copy part-position clear part-position ][ if httpclient [url-path: %/] ] ][ part-position: head url-scheme url-path: copy part-position clear part-position ] if httpclient [ part-position: find url-path "#" if part-position [ url-target: copy part-position clear part-position ] ] either httpclient [ url-object: make object! [ scheme: none domain: none path: none target: none full: func[][rejoin [scheme domain path target]] ] url-object/target: url-target ][ url-object: make object! [ scheme: none domain: none path: none full: func[][rejoin [scheme domain path]] ] ] url-object/scheme: to-url url-scheme url-object/domain: to-url url-domain url-object/path: to-file url-path url-object ] protect [ import-url import-file ]