According to the bindat spec docs, there are two specs to pack
strings. They are
strz. Both are for a fixed length
Now, suppose I want to pack
("some" "list" "of" "string), I should
(bindat-pack '((elem0 strz 5) (elem1 strz 5) (elem2 strz 3) (elem3 strz 7)) '((elem0 . "some") (elem1 . "list") (elem2 . "of") (elem3 . "string")))
But that is so inflexible for several reasons:
- I have to put name and length manually.
strzneed to know size of the string before hand instead of just calculate the length of given string.
repeat in the bindat spec but both are the same,
both need length of the list before hand.
So far, I've come up with this:
(require 'seq) (require 'bindat) (defun make-strz-spec (name value) (let ((value (cond ((stringp value) value) ((symbolp value) (symbol-name value)) (t (error "%s is neither string nor a symbol" value)))) (sym (make-symbol name))) `((,sym strz ,(+ 1 (string-bytes value))) (,sym . ,value)))) (defun make-strz-spec-list (string-list) (seq-map-indexed (lambda (str idx) (let ((name (concat "elem-" (number-to-string idx)))) (make-strz-spec name str))) string-list)) (defun bindat-pack-list-of-string (string-list) (let ((spec-pair (make-strz-spec-list string-list))) (bindat-pack (mapcar (lambda (elem) (nth 0 elem)) spec-pair) (mapcar (lambda (elem) (nth 1 elem)) spec-pair)))) ;; this (bindat-pack-list-of-string '("some" "list" "of" "string"))
But, that introduces more functions globally with very specific purpose. I don't want to pollute programming environment just for this.
Since I am still new with emacs-lisp and lisp in general. I am sure there's a better way than mine.
So, back to my main question, how can I bind-pack list of null terminated string?