;;; Exercice 1 ;; Voir feuille 10 de PFS [INF353] ;;; Exercice 2 (defclass personne () ((date-naissance :initarg :date-naissance :reader date-naissance))) (decode-universal-time (encode-universal-time 0 0 0 23 02 1991)) (defparameter *p* (make-instance 'personne :date-naissance (encode-universal-time 0 0 0 23 02 1991))) (defclass personne () ((date-naissance :initarg :date-naissance :reader date-naissance) (age :reader age))) (defun compute-age (birth-encoded-time) "age in seconds" (- (get-universal-time) birth-encoded-time)) (defun annees (duree) (floor (/ duree 60 60 24 365.25))) (defmethod initialize-instance :after ((personne personne) &key ) (setf (slot-value personne 'age) (compute-age (date-naissance personne)))) (setf (slot-value *p* 'age) (compute-age (date-naissance *p*))) (defparameter *p2* (make-instance 'personne :date-naissance (encode-universal-time 0 0 0 14 2 1993))) ;; (describe *p2*) ;; 3. (defclass personne () ((age :reader age))) ;; (describe *p2*) (defmethod initialize-instance :after ((personne personne) &rest initargs &key (date-naissance nil) &allow-other-keys) (when date-naissance (format t "after date-naissance ~A initargs ~A~%" date-naissance initargs) (setf (slot-value personne 'age) (compute-age date-naissance)))) (defparameter *p3* (make-instance 'personne :date-naissance (encode-universal-time 0 0 0 02 04 1996))) ;; (describe *p3*) ;;; Exercice 7 (defclass participant () ((nom :reader nom :initarg :nom :type string))) (defclass personnage (participant) ((age :type (unsigned-byte 8)))) (defclass heros (personnage) ((force :type (unsigned-byte 8)))) (defparameter *participant* (make-instance 'participant :nom "titi")) (defparameter *personnage* (make-instance 'personnage :nom "toto")) (defparameter *heros* (make-instance 'heros :nom "the heros")) (defgeneric taille (o)) (defmethod taille ((participant participant)) (format t "taille participant~%") (length (nom participant))) (defmethod taille ((personnage personnage)) (format t "taille personnage~%") (1+ (call-next-method))) (defmethod taille ((heros heros)) (format t "taille heros~%") (1+ (call-next-method))) (defmethod taille ((participant participant)) (format t "taille participant~%") (length (nom participant))) (defmethod taille ((personnage personnage)) (format t "taille personnage~%") (1+ (if (next-method-p) (call-next-method) (length (nom personnage))))) (defmethod taille ((heros heros)) (format t "taille heros~%") (1+ (if (next-method-p) (call-next-method) (length (nom heros))))) (fmakunbound 'taille) (defgeneric taille (o) (:method-combination +)) (defmethod taille + ((participant participant)) (length (nom participant))) (defmethod taille + ((personnage personnage)) 1) (defmethod taille + ((heros heros)) 1) ;;; Exercice 3 (defparameter *filename* "iso-latin-1.text") (defparameter *stream* (open *filename*)) (stream-external-format *stream*) ;; => :UTF-8 (defparameter *stream* (open *filename* :external-format :latin1)) (stream-external-format *stream*) ;; => :LATIN-1 ;;; Exercice 2 (defun isolatin1-to-utf8 (isolatin1 utf8) (let ((input (open isolatin1 :external-format :latin1)) (output (open utf8 :direction :output :external-format :utf-8 :if-exists :overwrite :if-does-not-exist :create))) (loop for line = (read-line input t nil) then (read-line input nil nil) while line do (write-line line output)) (close input) (close output))) ;; ou en utilisant with-open-file (defun isolatin1-to-utf8 (isolatin1 utf8) (with-open-file (input isolatin1 :external-format :latin1) (with-open-file (output utf8 :direction :output :external-format :utf-8 :if-exists :overwrite :if-does-not-exist :create) (loop for line = (read-line input t nil) then (read-line input nil nil) while line do (write-line line output))))) ;;; Exercice 3 (defun copy-binary-file (pathname &optional (target nil)) (with-open-file (input pathname :element-type 'unsigned-byte) (with-open-file (output (or target (concatenate 'string (namestring pathname) ".bak")) :element-type 'unsigned-byte :direction :output :if-exists :overwrite :if-does-not-exist :create) (handler-case (loop for byte = (read-byte input) do (write-byte byte output)) (end-of-file ())))))