|
| 1 | +use super::*; |
| 2 | + |
| 3 | +const LIBRARY : &str = "LibraryLoader.lib"; |
| 4 | +const DOCLIB : &str = "LibraryLoader.dcm"; |
| 5 | +const FP_FOLDER : &str = "LibraryLoader.pretty"; |
| 6 | +const U3D_FOLDER : &str = "LibraryLoader-3dmodels"; |
| 7 | + |
| 8 | +pub fn process(format: &Format, output_path : String, output_files : &mut Files, file_name : String, item : &mut Vec<u8>) -> LLResult<()> { |
| 9 | + |
| 10 | + let file_path = PathBuf::from(output_path.clone()).join(file_name.clone()); |
| 11 | + if let Some(ext) = &file_path.extension() |
| 12 | + { |
| 13 | + match ext.to_str() |
| 14 | + { |
| 15 | + Some("kicad_mod") => |
| 16 | + { |
| 17 | + output_files.insert(format!("{}/{}", FP_FOLDER, file_name), item.clone()); |
| 18 | + }, |
| 19 | + Some("lib") => |
| 20 | + { |
| 21 | + let chars = match std::str::from_utf8(&item[..]) |
| 22 | + { |
| 23 | + Ok(v) => v, |
| 24 | + Err(e) => return Err( |
| 25 | + LLError::new(format!("Could not convert file to valid utf8: {} ({})", e, file_name), "ll-core/src/format/processors/kicad.rs", 24) |
| 26 | + ) |
| 27 | + }; |
| 28 | + |
| 29 | + let mut d_out = Vec::new(); |
| 30 | + |
| 31 | + let enddefs : Vec<_> = chars.match_indices("ENDDEF").collect(); |
| 32 | + let mut start = 0; |
| 33 | + for (idx,_) in enddefs |
| 34 | + { |
| 35 | + let matching_def = chars[start..idx].match_indices("DEF").collect::<Vec<_>>()[0].0; |
| 36 | + d_out.extend_from_slice(&chars[matching_def..idx+6].as_bytes()); |
| 37 | + start = idx+7; |
| 38 | + } |
| 39 | + |
| 40 | + if let Some(f) = output_files.get_mut(LIBRARY) { |
| 41 | + f.append(&mut d_out); |
| 42 | + } |
| 43 | + else { |
| 44 | + // Create entry in output_files for library |
| 45 | + let mut file_data = Vec::new(); |
| 46 | + |
| 47 | + // Load in from possibly existing file |
| 48 | + let fn_lib = PathBuf::from(output_path).join(LIBRARY); |
| 49 | + |
| 50 | + if fn_lib.exists() |
| 51 | + { |
| 52 | + let mut f_lib = std::fs::File::open(fn_lib)?; |
| 53 | + |
| 54 | + f_lib.read_to_end(&mut file_data)?; |
| 55 | + } |
| 56 | + else { |
| 57 | + file_data.extend_from_slice(b"EESchema-LIBRARY Version 2.3\n#encoding utf-8"); |
| 58 | + } |
| 59 | + file_data.push(b'\n'); |
| 60 | + file_data.append(&mut d_out); |
| 61 | + |
| 62 | + output_files.insert(LIBRARY.to_string(), file_data); |
| 63 | + } |
| 64 | + }, |
| 65 | + Some("dcm") => |
| 66 | + { |
| 67 | + let chars = match std::str::from_utf8(&item[..]) |
| 68 | + { |
| 69 | + Ok(v) => v, |
| 70 | + Err(e) => return Err( |
| 71 | + LLError::new(format!("Could not convert file to valid utf8: {} ({})", e, file_name), "ll-core/src/format/processors/kicad.rs", 68) |
| 72 | + ) |
| 73 | + }; |
| 74 | + |
| 75 | + let mut d_out = Vec::new(); |
| 76 | + |
| 77 | + let endcmps : Vec<_> = chars.match_indices("$ENDCMP").collect(); |
| 78 | + let mut start = 0; |
| 79 | + for (idx,_) in endcmps |
| 80 | + { |
| 81 | + let matching_cmp = chars[start..idx].match_indices("$CMP").collect::<Vec<_>>()[0].0; |
| 82 | + d_out.extend_from_slice(&chars[matching_cmp..idx+7].as_bytes()); |
| 83 | + d_out.push(b'\n'); |
| 84 | + start = idx+8; |
| 85 | + } |
| 86 | + |
| 87 | + if let Some(f) = output_files.get_mut(DOCLIB) { |
| 88 | + f.append(&mut d_out); |
| 89 | + } |
| 90 | + else { |
| 91 | + // Create entry in output_files for library |
| 92 | + let mut file_data = Vec::new(); |
| 93 | + |
| 94 | + // Load in from possibly existing file |
| 95 | + let fn_lib = PathBuf::from(output_path).join(DOCLIB); |
| 96 | + |
| 97 | + if fn_lib.exists() |
| 98 | + { |
| 99 | + let mut f_lib = std::fs::File::open(fn_lib)?; |
| 100 | + |
| 101 | + f_lib.read_to_end(&mut file_data)?; |
| 102 | + } |
| 103 | + else { |
| 104 | + file_data.extend_from_slice(b"EESchema-DOCLIB Version 2.0\n"); |
| 105 | + } |
| 106 | + |
| 107 | + file_data.append(&mut d_out); |
| 108 | + |
| 109 | + output_files.insert(DOCLIB.to_string(), file_data); |
| 110 | + } |
| 111 | + }, |
| 112 | + Some("stl") | Some("stp") | Some("wrl") => |
| 113 | + { |
| 114 | + // 3D Files |
| 115 | + let mut folder = PathBuf::from(file_name.clone()); |
| 116 | + folder.set_extension("3dshapes"); |
| 117 | + output_files.insert(format!("{}/{}/{}",U3D_FOLDER,folder.to_string_lossy(), file_name), item.clone()); |
| 118 | + }, |
| 119 | + Some("mod") => |
| 120 | + { |
| 121 | + //Obsolete footprint module file |
| 122 | + } |
| 123 | + _ => println!("Unknown file type: {}", file_name) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + Ok(()) |
| 128 | + /*generic_processor(format, output_path, output_files, file_name, item)*/ |
| 129 | +} |
| 130 | + |
0 commit comments