[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master ae6b2b8: [doc] Replace bindat example: s/fortune co
From: |
Thien-Thi Nguyen |
Subject: |
[Emacs-diffs] master ae6b2b8: [doc] Replace bindat example: s/fortune cookie/rfc868 payload/ |
Date: |
Fri, 10 Mar 2017 07:22:42 -0500 (EST) |
branch: master
commit ae6b2b8918007c8694563dd8ba14207a560d72c1
Author: Thien-Thi Nguyen <address@hidden>
Commit: Thien-Thi Nguyen <address@hidden>
[doc] Replace bindat example: s/fortune cookie/rfc868 payload/
* doc/lispref/processes.texi (Bindat Examples):
Mention two examples in intro blurb; rewrite first example.
---
doc/lispref/processes.texi | 100 +++++++++------------------------------------
1 file changed, 20 insertions(+), 80 deletions(-)
diff --git a/doc/lispref/processes.texi b/doc/lispref/processes.texi
index 58e04a3..8bfb56b 100644
--- a/doc/lispref/processes.texi
+++ b/doc/lispref/processes.texi
@@ -3337,91 +3337,31 @@ dotted notation.
@node Bindat Examples
@subsection Examples of Byte Unpacking and Packing
address@hidden FIXME? This seems a very long example for something that is not
used
address@hidden very often. As of 24.1, gdb-mi.el is the only user of bindat.el
in Emacs.
address@hidden Maybe one or both of these examples should just be moved to the
address@hidden commentary of bindat.el.
- Here is a complete example of byte unpacking and packing:
+ Here are two complete examples that use bindat.el.
+The first shows simple byte packing:
@lisp
(require 'bindat)
-(defvar fcookie-index-spec
- '((:version u32)
- (:count u32)
- (:longest u32)
- (:shortest u32)
- (:flags u32)
- (:delim u8)
- (:ignored fill 3)
- (:offset repeat (:count) (:foo u32)))
- "Description of a fortune cookie index file's contents.")
-
-(defun fcookie (cookies &optional index)
- "Display a random fortune cookie from file COOKIES.
-Optional second arg INDEX specifies the associated index
-filename, by default \"COOKIES.dat\". Display cookie text
-in buffer \"*Fortune Cookie: BASENAME*\", where BASENAME
-is COOKIES without the directory part."
- (interactive "fCookies file: ")
- (let* ((info (with-temp-buffer
- (insert-file-contents-literally
- (or index (concat cookies ".dat")))
- (bindat-unpack fcookie-index-spec
- (buffer-string))))
- (sel (random (bindat-get-field info :count)))
- (beg (cdar (bindat-get-field info :offset sel)))
- (end (or (cdar (bindat-get-field info
- :offset (1+ sel)))
- (nth 7 (file-attributes cookies)))))
- (switch-to-buffer
- (get-buffer-create
- (format "*Fortune Cookie: %s*"
- (file-name-nondirectory cookies))))
- (erase-buffer)
- (insert-file-contents-literally
- cookies nil beg (- end 3))))
-
-(defun fcookie-create-index (cookies &optional index delim)
- "Scan file COOKIES, and write out its index file.
-Optional arg INDEX specifies the index filename, which by
-default is \"COOKIES.dat\". Optional arg DELIM specifies the
-unibyte character that, when found on a line of its own in
-COOKIES, indicates the border between entries."
- (interactive "fCookies file: ")
- (setq delim (or delim ?%))
- (let ((delim-line (format "\n%c\n" delim))
- (count 0)
- (max 0)
- min p q len offsets)
- (unless (= 3 (string-bytes delim-line))
- (error "Delimiter cannot be represented in one byte"))
- (with-temp-buffer
- (insert-file-contents-literally cookies)
- (while (and (setq p (point))
- (search-forward delim-line (point-max) t)
- (setq len (- (point) 3 p)))
- (setq count (1+ count)
- max (max max len)
- min (min (or min max) len)
- offsets (cons (1- p) offsets))))
- (with-temp-buffer
- (set-buffer-multibyte nil)
- (insert
- (bindat-pack
- fcookie-index-spec
- `((:version . 2)
- (:count . ,count)
- (:longest . ,max)
- (:shortest . ,min)
- (:flags . 0)
- (:delim . ,delim)
- (:offset . ,(mapcar (lambda (o)
- (list (cons :foo o)))
- (nreverse offsets))))))
- (let ((coding-system-for-write 'raw-text-unix))
- (write-file (or index (concat cookies ".dat")))))))
+(defun rfc868-payload ()
+ (bindat-pack
+ '((now-hi u16)
+ (now-lo u16))
+ ;; Emacs uses Unix epoch, while RFC868 epoch
+ ;; is 1900-01-01 00:00:00, which is 2208988800
+ ;; (or #x83aa7e80) seconds more.
+ (let ((now (time-add nil '(#x83aa #x7e80))))
+ `((now-hi . ,(car now))
+ (now-lo . ,(cadr now))))))
+
+(let ((s (rfc868-payload)))
+ (list (multibyte-string-p s)
+ (mapconcat (lambda (byte)
+ (format "%02x" byte))
+ s " ")
+ (current-time-string)))
+ @result{} (nil "dc 6d 17 01" "Fri Mar 10 13:13:53 2017")
@end lisp
The following is an example of defining and unpacking a complex
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] master ae6b2b8: [doc] Replace bindat example: s/fortune cookie/rfc868 payload/,
Thien-Thi Nguyen <=