37#include <wx/zipstrm.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/txtstrm.h>
45 wxString shortenedName = aProjectName;
46 shortenedName.Replace( wxS(
"ProProject_" ), wxS(
"" ) );
47 shortenedName.Replace( wxS(
"ProDocument_" ), wxS(
"" ) );
48 shortenedName = shortenedName.substr( 0, 10 );
59 wxString key = !aLibName.empty() ? ( aLibName +
':' + libReference ) : libReference;
62 libId.
Parse( key,
true );
68std::vector<IMPORT_PROJECT_DESC>
71 std::vector<IMPORT_PROJECT_DESC>
result;
73 std::map<wxString, EASYEDAPRO::PRJ_SCHEMATIC> prjSchematics = aProject.at(
"schematics" );
74 std::map<wxString, EASYEDAPRO::PRJ_BOARD> prjBoards = aProject.at(
"boards" );
76 std::map<wxString, wxString> prjPcbNames;
77 std::map<wxString, nlohmann::json> prjPcbs = aProject.at(
"pcbs" );
79 for(
const auto& [pcbUuid, pcbJsonEntry] : prjPcbs )
81 if( pcbJsonEntry.is_string() )
82 prjPcbNames.emplace( pcbUuid, pcbJsonEntry );
83 else if( pcbJsonEntry.is_object() )
84 prjPcbNames.emplace( pcbUuid, pcbJsonEntry.at(
"title" ) );
87 for(
const auto& [prjName, board] : prjBoards )
91 desc.
PCBId = board.pcb;
94 auto pcbNameIt = prjPcbNames.find( desc.
PCBId );
95 if( pcbNameIt != prjPcbNames.end() )
97 desc.
PCBName = pcbNameIt->second;
100 desc.
PCBName = pcbNameIt->first;
102 prjPcbNames.erase( pcbNameIt );
105 auto schIt = prjSchematics.find( desc.
SchematicId );
106 if( schIt != prjSchematics.end() )
113 prjSchematics.erase( schIt );
116 result.emplace_back( desc );
121 for(
const auto& [pcbId, pcbName] : prjPcbNames )
130 result.emplace_back( desc );
136 for(
const auto& [schId, schData] : prjSchematics )
145 result.emplace_back( desc );
154 const std::set<wxString>& aFileNames )
156 std::shared_ptr<wxZipEntry> entry;
157 wxFFileInputStream in( aZipFileName );
158 wxZipInputStream
zip( in );
160 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
162 wxString
name = entry->GetName();
166 if( aFileNames.find(
name ) != aFileNames.end() )
168 wxMemoryOutputStream memos;
170 wxStreamBuffer* buf = memos.GetOutputStreamBuffer();
173 wxString::FromUTF8( (
char*) buf->GetBufferStart(), buf->GetBufferSize() );
175 return nlohmann::json::parse( str );
178 catch( nlohmann::json::exception& e )
181 wxString::Format(
_(
"JSON error reading '%s': %s" ),
name, e.what() ) );
183 catch( std::exception& e )
189 return nlohmann::json{};
195 static const std::set<wxString> c_files = { wxS(
"project.json" ), wxS(
"device.json" ),
196 wxS(
"footprint.json" ), wxS(
"symbol.json" ) };
198 nlohmann::json j =
FindJsonFile( aZipFileName, c_files );
204 _(
"'%s' does not appear to be a valid EasyEDA (JLCEDA) Pro "
205 "project or library file. Cannot find project.json or device.json." ),
211 const wxString& aFileName,
212 std::function<
bool(
const wxString&,
const wxString&, wxInputStream& )> aCallback )
214 std::shared_ptr<wxZipEntry> entry;
215 wxFFileInputStream in( aFileName );
216 wxZipInputStream
zip( in );
220 THROW_IO_ERROR( wxString::Format(
_(
"Cannot read ZIP archive '%s'" ), aFileName ) );
223 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
225 wxString
name = entry->GetName();
226 wxString baseName =
name.AfterLast(
'\\' ).AfterLast(
'/' ).BeforeFirst(
'.' );
230 if( aCallback(
name, baseName,
zip ) )
233 catch( nlohmann::json::exception& e )
236 wxString::Format(
_(
"JSON error reading '%s': %s" ),
name, e.what() ) );
238 catch( std::exception& e )
247 const wxString& aSource )
249 wxTextInputStream txt( aInput, wxS(
" " ), wxConvUTF8 );
253 std::vector<nlohmann::json> lines;
254 while( aInput.CanRead() )
258 wxString line = txt.ReadLine();
260 if( !line.IsEmpty() )
262 nlohmann::json js = nlohmann::json::parse( line );
263 lines.emplace_back( js );
267 lines.emplace_back( nlohmann::json() );
270 catch( nlohmann::json::exception& e )
272 wxLogWarning( wxString::Format(
_(
"Cannot parse JSON line %d in '%s': %s" ),
273 currentLine, aSource, e.what() ) );
283std::vector<std::vector<nlohmann::json>>
286 wxTextInputStream txt( aInput, wxS(
" " ), wxConvUTF8 );
290 std::vector<std::vector<nlohmann::json>> lineBlocks;
291 lineBlocks.emplace_back();
293 while( aInput.CanRead() )
297 wxString line = txt.ReadLine();
299 if( !line.IsEmpty() )
301 nlohmann::json js = nlohmann::json::parse( line );
302 lineBlocks.back().emplace_back( js );
306 lineBlocks.emplace_back();
309 catch( nlohmann::json::exception& e )
311 wxLogWarning( wxString::Format(
_(
"Cannot parse JSON line %d in '%s': %s" ),
312 currentLine, aSource, e.what() ) );
322std::map<wxString, wxString>
325 std::map<wxString, wxString> stringMap;
327 for(
auto& [key, value] : aInput )
329 if( value.is_string() )
330 stringMap[key] = value.get<wxString>();
331 else if( value.is_number() )
332 stringMap[key] = wxString::FromCDouble( value.get<
double>() );
A logical library item identifier and consists of various portions much like a URI.
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
void IterateZipFiles(const wxString &aFileName, std::function< bool(const wxString &, const wxString &, wxInputStream &)> aCallback)
std::vector< nlohmann::json > ParseJsonLines(wxInputStream &aInput, const wxString &aSource)
std::vector< std::vector< nlohmann::json > > ParseJsonLinesWithSeparation(wxInputStream &aInput, const wxString &aSource)
Multiple document types (e.g.
nlohmann::json FindJsonFile(const wxString &aZipFileName, const std::set< wxString > &aFileNames)
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
nlohmann::json ReadProjectOrDeviceFile(const wxString &aZipFileName)
wxString ShortenLibName(wxString aProjectName)
std::map< wxString, wxString > AnyMapToStringMap(const std::map< wxString, nlohmann::json > &aInput)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
Describes how non-KiCad boards and schematics should be imported as KiCad projects.
wxString result
Test unit parsing edge cases and error handling.