01b71e369960268734b23bdd03d2196f11cc3434
[hw/teledongle] / scheme / gnet-partslistgag.scm
1 ; Copyright 2009 by Bdale Garbee <bdale@gag.com>
2 ; gnet-partslistgag.scm
3 ;
4 ; derived from gnet-partslist3.scm 
5 ; Copyright (C) 2001 MIYAMOTO Takanori
6
7 ; This program is free software; you can redistribute it and/or modify
8 ; it under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation; either version 2 of the License, or
10 ; (at your option) any later version.
11
12 ; This program is distributed in the hope that it will be useful,
13 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ; GNU General Public License for more details.
16
17 ; You should have received a copy of the GNU General Public License
18 ; along with this program; if not, write to the Free Software
19 ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
20
21 ; The /'s may not work on win32
22
23 ; Copyright (C) 2001 MIYAMOTO Takanori
24 ; gnet-partslist-common.scm
25
26 ; This program is free software; you can redistribute it and/or modify
27 ; it under the terms of the GNU General Public License as published by
28 ; the Free Software Foundation; either version 2 of the License, or
29 ; (at your option) any later version.
30
31 ; This program is distributed in the hope that it will be useful,
32 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
33 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34 ; GNU General Public License for more details.
35
36 ; You should have received a copy of the GNU General Public License
37 ; along with this program; if not, write to the Free Software
38 ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
39
40 (define (get-parts-table packages)
41   (if (null? packages)
42       '()
43       (let ((package (car packages)))
44         (if (string=? "1" (gnetlist:get-package-attribute package "nobom"))
45             (get-parts-table (cdr packages))
46             (cons (list (gnetlist:get-package-attribute package "refdes")
47                         (get-device package)
48                         (get-value package)  
49                         (gnetlist:get-package-attribute package "footprint")
50                         (gnetlist:get-package-attribute package "loadstatus")
51                         (gnetlist:get-package-attribute package "vendor")
52                         (gnetlist:get-package-attribute package "vendor_part_number")) ;; sdb change
53                   (get-parts-table (cdr packages)))))))
54
55 (define (write-one-row ls separator end-char port)
56   (if (null? ls)
57       '()
58       (begin (display "\"" port)
59              (display (car ls) port)
60              (for-each (lambda (st) (display separator port)(display st port)) (cdr ls))
61              (display end-char port))))
62
63 (define (get-sortkey-value ls key-column)
64   (list-ref (car ls) key-column))
65
66 (define (marge-sort-sub ls1 ls2 key-column)
67   (if (or (null? ls1) (null? ls2))
68       (append ls1 ls2)
69       (if (string-ci<=? (get-sortkey-value ls1  key-column) (get-sortkey-value ls2 key-column))
70           (cons (car ls1) (marge-sort-sub (cdr ls1) ls2 key-column))
71           (cons (car ls2) (marge-sort-sub ls1 (cdr ls2) key-column)))))
72
73 (define (marge-sort ls key-column)
74   (let ((midpoint (inexact->exact (floor (/ (length ls) 2)))))
75     (if (<= (length ls) 1)
76         (append ls)
77         (let ((top-half (reverse (list-tail (reverse ls) midpoint)))
78               (bottom-half (list-tail ls (- (length ls) midpoint))))
79           (set! top-half (marge-sort top-half key-column))
80           (set! bottom-half (marge-sort bottom-half key-column))
81           (marge-sort-sub top-half bottom-half key-column)))))
82
83 (define (marge-sort-with-multikey ls key-columns)
84   (if (or (<= (length ls) 1) (null? key-columns))
85       (append ls)
86       (let* ((key-column (car key-columns))
87              (sorted-ls (marge-sort ls key-column))
88              (key-column-only-ls 
89               ((lambda (ls) (let loop ((l ls))
90                               (if (null? l)
91                                   '()
92                                   (cons (get-sortkey-value l key-column) (loop (cdr l))))))
93                sorted-ls))
94              (first-value (get-sortkey-value sorted-ls key-column))
95              (match-length (length (member first-value (reverse key-column-only-ls))))
96              (first-ls (list-tail (reverse sorted-ls) (- (length sorted-ls) match-length)))
97              (rest-ls (list-tail sorted-ls match-length)))
98         (append (marge-sort-with-multikey first-ls (cdr key-columns))
99                 (marge-sort-with-multikey rest-ls key-columns)))))
100
101 (define partslistgag:write-top-header
102   (lambda (port)
103     (display "\"device\",\"value\",\"footprint\",\"loadstatus\",\"vendor\",\"vendor_part_number\",\"quantity\",\"refdes\"\n" port)))
104
105 (define (partslistgag:write-partslist ls port)
106   (if (null? ls)
107       '()
108       (begin (write-one-row (cdar ls) "\",\"" "\"," port)
109              (write-one-row (caar ls) " " "\"\n" port)
110              (partslistgag:write-partslist (cdr ls) port))))
111
112 (define partslistgag:write-bottom-footer
113   (lambda (port)
114       '()
115     ))
116
117 (define (count-same-parts ls)
118   (if (null? ls)
119       (append ls)
120       (let* ((parts-table-no-uref (let ((result '()))
121                                     (for-each (lambda (l) (set! result (cons (cdr l) result))) (reverse ls))
122                                     (append result)))
123              (first-ls (car parts-table-no-uref))
124              (match-length (length (member first-ls (reverse parts-table-no-uref))))
125              (rest-ls (list-tail ls match-length))
126              (match-ls (list-tail (reverse ls) (- (length ls) match-length)))
127              (uref-ls (let ((result '()))
128                         (for-each (lambda (l) (set! result (cons (car l) result))) match-ls)
129                         (append result))))
130         (cons (cons uref-ls (append first-ls  (list match-length))) (count-same-parts rest-ls)))))
131
132 (define partslistgag
133   (lambda (output-filename)
134     (let ((port (open-output-file output-filename))
135           (parts-table (marge-sort-with-multikey (get-parts-table packages) '(1 2 3 0))))
136       (set! parts-table (count-same-parts parts-table))
137       (partslistgag:write-top-header port)
138       (partslistgag:write-partslist parts-table port)
139       (partslistgag:write-bottom-footer port)
140       (close-output-port port))))