-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbommerge.c
196 lines (178 loc) · 5.15 KB
/
bommerge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#define MAX_PARTS 65536
int part_count=0;
char *partids[MAX_PARTS];
char *partds[MAX_PARTS];
char *parturl[MAX_PARTS];
char *partnumbers[MAX_PARTS];
char *partmfgnumbers[MAX_PARTS];
char *partsource[MAX_PARTS];
int lrtrim(char *s)
{
int in=0;
int o=0;
for(int i=0;s[i];i++) {
if (s[i]>' ') in=1;
if (in) s[o++]=s[i];
}
s[o]=0;
while(s[strlen(s)-1]<=' ') s[strlen(s)-1]=0;
return 0;
}
int dequote(char *des)
{
if (des[0]=='"') {
int len=strlen(des)-2;
for(int i=0;i<len;i++) des[i]=des[i+1];
des[len]=0;
}
return 0;
}
int main(int argc,char **argv)
{
if (argc!=4) {
fprintf(stderr,"usage: bommerge <kicad bom CSV file as input> <parts library CSV as input and output> <merged BOM as output>\n");
exit(-1);
}
char line[8192];
FILE *f=fopen(argv[2],"r");
char id[8192],ds[8192],url[8192],number[8192],mfgnumber[8192],source[8192];
int line_num=0;
line[0]=0; fgets(line,8192,f);
int errors=0;
while(line[0]) {
if (sscanf(line,"%[^;];%[^;];%[^;];%[^;];%[^;];%[^;\n\r]",id,ds,url,number,mfgnumber,source)!=6) {
fprintf(stderr,"ERROR: Could not scan line #%d of parts list file '%s'\n",line_num,argv[2]);
errors++;
} else {
partids[part_count]=strdup(id);
partds[part_count]=strdup(ds);
parturl[part_count]=strdup(url);
partnumbers[part_count]=strdup(number);
partmfgnumbers[part_count]=strdup(mfgnumber);
partsource[part_count]=strdup(source);
part_count++;
}
line_num++;
line[0]=0; fgets(line,8192,f);
}
fclose(f);
fprintf(stderr,"INFO: Read library of %d parts.\n",part_count);
if (errors) {
fprintf(stderr,"WARN: Could not parse %d part definitions.\n",errors);
}
// Remove merged BOMs
unlink("mouser.com.csv");
unlink("digikey.com.csv");
unlink("element14.com.csv");
int count=0;
errors=0;
f=fopen(argv[1],"r");
line[0]=0; fgets(line,8192,f);
line_num=0;
while(line[0]) {
// "Id";"Designator";"Package";"Quantity";"Designation";"Supplier and ref";
char index[8192],id[8192],pkg[8192],num[8192],des[8192],supplier[8192];
int r=sscanf(line,"%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]",index,id,pkg,num,des,supplier);
if (r<5) {
fprintf(stderr,"ERROR: Could not scan line #%d of parts list file '%s' (r=%d)\n",line_num,argv[2],r);
errors++;
} else {
// Skip header line
if (count) {
int part_id=-1;
dequote(des);
for(int i=0;i<part_count;i++) {
if (!strcmp(des,partids[i])) {
part_id=i; break;
}
}
if (part_id!=-1) {
char filename[8192];
snprintf(filename,8192,"%s.csv",partsource[part_id]);
FILE *f=fopen(filename,"a");
dequote(num);
fprintf(f,"%s;%s;%s\n",
partnumbers[part_id],
partmfgnumbers[part_id],
num);
fclose(f);
fprintf(stderr,"INFO: Sourcing '%s' from %s\n",des,partsource[part_id]);
} else
{
fprintf(stderr,"INFO: I don't yet know about part '%s'\n",des);
fprintf(stderr," %s x for %s\n",num,id);
fprintf(stderr," Package: %s\n",pkg);
fprintf(stderr," Designation: %s\n",des);
fprintf(stderr," Supplier: %s\n",supplier);
unlink("temp.html");
fprintf(stderr,">>> Deposit HTML page containing the appropriate part into temp.html\n");
FILE *f=NULL;
while(!f) {
sleep(1);
f=fopen("temp.html","r");
}
fprintf(stderr,"INFO: Parsing teml.html...\n");
sleep(1);
line[0]=0; fgets(line,8192,f);
int mouser=0;
char partnumber[8192],mfgpartnumber[8192],desc[8192];
partnumber[0]=0; mfgpartnumber[0]=0; desc[0]=0;
while(line[0]) {
if (strstr(line,"<span id=\"spnMouserPartNumFormattedForProdInfo\">")) {
line[0]=0; fgets(line,8192,f);
lrtrim(line);
strcpy(partnumber,line);
mouser=1;
}
if (strstr(line,"<span id=\"spnManufacturerPartNumber\">")) {
line[0]=0; fgets(line,8192,f);
lrtrim(line);
strcpy(mfgpartnumber,line);
}
if (strstr(line,"<span id=\"spnDescription\">")) {
line[0]=0; fgets(line,8192,f);
lrtrim(line);
strcpy(desc,line);
}
char datasheet[8192];
if (sscanf(line,"<a id=\"pdp-datasheet_0\" href=\"%[^\"]",datasheet)==1) {
fprintf(stderr," Datasheet: '%s'\n",datasheet);
}
line[0]=0; fgets(line,8192,f);
}
fclose(f);
unlink("temp.html");
if (mouser) {
fprintf(stderr," Mouser Part: '%s'\n",partnumber);
fprintf(stderr," Manufacturer Part: '%s'\n",mfgpartnumber);
fprintf(stderr," Description: '%s'\n",desc);
FILE *p=fopen(argv[2],"a");
// id,ds,url,number,mfgnumber,source
fprintf(p,"%s;%s;%s;%s;%s;%s\n",
des,
"<no datasheet>",
"<no URL>",
partnumber,
mfgpartnumber,
"mouser.com");
fclose(p);
}
}
}
count++;
}
line_num++;
line[0]=0; fgets(line,8192,f);
}
fclose(f);
fprintf(stderr,"INFO: Read schematic with %d unique parts.\n",count);
if (errors) {
fprintf(stderr,"WARN: Could not parse %d unique components.\n",errors);
}
return 0;
}