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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <pcad/s_expr_loader.h>
22
23#include <dsnlexer.h>
24#include <macros.h>
25#include <xnode.h>
26
27#include <wx/string.h>
28#include <wx/xml/xml.h>
29#include <wx/wxcrt.h>
30
31namespace PCAD2KICAD {
32
33static KEYWORD empty_keywords[1] = {};
34static const char ACCEL_ASCII_KEYWORD[] = "ACCEL_ASCII";
35
36
37void LoadInputFile( const wxString& aFileName, wxXmlDocument* aXmlDoc )
38{
39 char line[sizeof( ACCEL_ASCII_KEYWORD )];
40 int tok;
41 XNODE* iNode = nullptr, *cNode = nullptr;
42 wxString str, propValue, content;
43 wxCSConv conv( wxT( "windows-1251" ) );
44
45 FILE* fp = wxFopen( aFileName, wxT( "rt" ) );
46
47 if( !fp )
48 THROW_IO_ERROR( wxT( "Unable to open file: " ) + aFileName );
49
50 // check file format
51 if( !fgets( line, sizeof( line ), fp )
52 // first line starts with "ACCEL_ASCII" with optional stuff on same line after that.
53 || memcmp( line, ACCEL_ASCII_KEYWORD, sizeof(ACCEL_ASCII_KEYWORD)-1 ) )
54 THROW_IO_ERROR( wxT( "Unknown file type" ) );
55
56 // rewind the file
57 fseek( fp, 0, SEEK_SET );
58
59 // lexer now owns fp, will close on exception or return
60 DSNLEXER lexer( empty_keywords, 0, nullptr, fp, aFileName );
61
62 iNode = new XNODE( wxXML_ELEMENT_NODE, wxT( "www.lura.sk" ) );
63
64 while( ( tok = lexer.NextTok() ) != DSN_EOF )
65 {
66 if( tok == DSN_RIGHT )
67 {
68 iNode = iNode->GetParent();
69
70 // this will happen if there are more right parens than left
71 if( !iNode )
72 THROW_IO_ERROR( wxT( "Unexpected right paren" ) );
73 }
74 else if( tok == DSN_LEFT )
75 {
76 tok = lexer.NextTok();
77 str = wxEmptyString;
78 cNode = new XNODE( wxXML_ELEMENT_NODE, wxString( lexer.CurText(), conv ) );
79 iNode->AddChild( cNode );
80 iNode = cNode;
81 }
82 else if( cNode )
83 {
84 str = wxString( lexer.CurText(), conv );
85
86 if( tok == DSN_STRING )
87 {
88 // update attribute
89 if( iNode->GetAttribute( wxT( "Name" ), &propValue ) )
90 {
91 iNode->DeleteAttribute( wxT( "Name" ) );
92 iNode->AddAttribute( wxT( "Name" ), propValue + wxT( ' ' ) + str );
93 }
94 else
95 {
96 iNode->AddAttribute( wxT( "Name" ), str );
97 }
98 }
99 else if( str != wxEmptyString )
100 {
101 // update node content
102 content = cNode->GetNodeContent() + wxT( ' ' ) + str;
103
104 if( cNode->GetChildren() )
105 cNode->GetChildren()->SetContent( content );
106 else
107 cNode->AddChild( new wxXmlNode( wxXML_TEXT_NODE, wxEmptyString, content ) );
108 }
109 }
110 }
111
112 if( iNode )
113 {
114 aXmlDoc->SetRoot( iNode );
115 //aXmlDoc->Save( wxT( "test.xml" ) );
116 }
117}
118
119} // namespace PCAD2KICAD
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition dsnlexer.h:75
const char * CurText() const
Return a pointer to the current token's text.
Definition dsnlexer.h:411
int NextTok()
Return the next token found in the input file or DSN_EOF when reaching the end of file.
Definition dsnlexer.cpp:537
An extension of wxXmlNode that can format its contents as KiCad-style s-expressions.
Definition xnode.h:67
XNODE * GetParent() const
Definition xnode.h:107
void AddAttribute(const wxString &aName, const wxString &aValue) override
Definition xnode.cpp:88
@ DSN_LEFT
Definition dsnlexer.h:63
@ DSN_RIGHT
Definition dsnlexer.h:62
@ DSN_STRING
Definition dsnlexer.h:64
@ DSN_EOF
Definition dsnlexer.h:65
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
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:36