2012-12-29 09:54:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2013-03-09 19:36:31 +00:00
|
|
|
* Copyright (C) 2012-2013 Alexander Lunev <al.lunev@yahoo.com>
|
2022-08-31 22:57:24 +00:00
|
|
|
* Copyright (C) 2012-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-12-29 09:54:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2021-06-04 00:28:40 +00:00
|
|
|
#include <pcad/s_expr_loader.h>
|
2017-09-23 09:20:10 +00:00
|
|
|
|
2012-12-29 09:54:25 +00:00
|
|
|
#include <dsnlexer.h>
|
|
|
|
#include <macros.h>
|
|
|
|
#include <xnode.h>
|
|
|
|
|
2021-06-04 00:28:40 +00:00
|
|
|
#include <wx/string.h>
|
|
|
|
#include <wx/xml/xml.h>
|
2022-08-31 22:57:24 +00:00
|
|
|
#include <wx/wxcrt.h>
|
2021-06-04 00:28:40 +00:00
|
|
|
|
2012-12-29 09:54:25 +00:00
|
|
|
namespace PCAD2KICAD {
|
|
|
|
|
|
|
|
static KEYWORD empty_keywords[1] = {};
|
2013-06-25 16:05:51 +00:00
|
|
|
static const char ACCEL_ASCII_KEYWORD[] = "ACCEL_ASCII";
|
2012-12-29 09:54:25 +00:00
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2023-08-13 02:26:06 +00:00
|
|
|
void LoadInputFile( const wxString& aFileName, wxXmlDocument* aXmlDoc )
|
2012-12-29 09:54:25 +00:00
|
|
|
{
|
2013-06-25 16:05:51 +00:00
|
|
|
char line[sizeof( ACCEL_ASCII_KEYWORD )];
|
2012-12-29 09:54:25 +00:00
|
|
|
int tok;
|
2021-07-19 23:56:05 +00:00
|
|
|
XNODE* iNode = nullptr, *cNode = nullptr;
|
2013-06-12 19:43:22 +00:00
|
|
|
wxString str, propValue, content;
|
2012-12-29 09:54:25 +00:00
|
|
|
wxCSConv conv( wxT( "windows-1251" ) );
|
|
|
|
|
|
|
|
FILE* fp = wxFopen( aFileName, wxT( "rt" ) );
|
|
|
|
|
|
|
|
if( !fp )
|
|
|
|
THROW_IO_ERROR( wxT( "Unable to open file: " ) + aFileName );
|
|
|
|
|
2013-06-25 16:05:51 +00:00
|
|
|
// check file format
|
|
|
|
if( !fgets( line, sizeof( line ), fp )
|
2016-02-17 15:38:30 +00:00
|
|
|
// first line starts with "ACCEL_ASCII" with optional stuff on same line after that.
|
|
|
|
|| memcmp( line, ACCEL_ASCII_KEYWORD, sizeof(ACCEL_ASCII_KEYWORD)-1 ) )
|
2022-02-05 02:06:25 +00:00
|
|
|
THROW_IO_ERROR( wxT( "Unknown file type" ) );
|
2013-06-25 16:05:51 +00:00
|
|
|
|
|
|
|
// rewind the file
|
|
|
|
fseek( fp, 0, SEEK_SET );
|
|
|
|
|
2012-12-29 09:54:25 +00:00
|
|
|
// lexer now owns fp, will close on exception or return
|
2022-08-09 04:45:37 +00:00
|
|
|
DSNLEXER lexer( empty_keywords, 0, nullptr, fp, aFileName );
|
2012-12-29 09:54:25 +00:00
|
|
|
|
|
|
|
iNode = new XNODE( wxXML_ELEMENT_NODE, wxT( "www.lura.sk" ) );
|
|
|
|
|
|
|
|
while( ( tok = lexer.NextTok() ) != DSN_EOF )
|
|
|
|
{
|
|
|
|
if( tok == DSN_RIGHT )
|
|
|
|
{
|
|
|
|
iNode = iNode->GetParent();
|
2023-04-12 01:52:39 +00:00
|
|
|
|
|
|
|
// this will happen if there are more right parens than left
|
|
|
|
if( !iNode )
|
|
|
|
THROW_IO_ERROR( wxT( "Unexpected right paren" ) );
|
2012-12-29 09:54:25 +00:00
|
|
|
}
|
|
|
|
else if( tok == DSN_LEFT )
|
|
|
|
{
|
|
|
|
tok = lexer.NextTok();
|
|
|
|
str = wxEmptyString;
|
|
|
|
cNode = new XNODE( wxXML_ELEMENT_NODE, wxString( lexer.CurText(), conv ) );
|
|
|
|
iNode->AddChild( cNode );
|
|
|
|
iNode = cNode;
|
|
|
|
}
|
2013-06-12 19:43:22 +00:00
|
|
|
else if( cNode )
|
2012-12-29 09:54:25 +00:00
|
|
|
{
|
2013-06-12 19:43:22 +00:00
|
|
|
str = wxString( lexer.CurText(), conv );
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2012-12-29 09:54:25 +00:00
|
|
|
if( tok == DSN_STRING )
|
2013-06-12 19:43:22 +00:00
|
|
|
{
|
|
|
|
// update attribute
|
|
|
|
if( iNode->GetAttribute( wxT( "Name" ), &propValue ) )
|
|
|
|
{
|
|
|
|
iNode->DeleteAttribute( wxT( "Name" ) );
|
|
|
|
iNode->AddAttribute( wxT( "Name" ), propValue + wxT( ' ' ) + str );
|
|
|
|
}
|
|
|
|
else
|
2021-07-19 23:56:05 +00:00
|
|
|
{
|
2013-06-12 19:43:22 +00:00
|
|
|
iNode->AddAttribute( wxT( "Name" ), str );
|
2021-07-19 23:56:05 +00:00
|
|
|
}
|
2013-06-12 19:43:22 +00:00
|
|
|
}
|
|
|
|
else if( str != wxEmptyString )
|
|
|
|
{
|
|
|
|
// update node content
|
|
|
|
content = cNode->GetNodeContent() + wxT( ' ' ) + str;
|
|
|
|
|
|
|
|
if( cNode->GetChildren() )
|
|
|
|
cNode->GetChildren()->SetContent( content );
|
|
|
|
else
|
2021-07-19 23:56:05 +00:00
|
|
|
cNode->AddChild( new wxXmlNode( wxXML_TEXT_NODE, wxEmptyString, content ) );
|
2013-06-12 19:43:22 +00:00
|
|
|
}
|
2012-12-29 09:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( iNode )
|
|
|
|
{
|
|
|
|
aXmlDoc->SetRoot( iNode );
|
|
|
|
//aXmlDoc->Save( wxT( "test.xml" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace PCAD2KICAD
|