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