Allow for embedded commas (and quotes) in part numbers
[hw/altusmetrum] / bin / partslist-vendor
index 9e2545345d5cfc8dd6e46256b8b00cf4972d61e7..44054c76dee68c90f41d5b0e060b939363112c10 100755 (executable)
@@ -38,6 +38,8 @@ string[*] required_elements = {
        "mfg_part_number",
        "device",
        "value",
+       "refdes",
+       "loadstatus"
 };
 
 bool has_header_member(string member) {
@@ -86,21 +88,88 @@ string part_number(string[string] entry)
                return entry["vendor_part_number"];
 }
 
+string quoted(string v)
+{
+       if (String::index(v, "\"") >= 0 || String::index(v, ",") >= 0) {
+               if (String::index(v, "\"") >= 0) {
+                       string ret = "\"";
+                       for (int i = 0; i < String::length(v); i++) {
+                               if (v[i] == '"')
+                                       ret = ret + "\"";
+                               ret = ret + String::new(v[i]);
+                       }
+                       ret = ret + "\"";
+                       return ret;
+               } else {
+                       return "\"" + v + "\"";
+               }
+       } else {
+               return v;
+       }
+}
+
+void process_seeed(string[string] entry)
+{
+       if (entry["loadstatus"] == "noload")
+               return;
+
+       static bool start = true;
+       if (start) {
+               printf("Part/Designator,Manufacturer Part Number/Seeed SKU, Quantity\n");
+               start = false;
+       }
+
+       string[*] refdes = String::wordsplit(entry["refdes"], " \t");
+       if (dim(refdes) > 1)
+               printf ("\"");
+       for (int i = 0; i < dim(refdes); i++) {
+               printf("%s", refdes[i]);
+               if (i < dim(refdes) - 1)
+                       printf (",");
+       }
+       if (dim(refdes) > 1)
+               printf ("\"");
+       printf(",%s,%s\n", quoted(entry["mfg_part_number"]), entry["quantity"]);
+}
+
 void process_digikey(string[string] entry)
 {
+       if (entry["loadstatus"] == "noload")
+               return;
        printf("%s,%s,%s %s\n",
               entry["quantity"],
-              part_number(entry),
-              entry["device"],
-              entry["value"]);
+              quoted(part_number(entry)),
+              quoted(entry["device"]),
+              quoted(entry["value"]));
+}
+
+void process_mouser(string[string] entry)
+{
+       if (entry["loadstatus"] == "noload")
+               return;
+/*     printf("%s|%s\n", part_number(entry), entry["quantity"]); */
+
+       static bool start = true;
+
+       if (start) {
+               printf("Mouser Part Number,Manufacturer Part Number,Quantity 1\n");
+               start = false;
+       }
+
+       printf("%s,%s,%s\n",
+              quoted(entry["vendor_part_number"]),
+              quoted(entry["mfg_part_number"]),
+              entry["quantity"]);
 }
 
 void process_other(string[string] entry) {
+       if (entry["loadstatus"] == "noload")
+               return;
        printf("%s,%s,%s,%s %s\n",
               entry["vendor"],
               entry["quantity"],
-              part_number(entry),
-              entry["device"],
+              quoted(part_number(entry)),
+              quoted(entry["device"]),
               entry["value"]);
 }
 
@@ -109,12 +178,17 @@ void process_file(file f) {
        while (!File::end(f)) {
                string[string] entry = read_entry(f);
                string vendor = entry["vendor"];
-               if ((!is_uninit(&vendors) && has_vendor(vendors, vendor)) ||
-                   (!is_uninit(&not_vendors) && !has_vendor(not_vendors, vendor))) {
+               if (!is_uninit(&vendors) && has_vendor(vendors, "seeed")) {
+                       process_seeed(entry);
+               } else if ((!is_uninit(&vendors) && has_vendor(vendors, vendor)) ||
+                          (!is_uninit(&not_vendors) && !has_vendor(not_vendors, vendor))) {
                        switch (entry["vendor"]) {
                        case "digikey":
                                process_digikey(entry);
                                break;
+                       case "mouser":
+                               process_mouser(entry);
+                               break;
                        default:
                                process_other(entry);
                                break;