33#include <wx/zipstrm.h>
34#include <wx/wfstream.h>
35#include <wx/mstream.h>
36#include <wx/txtstrm.h>
41 wxString shortenedName = aProjectName;
42 shortenedName.Replace( wxS(
"ProProject_" ), wxS(
"" ) );
43 shortenedName.Replace( wxS(
"ProDocument_" ), wxS(
"" ) );
44 shortenedName = shortenedName.substr( 0, 10 );
55 wxString key = !libName.empty() ? ( libName +
':' + libReference ) : libReference;
58 libId.
Parse( key,
true );
64std::vector<IMPORT_PROJECT_DESC>
67 std::vector<IMPORT_PROJECT_DESC>
result;
69 std::map<wxString, EASYEDAPRO::PRJ_SCHEMATIC> prjSchematics = aProject.at(
"schematics" );
70 std::map<wxString, EASYEDAPRO::PRJ_BOARD> prjBoards = aProject.at(
"boards" );
72 std::map<wxString, wxString> prjPcbNames;
73 std::map<wxString, nlohmann::json> prjPcbs = aProject.at(
"pcbs" );
75 for(
const auto& [pcbUuid, pcbJsonEntry] : prjPcbs )
77 if( pcbJsonEntry.is_string() )
78 prjPcbNames.emplace( pcbUuid, pcbJsonEntry );
79 else if( pcbJsonEntry.is_object() )
80 prjPcbNames.emplace( pcbUuid, pcbJsonEntry.at(
"title" ) );
83 for(
const auto& [prjName, board] : prjBoards )
87 desc.
PCBId = board.pcb;
90 auto pcbNameIt = prjPcbNames.find( desc.
PCBId );
91 if( pcbNameIt != prjPcbNames.end() )
93 desc.
PCBName = pcbNameIt->second;
96 desc.
PCBName = pcbNameIt->first;
98 prjPcbNames.erase( pcbNameIt );
101 auto schIt = prjSchematics.find( desc.
SchematicId );
102 if( schIt != prjSchematics.end() )
109 prjSchematics.erase( schIt );
112 result.emplace_back( desc );
117 for(
const auto& [pcbId, pcbName] : prjPcbNames )
126 result.emplace_back( desc );
132 for(
const auto& [schId, schData] : prjSchematics )
141 result.emplace_back( desc );
150 const std::set<wxString>& aFileNames )
152 std::shared_ptr<wxZipEntry> entry;
153 wxFFileInputStream in( aZipFileName );
154 wxZipInputStream
zip( in );
156 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
158 wxString
name = entry->GetName();
162 if( aFileNames.find(
name ) != aFileNames.end() )
164 wxMemoryOutputStream memos;
166 wxStreamBuffer* buf = memos.GetOutputStreamBuffer();
169 wxString::FromUTF8( (
char*) buf->GetBufferStart(), buf->GetBufferSize() );
171 return nlohmann::json::parse( str );
174 catch( nlohmann::json::exception& e )
177 wxString::Format(
_(
"JSON error reading '%s': %s" ),
name, e.what() ) );
179 catch( std::exception& e )
185 return nlohmann::json{};
191 static const std::set<wxString> c_files = { wxS(
"project.json" ), wxS(
"device.json" ),
192 wxS(
"footprint.json" ), wxS(
"symbol.json" ) };
194 nlohmann::json j =
FindJsonFile( aZipFileName, c_files );
200 _(
"'%s' does not appear to be a valid EasyEDA (JLCEDA) Pro "
201 "project or library file. Cannot find project.json or device.json." ),
207 const wxString& aFileName,
208 std::function<
bool(
const wxString&,
const wxString&, wxInputStream& )> aCallback )
210 std::shared_ptr<wxZipEntry> entry;
211 wxFFileInputStream in( aFileName );
212 wxZipInputStream
zip( in );
216 THROW_IO_ERROR( wxString::Format(
_(
"Cannot read ZIP archive '%s'" ), aFileName ) );
219 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
221 wxString
name = entry->GetName();
222 wxString baseName =
name.AfterLast(
'\\' ).AfterLast(
'/' ).BeforeFirst(
'.' );
226 if( aCallback(
name, baseName,
zip ) )
229 catch( nlohmann::json::exception& e )
232 wxString::Format(
_(
"JSON error reading '%s': %s" ),
name, e.what() ) );
234 catch( std::exception& e )
243 const wxString& aSource )
245 wxTextInputStream txt( aInput, wxS(
" " ), wxConvUTF8 );
249 std::vector<nlohmann::json> lines;
250 while( aInput.CanRead() )
254 wxString line = txt.ReadLine();
256 if( !line.IsEmpty() )
258 nlohmann::json js = nlohmann::json::parse( line );
259 lines.emplace_back( js );
263 lines.emplace_back( nlohmann::json() );
266 catch( nlohmann::json::exception& e )
268 wxLogWarning( wxString::Format(
_(
"Cannot parse JSON line %d in '%s': %s" ),
269 currentLine, aSource, e.what() ) );
279std::vector<std::vector<nlohmann::json>>
282 wxTextInputStream txt( aInput, wxS(
" " ), wxConvUTF8 );
286 std::vector<std::vector<nlohmann::json>> lineBlocks;
287 lineBlocks.emplace_back();
289 while( aInput.CanRead() )
293 wxString line = txt.ReadLine();
295 if( !line.IsEmpty() )
297 nlohmann::json js = nlohmann::json::parse( line );
298 lineBlocks.back().emplace_back( js );
302 lineBlocks.emplace_back();
305 catch( nlohmann::json::exception& e )
307 wxLogWarning( wxString::Format(
_(
"Cannot parse JSON line %d in '%s': %s" ),
308 currentLine, aSource, e.what() ) );
318std::map<wxString, wxString>
321 std::map<wxString, wxString> stringMap;
323 for(
auto& [key, value] : aInput )
325 if( value.is_string() )
326 stringMap[key] = value.get<wxString>();
327 else if( value.is_number() )
328 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.