utils.lisp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: UTILS; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:utils)
  4. (defparameter *break-on-warn-user* nil "set to T if you'd like to stop in warn user")
  5. ;;; COMPLETELY UNPORTABLE!!!
  6. (defun warn-user (format-string &rest args)
  7. "print a warning error to *ERROR-OUTPUT* and continue"
  8. (when *break-on-warn-user* (break "Breaking in WARN-USER"))
  9. (format *error-output* "~&********************************************************************************~%")
  10. (format *error-output* "~&WARNING in ~a:: " (ccl::%last-fn-on-stack 1))
  11. (apply #'format *error-output* format-string args)
  12. (format *error-output* "~&**********************************************************************************~%"))
  13. (defparameter *max-raw-bytes-print-len* 10 "Max number of octets to print from an array")
  14. (defun printable-array (array &optional (max-len *max-raw-bytes-print-len*))
  15. "Given an array, return a string of the first *MAX-RAW-BYTES-PRINT-LEN* bytes"
  16. (let* ((len (length array))
  17. (print-len (min len max-len))
  18. (printable-array (make-array print-len :displaced-to array)))
  19. (format nil "[~:d of ~:d bytes] <~x>" print-len len printable-array)))
  20. (defun upto-null (string)
  21. "Trim STRING to end at first NULL found"
  22. (subseq string 0 (position #\Null string)))
  23. (defun dump-data (file-name data)
  24. (with-open-file (f file-name :direction :output :if-exists :supersede :element-type '(unsigned-byte 8))
  25. (write-sequence data f)))
  26. (defmacro redirect (filename &rest body)
  27. "Temporarily set *STANDARD-OUTPUT* to FILENAME and execute BODY."
  28. `(let ((*standard-output* (open ,filename :direction :output :if-does-not-exist :create :if-exists :supersede)))
  29. ,@body
  30. (finish-output *standard-output*)))
  31. (defun get-bitmask(start width)
  32. "Create a bit mask that begins at bit START (31 is MSB) and is WIDTH bits wide.
  33. Example: (get-bitmask 31 11) -->> #xffe00000"
  34. (ash (- (ash 1 width) 1) (- (1+ start) width)))
  35. (defmacro get-bitfield (int start width)
  36. "Extract WIDTH bits from INT starting at START
  37. Example: (get-bitfield #xFFFBB240 31 11) -->> #x7ff.
  38. The above will expand to (ash (logand #xFFFBB240 #xFFE00000) -21) at COMPILE time."
  39. `(ash (logand ,int ,(utils::get-bitmask start width)) ,(- ( - start width -1))))
  40. ;;;;;;;;;;;;;;;;;;;; convenience macros ;;;;;;;;;;;;;;;;;;;;
  41. (defmacro with-gensyms (syms &body body)
  42. `(let ,(mapcar #'(lambda (s)
  43. `(,s (gensym)))
  44. syms)
  45. ,@body))
  46. (defun make-keyword (name)
  47. (intern (string name) :keyword))
  48. (defmacro while (test &body body)
  49. `(do ()
  50. ((not ,test))
  51. ,@body))
  52. (defmacro aif (test-form then-form &optional else-form)
  53. `(let ((it ,test-form))
  54. (if it ,then-form ,else-form)))
  55. (defmacro awhen (test-form &body body)
  56. `(aif ,test-form
  57. (progn ,@body)))
  58. (defmacro fastest (&body body)
  59. `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
  60. ,@body))