texternal-format.lisp - clic - Clic is an command line interactive client for gopher written in Common LISP HTML git clone git://bitreich.org/clic/ git://hg6vgqziawt5s4dj.onion/clic/ DIR Log DIR Files DIR Refs DIR Tags DIR LICENSE --- texternal-format.lisp (3688B) --- 1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*- 2 ;;; 3 ;;; external-format.lisp --- External format classes and functions. 4 ;;; 5 ;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net> 6 ;;; 7 ;;; Permission is hereby granted, free of charge, to any person 8 ;;; obtaining a copy of this software and associated documentation 9 ;;; files (the "Software"), to deal in the Software without 10 ;;; restriction, including without limitation the rights to use, copy, 11 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies 12 ;;; of the Software, and to permit persons to whom the Software is 13 ;;; furnished to do so, subject to the following conditions: 14 ;;; 15 ;;; The above copyright notice and this permission notice shall be 16 ;;; included in all copies or substantial portions of the Software. 17 ;;; 18 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 ;;; DEALINGS IN THE SOFTWARE. 26 27 (in-package #:babel) 28 29 (defvar *default-eol-style* 30 #+windows :crlf 31 #-windows :lf 32 "The end-of-line style used by external formats if none is 33 explicitly given. Depends on the OS the code is compiled on.") 34 35 (deftype eol-style () 36 "Possible end-of-line styles." 37 '(member :cr :lf :crlf)) 38 39 (defclass external-format () 40 ((encoding :initarg :encoding :reader external-format-encoding 41 :type character-encoding) 42 (eol-style :initarg :eol-style :reader external-format-eol-style 43 :type eol-style :initform *default-eol-style*)) 44 (:documentation 45 "An EXTERNAL-FORMAT consists in a combination of a Babel 46 CHARACTER-ENCODING and an end-of-line style.")) 47 48 (defmethod print-object ((ef external-format) stream) 49 (print-unreadable-object (ef stream :type t :identity t) 50 (format stream "~A ~A" 51 (enc-name (external-format-encoding ef)) 52 (external-format-eol-style ef)))) 53 54 ;;; This interface is still somewhat sketchy. The rest of Babel 55 ;;; doesn't really understand external formats, for instance. 56 (defun make-external-format (encoding &key (eol-style *default-eol-style*)) 57 (check-type eol-style eol-style) 58 (make-instance 'external-format 59 :encoding (get-character-encoding encoding) 60 :eol-style eol-style)) 61 62 (defun ensure-external-format (thing) 63 (etypecase thing 64 (external-format thing) 65 (character-encoding (make-instance 'external-format :encoding thing)) 66 (symbol (make-external-format thing)) 67 (list (apply #'make-external-format thing)))) 68 69 (defun external-format-equal (ef1 ef2) 70 (and (eq (external-format-encoding ef1) (external-format-encoding ef2)) 71 (eq (external-format-eol-style ef1) (external-format-eol-style ef2)))) 72 73 (declaim (inline lookup-mapping)) 74 (defun lookup-mapping (ht encoding) 75 "HT should be an hashtable created by 76 INSTANTIATE-CONCRETE-MAPPINGS. ENCODING should be either an 77 external format, an encoding object or a keyword symbol 78 denoting a character encoding name or one of its aliases." 79 (or (etypecase encoding 80 (keyword 81 (gethash encoding ht)) 82 (babel-encodings::concrete-mapping 83 encoding) 84 (character-encoding 85 (gethash (enc-name encoding) ht)) 86 (external-format 87 (gethash (enc-name (external-format-encoding encoding)) ht))) 88 (error "~S is not a valid encoding designator" encoding)))