KiCad PCB EDA Suite
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 <al.lunev@yahoo.com>
5  * Copyright (C) 2012-2021 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 
34 namespace PCAD2KICAD {
35 
36 static KEYWORD empty_keywords[1] = {};
37 static const char ACCEL_ASCII_KEYWORD[] = "ACCEL_ASCII";
38 
39 
40  void LoadInputFile( const wxString& aFileName, wxXmlDocument* aXmlDoc )
41 {
42  char line[sizeof( ACCEL_ASCII_KEYWORD )];
43  int tok;
44  XNODE* iNode = nullptr, *cNode = nullptr;
45  wxString str, propValue, content;
46  wxCSConv conv( wxT( "windows-1251" ) );
47 
48  FILE* fp = wxFopen( aFileName, wxT( "rt" ) );
49 
50  if( !fp )
51  THROW_IO_ERROR( wxT( "Unable to open file: " ) + aFileName );
52 
53  // check file format
54  if( !fgets( line, sizeof( line ), fp )
55  // first line starts with "ACCEL_ASCII" with optional stuff on same line after that.
56  || memcmp( line, ACCEL_ASCII_KEYWORD, sizeof(ACCEL_ASCII_KEYWORD)-1 ) )
57  THROW_IO_ERROR( "Unknown file type" );
58 
59  // rewind the file
60  fseek( fp, 0, SEEK_SET );
61 
62  // lexer now owns fp, will close on exception or return
63  DSNLEXER lexer( empty_keywords, 0, fp, aFileName );
64 
65  iNode = new XNODE( wxXML_ELEMENT_NODE, wxT( "www.lura.sk" ) );
66 
67  while( ( tok = lexer.NextTok() ) != DSN_EOF )
68  {
69  if( tok == DSN_RIGHT )
70  {
71  iNode = iNode->GetParent();
72  }
73  else if( tok == DSN_LEFT )
74  {
75  tok = lexer.NextTok();
76  str = wxEmptyString;
77  cNode = new XNODE( wxXML_ELEMENT_NODE, wxString( lexer.CurText(), conv ) );
78  iNode->AddChild( cNode );
79  iNode = cNode;
80  }
81  else if( cNode )
82  {
83  str = wxString( lexer.CurText(), conv );
84 
85  if( tok == DSN_STRING )
86  {
87  // update attribute
88  if( iNode->GetAttribute( wxT( "Name" ), &propValue ) )
89  {
90  iNode->DeleteAttribute( wxT( "Name" ) );
91  iNode->AddAttribute( wxT( "Name" ), propValue + wxT( ' ' ) + str );
92  }
93  else
94  {
95  iNode->AddAttribute( wxT( "Name" ), str );
96  }
97  }
98  else if( str != wxEmptyString )
99  {
100  // update node content
101  content = cNode->GetNodeContent() + wxT( ' ' ) + str;
102 
103  if( cNode->GetChildren() )
104  cNode->GetChildren()->SetContent( content );
105  else
106  cNode->AddChild( new wxXmlNode( wxXML_TEXT_NODE, wxEmptyString, content ) );
107  }
108  }
109  }
110 
111  if( iNode )
112  {
113  aXmlDoc->SetRoot( iNode );
114  //aXmlDoc->Save( wxT( "test.xml" ) );
115  }
116 }
117 
118 } // namespace PCAD2KICAD
const char * CurText() const
Return a pointer to the current token's text.
Definition: dsnlexer.h:398
void LoadInputFile(const wxString &aFileName, wxXmlDocument *aXmlDoc)
This file contains miscellaneous commonly used macros and functions.
XNODE * GetChildren() const
Definition: xnode.h:62
XNODE * GetParent() const
Definition: xnode.h:72
static KEYWORD empty_keywords[1]
Hold an XML or S-expression element.
Definition: xnode.h:43
static const char ACCEL_ASCII_KEYWORD[]
int NextTok()
Return the next token found in the input file or DSN_EOF when reaching the end of file.
Definition: dsnlexer.cpp:526
Hold a keyword string and its unique integer token.
Definition: dsnlexer.h:39
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:78