38#include <wx/filename.h>
40#include <wx/mstream.h>
41#include <wx/wfstream.h>
42#include <wx/zipstrm.h>
51 return std::string( aStr.ToUTF8() );
58wxString
V3JsonToString(
const nlohmann::json& aValue,
const wxString& aDefault )
60 if( aValue.is_string() )
61 return aValue.get<wxString>();
63 if( aValue.is_number_integer() )
64 return wxString::Format(
"%lld",
static_cast<long long>( aValue.get<int64_t>() ) );
66 if( aValue.is_number_unsigned() )
67 return wxString::Format(
"%llu",
static_cast<unsigned long long>( aValue.get<uint64_t>() ) );
69 if( aValue.is_number_float() )
70 return wxString::FromDouble( aValue.get<
double>() );
72 if( aValue.is_boolean() )
73 return aValue.get<
bool>() ? wxS(
"true" ) : wxS(
"false" );
79wxString
V3GetString(
const nlohmann::json& aObj,
const char* aKey,
const wxString& aDefault )
81 if( !aObj.is_object() )
84 auto it = aObj.find( aKey );
86 if( it == aObj.end() )
93wxString
V3GetString(
const nlohmann::json& aObj,
const wxString& aKey,
const wxString& aDefault )
99double V3GetDouble(
const nlohmann::json& aObj,
const char* aKey,
double aDefault )
101 if( !aObj.is_object() )
104 auto it = aObj.find( aKey );
106 if( it == aObj.end() )
109 if( it->is_number() )
110 return it->get<
double>();
112 if( it->is_string() )
116 if( it->get<wxString>().ToCDouble( &value ) )
124int V3GetInt(
const nlohmann::json& aObj,
const char* aKey,
int aDefault )
126 if( !aObj.is_object() )
129 auto it = aObj.find( aKey );
131 if( it == aObj.end() )
134 if( it->is_number_integer() )
135 return it->get<
int>();
137 if( it->is_number_float() )
138 return static_cast<int>( std::lround( it->get<
double>() ) );
140 if( it->is_boolean() )
141 return it->get<
bool>() ? 1 : 0;
143 if( it->is_string() )
147 if( it->get<wxString>().ToLong( &value ) )
148 return static_cast<int>( value );
155bool V3GetBool(
const nlohmann::json& aObj,
const char* aKey,
bool aDefault )
157 if( !aObj.is_object() )
160 auto it = aObj.find( aKey );
162 if( it == aObj.end() )
165 if( it->is_boolean() )
166 return it->get<
bool>();
168 if( it->is_number() )
169 return it->get<
double>() != 0.0;
171 if( it->is_string() )
173 wxString s = it->get<wxString>();
175 if( s.CmpNoCase( wxS(
"true" ) ) == 0 )
178 if( s.CmpNoCase( wxS(
"false" ) ) == 0 )
183 if( s.ToLong( &value ) )
193 if( !aObj.is_object() )
196 auto it = aObj.find( aKey );
198 return it == aObj.end() || it->is_null();
205 return nlohmann::json();
209 return nlohmann::json::parse(
ToStdString( aId ) );
213 return nlohmann::json();
224static inline wxString GetString(
const nlohmann::json& aObj,
const char* aKey,
225 const wxString& aDefault = wxEmptyString )
230static inline int GetInt(
const nlohmann::json& aObj,
const char* aKey,
int aDefault = 0 )
236static wxString ParseDocType(
const nlohmann::json& aHead )
238 return GetString( aHead,
"docType" );
242static wxString ParseDocUuid(
const nlohmann::json& aHead )
244 return GetString( aHead,
"uuid" );
248static std::string ReadEntryText( wxInputStream& aInput )
250 wxMemoryOutputStream mem;
253 wxStreamBuffer* buf = mem.GetOutputStreamBuffer();
254 return std::string(
static_cast<const char*
>( buf->GetBufferStart() ), buf->GetBufferSize() );
258static std::string_view NextEpruLine( std::string_view& aText )
260 size_t eol = aText.find_first_of(
"\r\n" );
261 std::string_view line = aText.substr( 0, eol );
263 if( eol == std::string_view::npos )
269 bool crlf = aText[eol] ==
'\r' && eol + 1 < aText.size() && aText[eol + 1] ==
'\n';
270 aText.remove_prefix( eol + ( crlf ? 2 : 1 ) );
276static bool SplitEpruLine( std::string_view aLine, std::string_view& aOuter, std::string_view& aInner )
278 if( !aLine.empty() && aLine.back() ==
'|' )
279 aLine.remove_suffix( 1 );
281 size_t sepPos = aLine.find(
"||" );
283 if( sepPos == std::string_view::npos )
286 aOuter = aLine.substr( 0, sepPos );
287 aInner = aLine.substr( sepPos + 2 );
293static void ParseEpruStream( wxInputStream& aInput,
const wxString& aSource,
294 std::vector<V3_DOC_RAW>& aDocs )
296 std::string
text = ReadEntryText( aInput );
297 std::string_view remaining =
text;
299 std::optional<V3_DOC_RAW> currentDoc;
301 auto flushDoc = [&]()
309 std::vector<V3_ROW> keptRows;
310 keptRows.reserve( currentDoc->rows.size() );
312 for(
V3_ROW& row : currentDoc->rows )
314 if( !row.
inner.is_null() )
315 keptRows.push_back( std::move( row ) );
318 currentDoc->rows = std::move( keptRows );
319 currentDoc->rowById.clear();
321 for(
size_t i = 0; i < currentDoc->rows.size(); ++i )
323 if( !currentDoc->rows[i].id.empty() )
324 currentDoc->rowById[currentDoc->rows[i].id] = i;
327 aDocs.push_back( std::move( *currentDoc ) );
332 while( !remaining.empty() )
334 std::string_view outerStr;
335 std::string_view innerStr;
337 if( !SplitEpruLine( NextEpruLine( remaining ), outerStr, innerStr ) )
345 nlohmann::json outer = nlohmann::json::parse( outerStr );
346 nlohmann::json inner;
348 if( !innerStr.empty() )
349 inner = nlohmann::json::parse( innerStr );
351 wxString type = GetString( outer,
"type" );
353 if( type == wxS(
"DOCHEAD" ) )
359 doc.
docType = ParseDocType( inner );
360 doc.
uuid = ParseDocUuid( inner );
363 THROW_IO_ERRORF(
_(
"Invalid DOCHEAD in '%s' at line %d" ), aSource, currentLine );
365 currentDoc = std::move( doc );
371 THROW_IO_ERRORF(
_(
"Expected DOCHEAD in '%s' at line %d" ), aSource, currentLine );
374 row.
outer = std::move( outer );
375 row.
inner = std::move( inner );
377 row.
id = GetString( row.
outer,
"id" );
380 wxString dedupId = row.
id;
382 if( dedupId.empty() )
383 dedupId = wxString::Format(
"__line_%d", currentLine );
385 auto it = currentDoc->rowById.find( dedupId );
387 if( it == currentDoc->rowById.end() )
389 currentDoc->rowById[dedupId] = currentDoc->rows.size();
390 currentDoc->rows.emplace_back( std::move( row ) );
392 else if( row.
ticket >= currentDoc->rows[it->second].ticket )
394 currentDoc->rows[it->second] = std::move( row );
397 catch( nlohmann::json::exception& e )
399 THROW_IO_ERRORF(
_(
"JSON error in '%s' at line %d: %s" ), aSource, currentLine, e.what() );
409struct V3_LIBRARY_CONTENTS
411 bool hasIndex =
false;
412 bool hasElibu =
false;
413 std::set<wxString> docTypes;
417static bool HasValidV3LibraryContents(
const V3_LIBRARY_CONTENTS& aContents,
const wxString& aRequiredDocType )
419 if( !aContents.hasIndex || !aContents.hasElibu )
422 if( aRequiredDocType.empty() )
425 return aContents.docTypes.contains( aRequiredDocType );
429static void ScanEpruDocTypes( wxInputStream& aInput,
const wxString& aSource, std::set<wxString>& aDocTypes )
431 std::string
text = ReadEntryText( aInput );
432 std::string_view remaining =
text;
435 while( !remaining.empty() )
437 std::string_view outerStr;
438 std::string_view innerStr;
440 if( !SplitEpruLine( NextEpruLine( remaining ), outerStr, innerStr ) )
448 nlohmann::json outer = nlohmann::json::parse( outerStr );
450 if( GetString( outer,
"type" ) == wxS(
"DOCHEAD" ) && !innerStr.empty() )
452 nlohmann::json inner = nlohmann::json::parse( innerStr );
453 wxString docType = ParseDocType( inner );
455 if( !docType.empty() )
456 aDocTypes.insert( docType );
459 catch( nlohmann::json::exception& e )
461 THROW_IO_ERRORF(
_(
"JSON error in '%s' at line %d: %s" ), aSource, currentLine, e.what() );
469static nlohmann::json ReadJsonEntry( wxInputStream& aInput,
const wxString& aEntryName )
473 return nlohmann::json::parse( ReadEntryText( aInput ) );
475 catch( nlohmann::json::exception& e )
477 THROW_IO_ERRORF(
_(
"JSON error reading '%s': %s" ), aEntryName, e.what() );
482static void MergeLibraryIndex( nlohmann::json& aTarget, nlohmann::json&& aIndex )
484 if( !aIndex.is_object() )
487 for(
auto& [key, value] : aIndex.items() )
489 if( aTarget.contains( key ) && aTarget[key].is_object() && value.is_object() )
491 for(
auto& [itemKey, itemValue] : value.items() )
492 aTarget[key][itemKey] = std::move( itemValue );
494 else if( !aTarget.contains( key ) || !value.is_object() || !value.empty() )
496 aTarget[key] = std::move( value );
502static V3_LIBRARY_CONTENTS ScanV3LibraryArchive(
const wxString& aFileName,
const wxString& aRequiredDocType )
504 V3_LIBRARY_CONTENTS contents;
506 std::shared_ptr<wxZipEntry> entry;
507 wxFFileInputStream in( aFileName );
508 wxZipInputStream
zip( in );
513 while( entry.reset(
zip.GetNextEntry() ), entry.get() )
515 wxString entryName = entry->GetName();
516 wxString baseName = entryName.AfterLast(
'\\' ).AfterLast(
'/' ).Lower();
518 if( baseName == wxS(
"symbol2.json" ) || baseName == wxS(
"footprint2.json" )
519 || baseName == wxS(
"device2.json" ) )
521 contents.hasIndex =
true;
523 else if( baseName.EndsWith( wxS(
".elibu" ) ) )
525 contents.hasElibu =
true;
527 if( !aRequiredDocType.empty() )
528 ScanEpruDocTypes(
zip, entryName, contents.docTypes );
531 if( HasValidV3LibraryContents( contents, aRequiredDocType ) )
539static std::vector<V3_DOC_RAW> ParseV3DocsFromArchive(
const wxString& aFileName, nlohmann::json& aProject2 )
541 std::vector<V3_DOC_RAW> docs;
543 bool hasProject2 =
false;
544 bool hasEpru =
false;
546 std::shared_ptr<wxZipEntry> entry;
547 wxFFileInputStream in( aFileName );
548 wxZipInputStream
zip( in );
553 while( entry.reset(
zip.GetNextEntry() ), entry.get() )
555 wxString entryName = entry->GetName();
556 wxString baseName = entryName.AfterLast(
'\\' ).AfterLast(
'/' );
558 if( baseName.CmpNoCase( wxS(
"project2.json" ) ) == 0 )
560 aProject2 = ReadJsonEntry(
zip, entryName );
563 else if( baseName.Lower().EndsWith( wxS(
".epru" ) ) )
566 ParseEpruStream(
zip, entryName, docs );
570 if( !hasProject2 || !hasEpru )
572 THROW_IO_ERRORF(
_(
"'%s' does not appear to be a valid EasyEDA (JLCEDA) Pro v3 project. "
573 "Cannot find project2.json and .epru documents." ), aFileName );
580static std::vector<V3_DOC_RAW> ParseV3DocsFromLibraryArchive(
const wxString& aFileName, nlohmann::json& aLibraryIndex )
582 std::vector<V3_DOC_RAW> docs;
583 V3_LIBRARY_CONTENTS contents;
585 std::shared_ptr<wxZipEntry> entry;
586 wxFFileInputStream in( aFileName );
587 wxZipInputStream
zip( in );
592 aLibraryIndex = nlohmann::json::object();
594 while( entry.reset(
zip.GetNextEntry() ), entry.get() )
596 wxString entryName = entry->GetName();
597 wxString baseName = entryName.AfterLast(
'\\' ).AfterLast(
'/' );
598 wxString lowerBaseName = baseName.Lower();
600 if( lowerBaseName == wxS(
"symbol2.json" ) || lowerBaseName == wxS(
"footprint2.json" )
601 || lowerBaseName == wxS(
"device2.json" ) )
603 nlohmann::json
index = ReadJsonEntry(
zip, entryName );
605 MergeLibraryIndex( aLibraryIndex, std::move(
index ) );
607 contents.hasIndex =
true;
609 else if( lowerBaseName.EndsWith( wxS(
".elibu" ) ) )
611 contents.hasElibu =
true;
612 ParseEpruStream(
zip, entryName, docs );
615 contents.docTypes.insert( doc.
docType );
619 if( !HasValidV3LibraryContents( contents, wxEmptyString ) )
621 THROW_IO_ERRORF(
_(
"'%s' does not appear to be a valid EasyEDA (JLCEDA) Pro v3 library. Cannot find "
622 "symbol2.json, footprint2.json or device2.json and .elibu documents." ),
644 wxFileName fn( aFileName );
645 wxString ext = fn.GetExt().Lower();
647 if( ext != wxS(
"epro2" ) && ext != wxS(
"zip" ) )
652 std::shared_ptr<wxZipEntry> entry;
653 wxFFileInputStream in( aFileName );
654 wxZipInputStream
zip( in );
659 bool hasProject2 =
false;
660 bool hasEpru =
false;
662 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
664 wxString entryName = entry->GetName();
665 wxString baseName = entryName.AfterLast(
'\\' ).AfterLast(
'/' );
667 if( baseName.CmpNoCase( wxS(
"project2.json" ) ) == 0 )
669 else if( baseName.Lower().EndsWith( wxS(
".epru" ) ) )
672 if( hasProject2 && hasEpru )
687 wxFileName fn( aFileName );
689 if( fn.GetExt().Lower() != wxS(
"elibz2" ) )
694 V3_LIBRARY_CONTENTS contents = ScanV3LibraryArchive( aFileName, aRequiredDocType );
695 return HasValidV3LibraryContents( contents, aRequiredDocType );
715 m_rawDocs[raw.docType][raw.uuid] = std::move( raw );
730 m_rawDocs[raw.docType][raw.uuid] = std::move( raw );
736 static const std::map<wxString, V3_DOC_RAW>
empty;
749 auto typeIt =
m_rawDocs.find( aDocType );
754 auto docIt = typeIt->second.find( aUuid );
756 if( docIt == typeIt->second.end() )
759 return &docIt->second;
void Load()
Load all v3 docs from the archive.
const V3_DOC_RAW * FindRawDoc(const wxString &aDocType, const wxString &aUuid) const
const std::map< wxString, V3_DOC_RAW > & GetRawDocs(const wxString &aDocType) const
nlohmann::json m_project2
static bool IsV3Library(const wxString &aFileName, const wxString &aRequiredDocType=wxEmptyString)
Return true if aFileName looks like an EasyEDA Pro v3 library archive.
std::map< wxString, std::map< wxString, V3_DOC_RAW > > m_rawDocs
nlohmann::json m_libraryIndex
V3_DOC_PARSER(const wxString &aFileName)
static bool IsV3Archive(const wxString &aFileName)
Return true if aFileName looks like an EasyEDA Pro v3 archive.
void LoadLibrary()
Load all v3 docs from a library archive.
static bool empty(const wxTextEntryBase *aCtrl)
static std::string ToStdString(const wxString &aStr)
#define THROW_IO_ERRORF(msg,...)
bool V3GetBool(const nlohmann::json &aObj, const char *aKey, bool aDefault)
nlohmann::json V3ParseIdArray(const wxString &aId)
bool V3IsNullOrMissing(const nlohmann::json &aObj, const char *aKey)
double V3GetDouble(const nlohmann::json &aObj, const char *aKey, double aDefault)
wxString V3JsonToString(const nlohmann::json &aValue, const wxString &aDefault)
int V3GetInt(const nlohmann::json &aObj, const char *aKey, int aDefault)
wxString V3GetString(const nlohmann::json &aObj, const char *aKey, const wxString &aDefault)
Raw parsed document from an EasyEDA Pro v3 .epru stream.
One parsed row from an EasyEDA Pro v3 .epru document stream.