KiCad PCB EDA Suite
Loading...
Searching...
No Matches
s_expr_loader.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2012-2013 Alexander Lunev <[email protected]>
5 * Copyright (C) 2012-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <pcad/s_expr_loader.h>
26
27#include <dsnlexer.h>
28#include <macros.h>
29#include <xnode.h>
30
31#include <wx/string.h>
32#include <wx/xml/xml.h>
33#include <wx/wxcrt.h>
34
35namespace PCAD2KICAD {
36
37static KEYWORD empty_keywords[1] = {};
38static const char ACCEL_ASCII_KEYWORD[] = "ACCEL_ASCII";
39
40
41void LoadInputFile( const wxString& aFileName, wxXmlDocument* aXmlDoc )
42{
43 char line[sizeof( ACCEL_ASCII_KEYWORD )];
44 int tok;
45 XNODE* iNode = nullptr, *cNode = nullptr;
46 wxString str, propValue, content;
47 wxCSConv conv( wxT( "windows-1251" ) );
48
49 FILE* fp = wxFopen( aFileName, wxT( "rt" ) );
50
51 if( !fp )
52 THROW_IO_ERROR( wxT( "Unable to open file: " ) + aFileName );
53
54 // check file format
55 if( !fgets( line, sizeof( line ), fp )
56 // first line starts with "ACCEL_ASCII" with optional stuff on same line after that.
57 || memcmp( line, ACCEL_ASCII_KEYWORD, sizeof(ACCEL_ASCII_KEYWORD)-1 ) )
58 THROW_IO_ERROR( wxT( "Unknown file type" ) );
59
60 // rewind the file
61 fseek( fp, 0, SEEK_SET );
62
63 // lexer now owns fp, will close on exception or return
64 DSNLEXER lexer( empty_keywords, 0, nullptr, fp, aFileName );
65
66 iNode = new XNODE( wxXML_ELEMENT_NODE, wxT( "www.lura.sk" ) );
67
68 while( ( tok = lexer.NextTok() ) != DSN_EOF )
69 {
70 if( tok == DSN_RIGHT )
71 {
72 iNode = iNode->GetParent();
73
74 // this will happen if there are more right parens than left
75 if( !iNode )
76 THROW_IO_ERROR( wxT( "Unexpected right paren" ) );
77 }
78 else if( tok == DSN_LEFT )
79 {
80 tok = lexer.NextTok();
81 str = wxEmptyString;
82 cNode = new XNODE( wxXML_ELEMENT_NODE, wxString( lexer.CurText(), conv ) );
83 iNode->AddChild( cNode );
84 iNode = cNode;
85 }
86 else if( cNode )
87 {
88 str = wxString( lexer.CurText(), conv );
89
90 if( tok == DSN_STRING )
91 {
92 // update attribute
93 if( iNode->GetAttribute( wxT( "Name" ), &propValue ) )
94 {
95 iNode->DeleteAttribute( wxT( "Name" ) );
96 iNode->AddAttribute( wxT( "Name" ), propValue + wxT( ' ' ) + str );
97 }
98 else
99 {
100 iNode->AddAttribute( wxT( "Name" ), str );
101 }
102 }
103 else if( str != wxEmptyString )
104 {
105 // update node content
106 content = cNode->GetNodeContent() + wxT( ' ' ) + str;
107
108 if( cNode->GetChildren() )
109 cNode->GetChildren()->SetContent( content );
110 else
111 cNode->AddChild( new wxXmlNode( wxXML_TEXT_NODE, wxEmptyString, content ) );
112 }
113 }
114 }
115
116 if( iNode )
117 {
118 aXmlDoc->SetRoot( iNode );
119 //aXmlDoc->Save( wxT( "test.xml" ) );
120 }
121}
122
123} // namespace PCAD2KICAD
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:80
const char * CurText() const
Return a pointer to the current token's text.
Definition: dsnlexer.h:399
int NextTok()
Return the next token found in the input file or DSN_EOF when reaching the end of file.
Definition: dsnlexer.cpp:518
Hold an XML or S-expression element.
Definition: xnode.h:44
XNODE * GetParent() const
Definition: xnode.h:72
XNODE * GetChildren() const
Definition: xnode.h:62
@ DSN_LEFT
Definition: dsnlexer.h:68
@ DSN_RIGHT
Definition: dsnlexer.h:67
@ DSN_STRING
Definition: dsnlexer.h:69
@ DSN_EOF
Definition: dsnlexer.h:70
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
This file contains miscellaneous commonly used macros and functions.
void LoadInputFile(const wxString &aFileName, wxXmlDocument *aXmlDoc)
static KEYWORD empty_keywords[1]
static const char ACCEL_ASCII_KEYWORD[]
Hold a keyword string and its unique integer token.
Definition: dsnlexer.h:41