<commit_before>; buffmap.scm - Build variables from a buffer following predefined definition.
;
; Copyright (c) 2011 by <NAME> <<EMAIL>>.
; All Right Reserved.
;
; These utilities builds functionis to read and write variables.
; A typical use is in conjunction with a binary communication protocol (ie:
; modbus), the variables are read from a buffer and written using a function
; provided by client.
;
; TODO: This is actually a work in progress and need substantial testing.
; 

(##namespace ("buffmap#"))
(##include "~~lib/gambit#.scm")
(##namespace ("ctypes#" read-f32 read-f64))


(define types '(bit byte f32 f64))

(define-type
  var
  id: F32C84CD-66FA-4E85-920E-13C4560DDA55
  constructor: %make-var
  name
  type
  offset
  bit-index)

(define (make-var name type offset #!optional (bit-index #f))
  (cond
    ((and (eq? type 'bit)
	  (integer? bit-index)
	  (and (>= bit-index 0)
	       (< bit-index 8))) (%make-var name type offset bit-index))
    ((not (memq type types)) (raise "Unknow type"))
    (else (%make-var name type offset #f))))

; Build an hash table of accessor to BUFFER with mapping defined by DATAMAP,
; where DATAMAP is a list of variables as defined by make-var.
;
; Usage: ((table-ref accessors 'v0))

(define (build-accessors buffer datamap)
  (let ((accessors (make-table)))
    (for-each
     (lambda (x)
       (let ((name (var-name x))
             (type (var-type x))
             (offset (var-offset x)))
	 (cond
	   ((eq? type 'byte)
	    (table-set! accessors name
			(lambda ()
			  (u8vector-ref buffer offset))))
	   ((eq? type 'bit)
	    (table-set! accessors name
			(lambda ()
			  (extract-bit-field
			    1
			    (var-bit-index x)
			    (u8vector-ref buffer offset)))))
	   ((eq? type 'f32)
	    (table-set! accessors name
			(lambda ()
			  (with-input-from-u8vector
			    (subu8vector buffer offset (+ offset 4))
			    (lambda () (read-f32 (current-input-port)))))))
	   ((eq? type 'f64)
	    (table-set! accessors name
			(lambda ()
			  (with-input-from-u8vector
			    (subu8vector buffer offset (+ offset 8))
			    (lambda () (read-f64 (current-input-port)))))))<commit_msg>Handle u16 and u32 datatypes.<commit_after>; buffmap.scm - Build variables from a buffer following predefined definition.
;
; Copyright (c) 2011 by <NAME> <<EMAIL>>.
; All Right Reserved.
;
; These utilities builds functionis to read and write variables.
; A typical use is in conjunction with a binary communication protocol (ie:
; modbus), the variables are read from a buffer and written using a function
; provided by client.
;
; TODO: This is actually a work in progress and need substantial testing.
; 

(##namespace ("buffmap#"))
(##include "~~lib/gambit#.scm")
(##namespace ("ctypes#" read-u16 write-u16 read-u32 write-u32 read-f32 read-f64))


(define types '(bit byte f32 f64))

(define-type
  var
  id: F32C84CD-66FA-4E85-920E-13C4560DDA55
  constructor: %make-var
  name
  type
  offset
  bit-index)

(define (make-var name type offset #!optional (bit-index #f))
  (cond
    ((and (eq? type 'bit)
	  (integer? bit-index)
	  (and (>= bit-index 0)
	       (< bit-index 8))) (%make-var name type offset bit-index))
    ((not (memq type types)) (raise "Unknow type"))
    (else (%make-var name type offset #f))))

; Build an hash table of accessor to BUFFER with mapping defined by DATAMAP,
; where DATAMAP is a list of variables as defined by make-var.
;
; Usage: ((table-ref accessors 'v0))

(define (build-accessors buffer datamap)
  (let ((accessors (make-table)))
    (for-each
     (lambda (x)
       (let ((name (var-name x))
             (type (var-type x))
             (offset (var-offset x)))
	 (cond
	   ((eq? type 'byte) (table-set! accessors name
                               (lambda ()
                                 (u8vector-ref buffer offset))))
	   ((eq? type 'bit) (table-set! accessors name
                              (lambda ()
                                (extract-bit-field
                                  1
                                  (var-bit-index x)
                                  (u8vector-ref buffer offset)))))
           ((eq? type 'u16) (table-set! accessor name
                              (lambda ()
                                (with-input-from-u8vector
                                  (subu8vector buffer offset (+ offset 2))
                                  (lambda () (read-u16))))))
           ((eq? type 'u32) (table-set! accessor name
                              (lambda ()
                                (with-input-from-u8vector
                                  (subu8vector buffer offset (+ offset 4))
                                  (lambda () (read-u32))))))
	   ((eq? type 'f32) (table-set! accessors name
                              (lambda ()
                                (with-input-from-u8vector
                                  (subu8vector buffer offset (+ offset 4))
                                  (lambda () (read-f32))))))
	   ((eq? type 'f64) (table-set! accessors name
                              (lambda ()
                                (with-input-from-u8vector
                                  (subu8vector buffer offset (+ offset 8))
                                  (lambda () (read-f64))))))
{'fraction_non_alphanumeric': 0.1270234241096934, 'fraction_numerical': 0.024757189106836792, 'mean_word_length': 1.942296918767507, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '757752', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1550, 'n_words': 505}