Skip to content

Commit 5ad24f8

Browse files
committed
DomElement: Ignore garbage bytes \x0C and \x06 in XML files
It seems that some EAGLE projects (maybe only the Arduino Nano schematic?) contain garbage bytes, effectively making the XML invalid and the parser failing. Thus now stripping those two garbage bytes from the input data.
1 parent 19a3f64 commit 5ad24f8

5 files changed

Lines changed: 14 additions & 11 deletions

File tree

parseagle/board/board.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Board::~Board() noexcept
2828

2929
void Board::load(const QByteArray& content, QStringList* errors)
3030
{
31-
const DomElement root = DomElement::parse(content);
31+
const DomElement root = DomElement::parseDocument(content);
3232
const DomElement drawing = root.getFirstChild("drawing");
3333

3434
if (drawing.hasChild("grid")) {

parseagle/common/domelement.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,21 @@ DomElement DomElement::parse(QXmlStreamReader& reader)
5151
return root;
5252
}
5353

54-
DomElement DomElement::parse(const QString& data)
54+
DomElement DomElement::parse(const QByteArray& data)
5555
{
5656
QXmlStreamReader reader;
5757
reader.addData(data);
5858
return parse(reader);
5959
}
6060

61-
DomElement DomElement::parse(const QByteArray& data)
61+
DomElement DomElement::parseDocument(QByteArray data)
6262
{
63-
QXmlStreamReader reader;
64-
reader.addData(data);
65-
return parse(reader);
63+
// Workaround for garbage in some Eagle XML files, see
64+
// https://gitlab.com/kicad/code/kicad/-/work_items/11008
65+
data.replace("\x0c", "");
66+
data.replace("\x06", "");
67+
68+
return parse(data);
6669
}
6770

6871
QString DomElement::getAttributeAsString(const QString& name) const
@@ -72,7 +75,7 @@ QString DomElement::getAttributeAsString(const QString& name) const
7275
} else {
7376
throw std::runtime_error(
7477
QString("Attribute '%1' not found in XML element '%2'.")
75-
.arg(name).arg(mName).toStdString());
78+
.arg(name, mName).toStdString());
7679
}
7780
}
7881

parseagle/common/domelement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class DomElement final
1616

1717
// Parsers
1818
static DomElement parse(QXmlStreamReader& reader);
19-
static DomElement parse(const QString& data);
2019
static DomElement parse(const QByteArray& data);
20+
static DomElement parseDocument(QByteArray data);
2121

2222
// Getters
2323
const QString& getTagName() const noexcept {return mName;}

parseagle/library.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Library::Library(const QByteArray& content, QStringList* errors)
2424

2525
Library::Library(const DomElement& root, QStringList* errors)
2626
{
27-
load(root, errors);
27+
load(root, errors);
2828
}
2929

3030
Library::~Library() noexcept
@@ -33,7 +33,7 @@ Library::~Library() noexcept
3333

3434
void Library::load(const QByteArray& content, QStringList* errors)
3535
{
36-
const DomElement root = DomElement::parse(content);
36+
const DomElement root = DomElement::parseDocument(content);
3737
const DomElement drawing = root.getFirstChild("drawing");
3838
const DomElement library = drawing.getFirstChild("library");
3939
load(library, errors);

parseagle/schematic/schematic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Schematic::~Schematic() noexcept
2727
}
2828

2929
void Schematic::load(const QByteArray& content, QStringList* errors) {
30-
const DomElement root = DomElement::parse(content);
30+
const DomElement root = DomElement::parseDocument(content);
3131
const DomElement drawing = root.getFirstChild("drawing");
3232

3333
if (drawing.hasChild("grid")) {

0 commit comments

Comments
 (0)