Changeset 12432 in project for release/3/formular/trunk/form2txt.scm


Ignore:
Timestamp:
11/10/08 02:58:06 (12 years ago)
Author:
Ivan Raikov
Message:

Added support for reading from and writing to sexpression alists.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/3/formular/trunk/form2txt.scm

    r10243 r12432  
    2020;; <http://www.gnu.org/licenses/>.
    2121;;
     22
     23(require-extension utf8)
     24(require-extension utf8-srfi-13)
     25(require-extension utf8-srfi-14)
    2226
    2327(require-extension srfi-1)
     
    2832(require-extension ssax-core)
    2933(require-extension fmt)
    30 (require-extension utf8)
    31 
     34(require-extension rb-tree)
    3235(require-extension formular)
    3336
     
    5154    ((gt)   ">")
    5255    (else (error 'entity-ref->string "unknown entity " entity))))
    53    
     56
     57(define (alist->rb-tree alst)
     58  (define (s<= x y)
     59    (cond ((string-ci< x y) -1)
     60          ((string-ci= x y) 0)
     61          (else 1)))
     62  (let* ((tree    (make-rb-tree s<=))
     63         (update! (tree 'put!)))
     64    (for-each (lambda (x) (let ((k (car x))
     65                                (v (cdr x)))
     66                            (update! k v)))
     67              alst)
     68    tree))
     69                             
     70
    5471
    5572(define (xml:read-string s)
     
    124141(define opt_prefix        "Form Submission")
    125142(define opt_fields        #f)
     143(define opt_alist_output  #f)
     144(define opt_alist_input   #f)
    126145
    127146(define opts
     
    143162                       (string-append "specify prefix for text page title (default: " opt_prefix ")")
    144163                       (set! opt_prefix (->string arg)))
     164    ,(args:make-option (write-alist)     (required: "FILE")   
     165                       "write an alist representation to FILE"
     166                       (set! opt_alist_output  (->string arg)))
     167    ,(args:make-option (read-alist)     (required: "FILE")   
     168                       "read an alist representation from FILE"
     169                       (set! opt_alist_input  (->string arg)))
    145170    ,(args:make-option (h help)  #:none               "Print help"
    146171                       (usage))))
     
    162187
    163188(define (main options operands)
    164   (let ((forms (mbox->form-stream opt_mbox-path)))
    165     (pp-formular-tree/text (form-stream->tree forms)
     189  (let ((tree  (if opt_alist_input
     190                   (alist->rb-tree (read (open-input-file opt_alist_input)))
     191                   (form-stream->tree (mbox->form-stream opt_mbox-path)))))
     192
     193    (if opt_alist_output
     194        (let ((oport (open-output-file opt_alist_output)))
     195          (display "(" oport)
     196          ((tree 'for-each-ascending) (lambda (x) (write x oport)))
     197          (display ")" oport) ))
     198         
     199    (pp-formular-tree/text tree
    166200                           opt_prefix
    167201                           opt_order
Note: See TracChangeset for help on using the changeset viewer.