clean up offboard parts schematic a bit
[hw/telelco] / gnet-partslist-keithp.scm
1 ; Copyright (C) 2001-2010 MIYAMOTO Takanori
2 ; gnet-partslist-keithp.scm
3
4 ; This program is free software; you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation; either version 2 of the License, or
7 ; (at your option) any later version.
8
9 ; This program is distributed in the hope that it will be useful,
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ; GNU General Public License for more details.
13
14 ; You should have received a copy of the GNU General Public License
15 ; along with this program; if not, write to the Free Software
16 ; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 ; The /'s may not work on win32
19 (load (string-append gedadata "/scheme/gnet-partslist-common.scm"))
20
21 (define partslist-keithp:write-top-header
22   (lambda (port)
23     (display ".START\n" port)
24     (display "..device\tvalue\tfootprint\t\tvendor\tvendor_part_number\tquantity\trefdes\n" port)))
25
26 (define (partslist-keithp:write-partslist ls port)
27   (if (null? ls)
28       '()
29       (begin (write-one-row (cdar ls) "\t" "\t" port)
30              (write-one-row (caar ls) " " "\n" port)
31              (partslist-keithp:write-partslist (cdr ls) port))))
32
33 (define partslist-keithp:write-bottom-footer
34   (lambda (port)
35     (display ".END" port)
36     (newline port)))
37
38 (define (count-same-parts ls)
39   (if (null? ls)
40       (append ls)
41       (let* ((parts-table-no-uref (let ((result '()))
42                                     (for-each (lambda (l) (set! result (cons (cdr l) result))) (reverse ls))
43                                     (append result)))
44              (first-ls (car parts-table-no-uref))
45              (match-length (length (member first-ls (reverse parts-table-no-uref))))
46              (rest-ls (list-tail ls match-length))
47              (match-ls (list-tail (reverse ls) (- (length ls) match-length)))
48              (uref-ls (let ((result '()))
49                         (for-each (lambda (l) (set! result (cons (car l) result))) match-ls)
50                         (append result))))
51         (cons (cons uref-ls (append first-ls  (list match-length))) (count-same-parts rest-ls)))))
52
53 (define get-vendor
54    (lambda (package)
55       (gnetlist:get-package-attribute package "vendor")))
56
57 (define get-vendor-part-number
58    (lambda (package)
59       (gnetlist:get-package-attribute package "vendor_part_number")))
60
61 (define get-footprint
62    (lambda (package)
63       (gnetlist:get-package-attribute package "footprint")))
64
65 (define (get-parts-table-keithp packages)
66   (if (null? packages)
67       '()
68       (let ((package (car packages)))
69         (if (string=? (get-device package) "include")
70             (get-parts-table-keithp (cdr packages))
71             (cons (list package
72                         (get-device package)
73                         (get-value package)
74                         (get-footprint package)
75                         (get-vendor package)
76                         (get-vendor-part-number package)) ;; sdb change
77                   (get-parts-table-keithp (cdr packages)))))))
78
79 (define partslist-keithp
80   (lambda (output-filename)
81     (let ((port (open-output-file output-filename))
82           (parts-table (marge-sort-with-multikey (get-parts-table-keithp packages) '(1 2 3 0))))
83       (set! parts-table (count-same-parts parts-table))
84       (partslist-keithp:write-top-header port)
85       (partslist-keithp:write-partslist parts-table port)
86       (close-output-port port))))