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 "provided")
52 (gnetlist:get-package-attribute package "mfg")
53 (gnetlist:get-package-attribute package "mfg_part_number")
54 (gnetlist:get-package-attribute package "vendor")
55 (gnetlist:get-package-attribute package "vendor_part_number")) ;; sdb change
56 (get-parts-table (cdr packages)))))))
58 (define (write-one-row ls separator end-char port)
61 (begin (display "" port)
62 (display (car ls) port)
63 (for-each (lambda (st) (display separator port)(display st port)) (cdr ls))
64 (display end-char port))))
66 (define (get-sortkey-value ls key-column)
67 (list-ref (car ls) key-column))
69 (define (marge-sort-sub ls1 ls2 key-column)
70 (if (or (null? ls1) (null? ls2))
72 (if (string-ci<=? (get-sortkey-value ls1 key-column) (get-sortkey-value ls2 key-column))
73 (cons (car ls1) (marge-sort-sub (cdr ls1) ls2 key-column))
74 (cons (car ls2) (marge-sort-sub ls1 (cdr ls2) key-column)))))
76 (define (marge-sort ls key-column)
77 (let ((midpoint (inexact->exact (floor (/ (length ls) 2)))))
78 (if (<= (length ls) 1)
80 (let ((top-half (reverse (list-tail (reverse ls) midpoint)))
81 (bottom-half (list-tail ls (- (length ls) midpoint))))
82 (set! top-half (marge-sort top-half key-column))
83 (set! bottom-half (marge-sort bottom-half key-column))
84 (marge-sort-sub top-half bottom-half key-column)))))
86 (define (marge-sort-with-multikey ls key-columns)
87 (if (or (<= (length ls) 1) (null? key-columns))
89 (let* ((key-column (car key-columns))
90 (sorted-ls (marge-sort ls key-column))
92 ((lambda (ls) (let loop ((l ls))
95 (cons (get-sortkey-value l key-column) (loop (cdr l))))))
97 (first-value (get-sortkey-value sorted-ls key-column))
98 (match-length (length (member first-value (reverse key-column-only-ls))))
99 (first-ls (list-tail (reverse sorted-ls) (- (length sorted-ls) match-length)))
100 (rest-ls (list-tail sorted-ls match-length)))
101 (append (marge-sort-with-multikey first-ls (cdr key-columns))
102 (marge-sort-with-multikey rest-ls key-columns)))))
104 (define partslistgag:write-top-header
106 (display "device,value,footprint,loadstatus,provided,mfg,mfg_part_number,vendor,vendor_part_number,quantity,refdes\n" port)))
108 (define (partslistgag:write-partslist ls port)
111 (begin (write-one-row (cdar ls) "," "," port)
112 (write-one-row (caar ls) " " "\n" port)
113 (partslistgag:write-partslist (cdr ls) port))))
115 (define partslistgag:write-bottom-footer
120 (define (count-same-parts ls)
123 (let* ((parts-table-no-uref (let ((result '()))
124 (for-each (lambda (l) (set! result (cons (cdr l) result))) (reverse ls))
126 (first-ls (car parts-table-no-uref))
127 (match-length (length (member first-ls (reverse parts-table-no-uref))))
128 (rest-ls (list-tail ls match-length))
129 (match-ls (list-tail (reverse ls) (- (length ls) match-length)))
130 (uref-ls (let ((result '()))
131 (for-each (lambda (l) (set! result (cons (car l) result))) match-ls)
133 (cons (cons uref-ls (append first-ls (list match-length))) (count-same-parts rest-ls)))))
136 (lambda (output-filename)
137 (let ((port (open-output-file output-filename))
138 (parts-table (marge-sort-with-multikey (get-parts-table packages) '(1 2 3 0))))
139 (set! parts-table (count-same-parts parts-table))
140 (partslistgag:write-top-header port)
141 (partslistgag:write-partslist parts-table port)
142 (partslistgag:write-bottom-footer port)
143 (close-output-port port))))