aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorThien-Thi Nguyen2017-03-10 13:25:41 +0100
committerThien-Thi Nguyen2017-03-10 13:25:41 +0100
commitae6b2b8918007c8694563dd8ba14207a560d72c1 (patch)
treecba2e7ead80ce33fed2325f99fa9a93f1326fe92 /doc
parent26848af97f333c4699934a545eb2888b1006b326 (diff)
downloademacs-ae6b2b8918007c8694563dd8ba14207a560d72c1.tar.gz
emacs-ae6b2b8918007c8694563dd8ba14207a560d72c1.zip
[doc] Replace bindat example: s/fortune cookie/rfc868 payload/
* doc/lispref/processes.texi (Bindat Examples): Mention two examples in intro blurb; rewrite first example.
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/processes.texi100
1 files changed, 20 insertions, 80 deletions
diff --git a/doc/lispref/processes.texi b/doc/lispref/processes.texi
index 58e04a311a1..8bfb56bd96e 100644
--- a/doc/lispref/processes.texi
+++ b/doc/lispref/processes.texi
@@ -3337,91 +3337,31 @@ dotted notation.
3337 3337
3338@node Bindat Examples 3338@node Bindat Examples
3339@subsection Examples of Byte Unpacking and Packing 3339@subsection Examples of Byte Unpacking and Packing
3340@c FIXME? This seems a very long example for something that is not used
3341@c very often. As of 24.1, gdb-mi.el is the only user of bindat.el in Emacs.
3342@c Maybe one or both of these examples should just be moved to the
3343@c commentary of bindat.el.
3344 3340
3345 Here is a complete example of byte unpacking and packing: 3341 Here are two complete examples that use bindat.el.
3342The first shows simple byte packing:
3346 3343
3347@lisp 3344@lisp
3348(require 'bindat) 3345(require 'bindat)
3349 3346
3350(defvar fcookie-index-spec 3347(defun rfc868-payload ()
3351 '((:version u32) 3348 (bindat-pack
3352 (:count u32) 3349 '((now-hi u16)
3353 (:longest u32) 3350 (now-lo u16))
3354 (:shortest u32) 3351 ;; Emacs uses Unix epoch, while RFC868 epoch
3355 (:flags u32) 3352 ;; is 1900-01-01 00:00:00, which is 2208988800
3356 (:delim u8) 3353 ;; (or #x83aa7e80) seconds more.
3357 (:ignored fill 3) 3354 (let ((now (time-add nil '(#x83aa #x7e80))))
3358 (:offset repeat (:count) (:foo u32))) 3355 `((now-hi . ,(car now))
3359 "Description of a fortune cookie index file's contents.") 3356 (now-lo . ,(cadr now))))))
3360 3357
3361(defun fcookie (cookies &optional index) 3358(let ((s (rfc868-payload)))
3362 "Display a random fortune cookie from file COOKIES. 3359 (list (multibyte-string-p s)
3363Optional second arg INDEX specifies the associated index 3360 (mapconcat (lambda (byte)
3364filename, by default \"COOKIES.dat\". Display cookie text 3361 (format "%02x" byte))
3365in buffer \"*Fortune Cookie: BASENAME*\", where BASENAME 3362 s " ")
3366is COOKIES without the directory part." 3363 (current-time-string)))
3367 (interactive "fCookies file: ") 3364 @result{} (nil "dc 6d 17 01" "Fri Mar 10 13:13:53 2017")
3368 (let* ((info (with-temp-buffer
3369 (insert-file-contents-literally
3370 (or index (concat cookies ".dat")))
3371 (bindat-unpack fcookie-index-spec
3372 (buffer-string))))
3373 (sel (random (bindat-get-field info :count)))
3374 (beg (cdar (bindat-get-field info :offset sel)))
3375 (end (or (cdar (bindat-get-field info
3376 :offset (1+ sel)))
3377 (nth 7 (file-attributes cookies)))))
3378 (switch-to-buffer
3379 (get-buffer-create
3380 (format "*Fortune Cookie: %s*"
3381 (file-name-nondirectory cookies))))
3382 (erase-buffer)
3383 (insert-file-contents-literally
3384 cookies nil beg (- end 3))))
3385
3386(defun fcookie-create-index (cookies &optional index delim)
3387 "Scan file COOKIES, and write out its index file.
3388Optional arg INDEX specifies the index filename, which by
3389default is \"COOKIES.dat\". Optional arg DELIM specifies the
3390unibyte character that, when found on a line of its own in
3391COOKIES, indicates the border between entries."
3392 (interactive "fCookies file: ")
3393 (setq delim (or delim ?%))
3394 (let ((delim-line (format "\n%c\n" delim))
3395 (count 0)
3396 (max 0)
3397 min p q len offsets)
3398 (unless (= 3 (string-bytes delim-line))
3399 (error "Delimiter cannot be represented in one byte"))
3400 (with-temp-buffer
3401 (insert-file-contents-literally cookies)
3402 (while (and (setq p (point))
3403 (search-forward delim-line (point-max) t)
3404 (setq len (- (point) 3 p)))
3405 (setq count (1+ count)
3406 max (max max len)
3407 min (min (or min max) len)
3408 offsets (cons (1- p) offsets))))
3409 (with-temp-buffer
3410 (set-buffer-multibyte nil)
3411 (insert
3412 (bindat-pack
3413 fcookie-index-spec
3414 `((:version . 2)
3415 (:count . ,count)
3416 (:longest . ,max)
3417 (:shortest . ,min)
3418 (:flags . 0)
3419 (:delim . ,delim)
3420 (:offset . ,(mapcar (lambda (o)
3421 (list (cons :foo o)))
3422 (nreverse offsets))))))
3423 (let ((coding-system-for-write 'raw-text-unix))
3424 (write-file (or index (concat cookies ".dat")))))))
3425@end lisp 3365@end lisp
3426 3366
3427The following is an example of defining and unpacking a complex 3367The following is an example of defining and unpacking a complex