comparison lisp/emacs-lisp/bindat.el @ 70906:6d78b8af6fc8

(bindat-unpack, bindat-pack): Signal error if RAW-DATA is a multibyte string.
author Thien-Thi Nguyen <ttn@gnuvola.org>
date Thu, 25 May 2006 08:51:31 +0000
parents c77bf4eed67a
children ae4950765fb1
comparison
equal deleted inserted replaced
70905:adc0a37fadc1 70906:6d78b8af6fc8
343 (setq struct (append data struct)))))) 343 (setq struct (append data struct))))))
344 struct)) 344 struct))
345 345
346 (defun bindat-unpack (spec raw-data &optional pos) 346 (defun bindat-unpack (spec raw-data &optional pos)
347 "Return structured data according to SPEC for binary data in RAW-DATA. 347 "Return structured data according to SPEC for binary data in RAW-DATA.
348 RAW-DATA is a string or vector. Optional third arg POS specifies the 348 RAW-DATA is a unibyte string or vector. Optional third arg POS specifies
349 starting offset in RAW-DATA." 349 the starting offset in RAW-DATA."
350 (when (multibyte-string-p raw-data)
351 (error "String is multibyte"))
350 (unless pos (setq pos 0)) 352 (unless pos (setq pos 0))
351 (bindat--unpack-group spec)) 353 (bindat--unpack-group spec))
352 354
353 (defun bindat-get-field (struct &rest field) 355 (defun bindat-get-field (struct &rest field)
354 "In structured data STRUCT, return value of field named FIELD. 356 "In structured data STRUCT, return value of field named FIELD.
579 (bindat--pack-item last type len) 581 (bindat--pack-item last type len)
580 )))))) 582 ))))))
581 583
582 (defun bindat-pack (spec struct &optional raw-data pos) 584 (defun bindat-pack (spec struct &optional raw-data pos)
583 "Return binary data packed according to SPEC for structured data STRUCT. 585 "Return binary data packed according to SPEC for structured data STRUCT.
584 Optional third arg RAW-DATA is a pre-allocated string or vector to pack into. 586 Optional third arg RAW-DATA is a pre-allocated unibyte string or vector to
585 Optional fourth arg POS is the starting offset into RAW-DATA. 587 pack into. Optional fourth arg POS is the starting offset into RAW-DATA."
586 Note: The result is a multibyte string; use `string-make-unibyte' on it 588 (when (multibyte-string-p raw-data)
587 to make it unibyte if necessary." 589 (error "Pre-allocated string is multibyte"))
588 (let ((no-return raw-data)) 590 (let ((no-return raw-data))
589 (unless pos (setq pos 0)) 591 (unless pos (setq pos 0))
590 (unless raw-data 592 (unless raw-data
591 (setq raw-data (make-vector (+ pos (bindat-length spec struct)) 0))) 593 (setq raw-data (make-vector (+ pos (bindat-length spec struct)) 0)))
592 (bindat--pack-group struct spec) 594 (bindat--pack-group struct spec)