Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Standard Macro Library

Name: Anonymous 2012-09-12 21:50

What's in your <stdmacro.h> /prog/?

Name: Anonymous 2012-09-12 23:01

U MENA void.h?

Name: Anonymous 2012-09-12 23:06

I always thought these were a bad idea. There's too much temptation to put everything in one big header and include it everywhere, even when it's not needed - this can slow down compilation by quite a bit.

I always break things up, even when the length of the header file doesn't make it seem like a necessary thing. My bit.h is like 15 lines, for example.  If you've ever had to split up a big header file, you'll appreciate the reason for this.

Name: Anonymous 2012-09-13 0:00

>>3
Just use cat before compiling; make it all one big source file with no repeated header files

no need to coordinate header files like a sucker

Name: Anonymous 2012-09-13 3:57

If you call your own header stdmacro.h then you are invoking undefined behavior by including it.

Name: Anonymous 2012-09-13 4:07


(defun hd (s bs)
  (let ((s (min s (length bs))))
    (dotimes (y (truncate (+ s 15) 16))
      (format t "~4,'0X |" (* y 16))
      (dotimes (x 16)
        (let ((i (+ (* y 16) x)))
          (when (= (rem i 16) 8)  (format t " "))
          (if (< i s)
              (format t " ~2,'0X" (aref bs i))
              (format t "   "))))
      (format t " |")
      (dotimes (x 16)
        (let* ((i (+ (* y 16) x)))
          (format t "~a" (if (and (< i s) (<= #x20  (aref bs i) #x7E))
                             (code-char (aref bs i))
                             #\Space))))
      (format t "~%"))))


(defun string-to-utf8 (string) (sb-ext:string-to-octets string))
(defun utf8-to-string (bytes)
  (sb-ext:octets-to-string
   (coerce bytes '(SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)))))

(defmacro size-in-array (type)
  (let* ((n 100000)
         (o (with-output-to-string (s)
              (let ((*standard-output* s)
                    (*trace-output* s))
                (eval `(time (make-array ,n :element-type ',type))))))
         (o (loop for i = 0 then (1+ j)
                  as j = (position #\Newline o :start i)
                  collect (subseq o i j)
                  while j))
         (o (or #+sbcl (caddr (reverse o))
                #+clozure (cadr (reverse o))
                (error "size-in-array: your platform is not supported")))
         (o (coerce (remove-if (lambda (x) (char= x #\,)) (coerce o 'list))
                    'string))
         (o (truncate (read-from-string o) n)))
    o))


(defun %grow (s l)
  (declare ((SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)) s)
           (fixnum l))
  (let* ((n (length s))
         (d (vec (* 2 (+ n l)) u1)))
    (dotimes (i n) (setf (aref d i) (aref s i)))
    d))

(defmacro %new (&rest as) `(cons (vec ,(if as (car as) 0) u1) 0))
(defmacro %ptr (x) `(cons (the (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)) ,x) 0))
(defun %clone (x) (cons (car x) (cdr x)))
(defun %crop (x)
  (let* ((d (vec (cdr x) u1))
         (s (car x)))
    (dotimes (i (length d))
      (setf (aref d i) (aref s i)))
    d))
(defmacro %arr (x) `(the (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)) (car ,x)))
(defmacro %off (x) `(the fixnum (cdr ,x)))

(defun %put (p s)
  (let* ((d (%arr p))
         (i (%off p))
         (n (+ i (length s))))
    (when (< (length d) n)
      (let ((nd (vec (* 2 n) u1)))
        (acpy nd d)
        (setf d nd)))
    (loop as v across s
       do (progn (setf (aref d i) v)
                 (incf i)))
    (setf (car p) d
          (cdr p) i)
    p))

(defun %get (p n)
  (let* ((s (%arr p))
         (j (%off p))
         (l (- (length s) j))
         (d (vec (min n l) u1)))
    (dotimes (i (length d))
      (setf (aref d i) (aref s j))
      (incf j))
    (setf (cdr p) j)
    d))


(defmacro % (&rest as)
  (let ((n 1)
        (p nil)
        (v (gensym))
        (g (gensym))
        (a (gensym))
        (i (gensym)))
    (when (numberp (first as))
      (setf n  (car as)
            as (cdr as)))
    (setf p  (car as)
          as (cdr as))
    `(let* ((,g ,p)
            (,a (%arr ,g))
            ,@(when as `((,v ,(car as))))
            (,i (the fixnum (cdr ,g))))
       ,@(when as
           `((when (<= (length ,a) ,i)
               (setf ,a (the (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)) (%grow ,a 1)))
               (setf (car ,g) ,a))))
       (prog1 (aref ,a ,i)
         ,@(when as `((setf (aref ,a ,i) (logand #xFF ,v))))
         ,@(unless (eql n 0) `((setf (cdr ,g) (the fixnum (+ ,i ,n)))))))))


(defmacro raw  (n s f)
  (with-gensyms (x)
    `(let ((,x (%get ,s ,n)))
       (if (= (length ,x) ,n)
           ,x
           ,f))))

(defun lsbmsb (n s f &key lsb)
  (with-gensyms (tmp)
    `(let ((,tmp (raw ,n ,s ,f)))
       (logior ,@(mapcar (lambda (i) `(ash (aref ,tmp ,i)
                                           ,(* (if lsb i (- n 1 i)) 8)))
                         (rng 0 (- n 1)))))))

(defmacro lsb  (n s f) (lsbmsb n s f :lsb t))
(defmacro msb  (n s f) (lsbmsb n s f :lsb nil))
(defmacro utf8 (n s f) `(utf8-to-string (raw ,n ,s ,f)))


(defmacro deser (stream fields &body body)
  (let* ((s (gensym))
         (b (gensym))
         (l (gensym))
         (f `(error "~a: failed to deserialize stream" (current-function-name))))
    (when (eql (first body) :on-fail)
      (setf f (second body))
      (setf body (cddr body)))
    (let ((bs (mapcar (lambda (x)
                        (when (numberp (second x))
                          (setf x (cons (car x) (cons 'lsb (cdr x)))))
                        (let* ((h (car x))
                               (b `(,@(cdr x) ,s (funcall ,l)))
                               (g (gensym))
                               (c (gensym)))
                          (when (consp h)
                            (setf b `(let ((,g ,b)
                                           (,c ,(second h)))
                                       (unless (if (arrayp ,c)
                                                   (acmp ,g ,c)
                                                   (equal ,g ,c))
                                         ,f)))
                            (setf h g))
                          `(,h ,b)))
                      fields)))
      `(block ,b
         (let* ((,l (lambda () ,f))
                (,s ,stream)
                (,s (if (arrayp ,s) (%ptr ,s) ,s)) ;in case user passes raw array
                ,@bs)
           (declare (ignorable ,@(mapcar #'first bs)))
           ,@body)))))


(defmacro ser-arr  (s &rest as)
  (let* ((ys (car (last as)))
         (as (butlast as))
         (y (gensym)))
    (if (and (= (length as) 1) (eq (car as) 1))
        `(%put ,s ,ys)
        (progn (when (numberp (car as))
                 (setf as (cons 'lsb as)))
               `(loop as ,y across ,ys
                   do (,(intern (concatenate 'string "SER-" (symbol-name (car as))))
                        ,s
                        ,@(cdr as)
                        ,y))))))

(defmacro ser-dup (s n &rest x)
  (when (numberp (car x))
    (setf x (cons 'lsb x)))
  `(loop as i from 0 below ,n
      do (,(intern (concatenate 'string "SER-" (symbol-name (car x))))
           ,s
           ,@(cdr x))))


(defun ser-lsbmsb (stream n val &key lsb)
  (with-gensyms (s v)
    `(let ((,s ,stream)
           (,v ,val))
       ,@(mapcar (lambda (i) `(% ,s (ash ,v ,(- (* (if lsb i (- n 1 i)) 8)))))
                 (rng 0 (- n 1))))))

(defmacro ser-lsb  (s n v) (ser-lsbmsb s n v :lsb t))
(defmacro ser-msb  (s n v) (ser-lsbmsb s n v :lsb nil))
(defmacro ser-utf8 (s v) `(ser-arr ,s 1 (string-to-utf8 ,v)))


(defmacro ser (stream &body fields)
  (with-gensyms (s)
    `(let* ((,s ,(if (eq stream t) `(%new) stream)))
       ,@(mapcar (lambda (x)
                   (let* (;(n (car x))
                          (x (cdr x)))
                     (when (numberp (car x))
                       (setf x (cons 'lsb x)))
                     `(,(intern (concatenate 'string "SER-" (symbol-name (car x))))
                        ,s
                        ,@(cdr x))))
                 fields)
       ,@(when (eq stream t) `((%crop ,s)))
       )))


;; sequence coerce
(defmacro sc (elem-type seq) `(coerce ,seq '(simple-array ,elem-type (*))))

(defun save-file (path data)
  (with-open-file (stream path
                          :direction :output
                          :element-type '(unsigned-byte 8)
                          :if-exists :supersede)
    (write-sequence data stream))
  nil)

Name: Anonymous 2012-09-13 5:11

I call mine _.h

Name: Anonymous 2012-09-13 6:32

#include <my_anus>

Name: Anonymous 2012-09-13 12:59


#define forever for(;;)
#define until(c) while(!c)
#define unless(c) if(!c)
#define when(c) if(c)

Name: Anonymous 2012-09-13 13:06

>>9
#define until(c) while(!c)

U MENA #define until(c) while(!(c))?!

Name: Anonymous 2012-09-13 13:07

cpp is shit. Use m4.

Name: Anonymous 2012-09-13 17:53

>>11
This isn't SEPPLES, go back to the imageboards

Name: Anonymous 2012-09-13 18:07

>>12
By 'cpp' he meant 'C pre-processor', not 'C++'. Why don't you go back to the imageboards?

Name: Anonymous 2012-09-13 23:20

>>10
YHBT

>>11
Using m4 instead of cpp is like killing a fly with an H-bomb.

Name: Anonymous 2012-09-13 23:39

>>12
lol retard

Don't change these.
Name: Email:
Entire Thread Thread List