1 ; Copyright 2009 by Bdale Garbee <bdale@gag.com>
2 ; gnet-partslistgag.scm
4 ; derived from gnet-partslist3.scm
5 ; Copyright (C) 2001 MIYAMOTO Takanori
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.
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.
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
21 ; The /'s may not work on win32
23 ; Copyright (C) 2001 MIYAMOTO Takanori
24 ; gnet-partslist-common.scm
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.
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.
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
40 (define (get-parts-table packages)
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")
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)))))))
55 (define (write-one-row ls separator end-char port)
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))))
63 (define (get-sortkey-value ls key-column)
64 (list-ref (car ls) key-column))
66 (define (marge-sort-sub ls1 ls2 key-column)
67 (if (or (null? ls1) (null? 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)))))
73 (define (marge-sort ls key-column)
74 (let ((midpoint (inexact->exact (floor (/ (length ls) 2)))))
75 (if (<= (length ls) 1)
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)))))
83 (define (marge-sort-with-multikey ls key-columns)
84 (if (or (<= (length ls) 1) (null? key-columns))
86 (let* ((key-column (car key-columns))
87 (sorted-ls (marge-sort ls key-column))
89 ((lambda (ls) (let loop ((l ls))
92 (cons (get-sortkey-value l key-column) (loop (cdr l))))))
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)))))
101 (define partslistgag:write-top-header
103 (display "\"device\",\"value\",\"footprint\",\"loadstatus\",\"vendor\",\"vendor_part_number\",\"quantity\",\"refdes\"\n" port)))
105 (define (partslistgag:write-partslist ls port)
108 (begin (write-one-row (cdar ls) "\",\"" "\"," port)
109 (write-one-row (caar ls) " " "\"\n" port)
110 (partslistgag:write-partslist (cdr ls) port))))
112 (define partslistgag:write-bottom-footer
117 (define (count-same-parts ls)
120 (let* ((parts-table-no-uref (let ((result '()))
121 (for-each (lambda (l) (set! result (cons (cdr l) result))) (reverse ls))
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)
130 (cons (cons uref-ls (append first-ls (list match-length))) (count-same-parts rest-ls)))))
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))))